Archived topic
Conditional Meta Items Using Post Category
3 replies · Started by Eric on November 24, 2021
Hello,
I spend a fair amount of time with the functions from the documentation to add a custom meta item following some of the topics on the forum.
https://docs.generatepress.com/article/generate_header_entry_meta_items/
Now I want to build the list of meta items based on the post category, is it possible? In the below snippet, instead of is_single() can I fetch the post category somehow?
I have also seen the suggestion to use Block Elements with the GenerateBlock but I am not sure if I can add custom meta items that I have added.
Thanks!
add_filter( 'generate_header_entry_meta_items', function( $items ) {
if( is_single() ){
return array(
//'categories',
'date'
);
}
return $items;
} );
Hi there,
Now I want to build the list of meta items based on the post category, is it possible? In the below snippet, instead of is_single() can I fetch the post category somehow?
It's possible.
Here's an example: Checks if the page is a single post page and has category "Movies".
add_filter( 'generate_header_entry_meta_items', function( $items ) {
if( is_single() && has_category('Movies') ){
return array(
//'categories',
'date'
);
}
return $items;
} );
I have also seen the suggestion to use Block Elements with the GenerateBlock but I am not sure if I can add custom meta items that I have added.
Ah yes, what you mean is the Block Element - Content Template. Here's a demo video -
https://www.youtube.com/watch?v=-ZTQP_KA2xE
That's actually much easier. It's pretty straight-forward. You place a dynamic text block for each meta item you want to display and set the post meta to be displayed for each block. I suggest trying it. :D
The code snippet works great!
Thank you.
I just downloaded Generate Blocks and will get familiar with it.
I just downloaded Generate Blocks and will get familiar with it.
That's good. You're missing out on GP Premium's Block Element features if you're not utilizing it. It's worth learning. :)
No problem. Glad to be of any help. :D