Hi i need get term ID from custom taxonomy but it is always NULL.
taxonomy-galerie-kategorie.php
<?php
/**
* A custom taxonomy template for gallery.
*/
?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/html-header', 'parts/shared/header' ) ); ?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/templates/header-image' ) ); ?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/templates/gallery' ) ); ?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/footer','parts/shared/html-footer' ) ); ?>
query_posts(['post_type' => 'galerie']);
$terms = get_categories([
'taxonomy' => 'galerie-kategorie',
'hide_empty' => false,
'orderby' => 'date',
'order' => 'ASC'
]);
var_dump(get_queried_object());
Can someone help me please?
Hi i need get term ID from custom taxonomy but it is always NULL.
taxonomy-galerie-kategorie.php
<?php
/**
* A custom taxonomy template for gallery.
*/
?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/html-header', 'parts/shared/header' ) ); ?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/templates/header-image' ) ); ?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/templates/gallery' ) ); ?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/footer','parts/shared/html-footer' ) ); ?>
query_posts(['post_type' => 'galerie']);
$terms = get_categories([
'taxonomy' => 'galerie-kategorie',
'hide_empty' => false,
'orderby' => 'date',
'order' => 'ASC'
]);
var_dump(get_queried_object());
Can someone help me please?
Share Improve this question asked Dec 4, 2019 at 15:05 d3tr1tusd3tr1tus 1114 bronze badges 3 |1 Answer
Reset to default 1You don't need to use any query at all, especially not query_posts
.
Instead, use the pre_get_posts
action to make sure that any query for that taxonomy for a frontend archive, has the appropriate post types:
add_action( 'pre_get_posts', function( \WP_Query $q ) {
// we only want the main query for galerie-kategorie archives
if ( ! $q->is_main_query() || !$q->is_tax( 'galerie-kategorie' ) ) {
return;
}
$q->set( 'post_type', 'galerie' );
});
Now it will make sure WP grabs what you wanted the first time around.
Otherwise, by using a whole new WP_Query
/query_post
you:
- double the number of queries, WP did all that work and you've just thrown it in the trash and told it to do it all over again! Poor WP :(
- Make your page twice as slow as it has to do everything twice
- Breaks pagination! Now you have to do a tonne of extra work to get it working again, and you have to write custom pagination code to get the links working
- Introduces compatibility problems with plugins and code that use
pre_get_posts
- Made your code longer
- Cluttered up your templates, a
pre_get_post
filter can be hidden away infunctions.php
If you ever need to modify ro change the posts WP fetches from the database, use pre_get_posts
. Think of it like changing your food order before you give it to kitchen. The last thing you want is to order food, have it arrive, then change your mind and have to wait again for new food.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744940146a4602272.html
query_posts
to change the what gets shown on a page, I would purge all knowledge of that function as there are no valid uses of it in normal web development. Use thepre_get_posts
filter to change what gets fetched from the database instead, and you'll avoid a lot of problems, and your site will be faster. For example you might not have noticed, but your pagination will be broken because you usedquery_posts
– Tom J Nowell ♦ Commented Dec 4, 2019 at 15:45new WP_Query
instead and everythink is fine :) – d3tr1tus Commented Dec 4, 2019 at 16:09pre_get_posts
filter, you've gone from a 1/10 to a 3/10,pre_get_posts
would be a 9/10 that uses half the code – Tom J Nowell ♦ Commented Dec 4, 2019 at 16:21