How to set a certain category
-
Hi Daniel,
Siple but effective, thnx!
But:
How do I set a certain category of posts or category-ID (and sub-category) to the index?Thnx!
AK
Zaandam
-
Hi,
For more advanced queries that are not handled by the shortcode you need to use the WP_Query-compatible interface via
the_a_z_listing()
function in your theme.e.g. to show posts with ‘term1’ and ‘term2’ from taxonomy ‘tax-slug’:
the_a_z_listing( array( 'tax_query' => array( 'taxonomy' => 'tax-slug', 'terms', array('term1', 'term2') ) ) );
-
This reply was modified 8 years, 4 months ago by
Dani Llewellyn.
Thanks Daniel!
Do have to paste that into my child functions.php? Because when I do, the (page, not post-) list is above my header ??
Sorry, but I am not much of a ‘coder’… but I can copy/paste ??
Hi, sorry about the delay replying, I overlooked the email notification.
The code that I shared in my previous message needs to be entered into a theme template file. You can copy index.php in your theme and replace the loop section with the code above. The WordPress template hierarchy is useful to determine which file name you should use.
If your theme is not made by yourself, but obtained off the internet, then it is recommended to not modify directly, but to create a Child Theme so that you are able to update the theme from the ‘net without overwriting/losing your changes.
Hi Daniel,
I have some issues displaying the content from a specific category. I use the code below in my template to show all posts from the category “frankrijk”.
<?php the_a_z_listing( array( 'tax_query' => array( 'taxonomy' => 'frankrijk', ) ) ); ?>
Unfortunately it keep showing my pages instead of my posts in the listing. Can you help me out?
Best,
Stephan-
This reply was modified 8 years, 2 months ago by
stevv.
Stevv,
For taxonomy queries, referring to the documentation on WP_Query should provide the relevant information. If you are querying for posts assigned the term of “frankrijk” from the taxonomy called “Categories” which is enabled by default when you first install WordPress then try:
the_a_z_listing( array( 'tax_query' => array( 'taxonomy' => 'category', 'terms', array('frankrijk') ) ) );
-
This reply was modified 8 years, 2 months ago by
Dani Llewellyn. Reason: fixed name of taxonomy 'category'
Hi Daniel,
Thanks for your quick reply! I tried the customized php code, but it keeps showing me an a-z index of all pages. I also test it on the post_tag taxonomy, but it also shows an index with pages. No sure what I am doing wrong.
Best
you probably need to specify the post-type if you want something other than pages shown. Amending the example to include a post-type of
post
, which is a default in-built post-type, to show entries from your ‘blog’ you should get:the_a_z_listing( array( 'post_type' => 'post', 'tax_query' => array( 'taxonomy' => 'category', 'terms', array('frankrijk') ) ) );
The function called
the_a_z_listing()
takes an array as it’s argument similarly tonew WP_Query()
where both the in-built WP_Query and the_a_z_listing() use the same array layout and parameters.-
This reply was modified 8 years, 2 months ago by
Dani Llewellyn. Reason: got the slug of posts post-type wrong - it's not plural!
Thanks again for your help! This fixed showing the pages instead of the posts. Now it is showing my posts. Still have issues showing a specific category (it is showing all), but will study the WP Query Class Reference for that.
If your tax_query matches no posts then it will default to displaying every post instead. This can be confusing when you expect it to display an empty list because you haven’t assigned the term to any posts.
I’m tentatively setting this thread as resolved. If this is incorrect please comment further and I’ll re-visit to help you as best I can ??
am trying the below code to list posts from a particular category but still it shows all the posts in all categories. any help would be appreciated
the_a_z_listing(
array(
‘post_type’ => ‘post’,
‘tax_query’ => array(
‘taxonomy’ => ‘category’,
‘terms’, array(‘textniz’)
)
)
);it looks like your code is very slightly mistyped, by a single character. After your ‘terms’ key you placed a comma which makes ‘terms’ a value and the next parameter also a value, instead of a key-value pair.
Try:
the_a_z_listing( array( 'post_type' => 'post', 'tax_query' => array( 'taxonomy' => 'category', 'terms' => array( 'textniz' ) ) ) );
Note the slight amendment to the ‘terms’ line. I believe this should fix it for you.
Aah, I see the mistake was in my previous example code above. I can’t edit that post any more to correct it, so hopefully the last post will help anyone who is confused by my fat fingers ??
i got it working with the below code. thanks so much daniel
the_a_z_listing( array( ‘post_type’ => ‘post’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘category’,
‘field’ => ‘slug’,
‘terms’ => ‘a-z-search’,
),
),
)
); -
This reply was modified 8 years, 4 months ago by
- The topic ‘How to set a certain category’ is closed to new replies.