- This topic has 5 replies, 2 voices, and was last updated 3 years, 4 months ago by Tom.
-
AuthorPosts
-
December 10, 2020 at 2:14 am #1574031Tereza
Hello,
I have custom post type with some ACF. I would like to display this ACF under the title of CPT in Archive of this CPT and in the Category of this CPT. In Archive it works perfectly, but in category this hook is missing.
I add the these rules for location:
– CPT Category – ‘name of the category’
– CPT Archive
– CPT Category Archive – ‘name of the category’
I use the hook called ‘generate_after_entry_title’At first time I tried to create my own files for this CPT: archive-nameofcpt.php and content-nameofcpt.php, but the hook disappeared from Archive too, so I deleted it. Should I create some special files for category or something else?
I work on localhost for this time, but I try to upload it on some webhosting if you need to see it.
Thank you
December 10, 2020 at 3:32 am #1574106TerezaAnd second thing is, that If I want to test it, and I pick the location of the hook as:
Post Category Archive – all categories
and press Update, this setting is automatically changed to CPT Category Archive and nothing is displayed. π
December 10, 2020 at 4:51 am #1574192TerezaWell, I found the cause of the problem. I use this filter in functions.php for displaying the CPT (named pedagog) in categories. If I delete it, the hook works in categories for Posts. But categories dont display the CPT. How to display the CPT in categories with working hooks, um..?
add_filter('pre_get_posts', 'query_post_type'); function query_post_type($query) { if( is_category() ) { $post_type = get_query_var('post_type'); if($post_type) $post_type = $post_type; else $post_type = array('nav_menu_item', 'post', 'pedagog'); $query->set('post_type',$post_type); return $query; } }
December 10, 2020 at 3:20 pm #1575000TomLead DeveloperLead DeveloperHi there,
What exactly is that filter trying to do? May be able to offer an alternative.
Let me know π
December 11, 2020 at 3:47 am #1575760TerezaHello Tom,
thank you for your reply. I searched how to rebuild the code of the filter above. I try this and now the hooks work πfunction custom_post_type_cat_filter($query) { if ( !is_admin() && $query->is_main_query() ) { if ($query->is_category()) { $query->set( 'post_type', array( 'post', 'pedagog' ) ); } } } add_action('pre_get_posts','custom_post_type_cat_filter');
It displays the custom post types called ‘pedagog’ in category archive. Without this code I can add category to the CPT, but nothing is displayed on category archive. I am sorry for the interruption π
December 11, 2020 at 10:43 am #1576510TomLead DeveloperLead DeveloperAwesome, all working correctly now? π
-
AuthorPosts
- You must be logged in to reply to this topic.