Community Forums › Forums › Archived Forums › General Discussion › Why does WP ignore the content of my taxonomy-portfolio.php?
Tagged: Custom Post Type, custom taxonomy, taxonomy.php
- This topic has 9 replies, 2 voices, and was last updated 8 years, 11 months ago by
mocca.
-
AuthorPosts
-
May 20, 2016 at 2:45 pm #185986
mocca
MemberHi!
For my custom theme I created a Custom Post Type „Portfolio“ which has the custom taxonomy „Work“. Everything works well. Now, I want a template for the list where all custom posts assigned to a certain taxonomy term i.e. “logo” are listed. If this file is called taxonomy.php everything looks as I described it in the custom loop. Unfortunately, this template shows me not only the custom posts assigned the taxonomy term “logo“ but all custom posts within the CPT “Portfolio”. So I renamed the template file to taxonomy-portfolio.php (with no changes within the file). Now I get a list with exactly the posts which are assigned to i.e. ”logo”. Problem: WordPress now ignores the content (i.e. my custom loop) of the taxonomy-portfolio.php and uses the Genesis fallback template file (archive.php).
Why does WP ignore the content of my taxonomy-portfolio.php whereas it displays everything correctly when I rename the file to taxonomy.php?
This is the code of the taxonomy-portfolio.php:
<?php
/** * * Template for the custom taxonomy “Work“ * Description: Lists all custom posts assigned to a specific taxonomy term within the CPT "Portfolio" * */ remove_action ('genesis_loop', 'genesis_do_loop'); // Remove the standard loop add_action( 'genesis_loop', 'custom_loop' ); // Add custom loop function custom_loop() { echo '<div class="page entry">'; echo '<div class="entry-content">'; $args = array( 'post_type' => 'portfolio', 'orderby' => 'menu_order', 'order' => 'ASC', ); $loop = new WP_Query( $args ); if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post(); global $post; echo '<div id="portfolio-item">'; echo '<div class="pic">'. get_the_post_thumbnail( $id, array(300,380) ).'</div>'; echo '<h3>' . get_the_title() . '</h3>'; echo get_the_excerpt(); echo '<a href="' . get_post_permalink() . '" class="readmore">read more</a>'; echo '</div>'; endwhile; endif; echo '</div><!-- end .entry-content -->'; echo '</div><!-- end .page .entry -->'; } genesis();
Thanks!
May 20, 2016 at 2:55 pm #185987mocca
MemberThis is the code in the functions.php:
/* * Custom Post Type: Portfolio */ function post_type_portfolio() { register_post_type( 'portfolio', array( 'label' => __('Portfolio'), 'public' => true, 'show_ui' => true, 'query_var' => true, 'taxonomies' => array( 'work' ), 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'), ) ); } add_action('init', 'post_type_portfolio'); /** * Add custom taxonomies */ function add_custom_taxonomies() { // Add new "Work" taxonomy to Posts register_taxonomy('work', 'portfolio', array( 'hierarchical' => true, 'labels' => array( 'name' => _x( 'Work', 'taxonomy general name' ), 'singular_name' => _x( 'Work', 'taxonomy singular name' ), 'search_items' => __( 'Search Work' ), 'all_items' => __( 'All Work' ), 'parent_item' => __( 'Parent Work' ), 'parent_item_colon' => __( 'Parent Work:' ), 'edit_item' => __( 'Edit Work' ), 'update_item' => __( 'Update Work' ), 'add_new_item' => __( 'Add New Work' ), 'new_item_name' => __( 'New Work Name' ), 'menu_name' => __( 'Work' ), ), 'rewrite' => array( 'slug' => 'work', 'with_front' => false, 'hierarchical' => true ), )); } add_action( 'init', 'add_custom_taxonomies', 0 ); // Make archives.php Include Custom Post Types function add_custom_types( $query ) { if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'post_type', array( 'post', 'nav_menu_item', 'portfolio' )); return $query; } } add_filter( 'pre_get_posts', 'add_custom_types' );
May 20, 2016 at 3:45 pm #185992Victor Font
ModeratorIf you name a file taxonomy-portfolio, WordPress looks for a taxonomy named portfolio. In your code, you named the taxonomy 'Work', not portfolio. Portfolio is your post type. The correct name for your taxonomy template is taxonomy-work.php.
https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?May 20, 2016 at 3:48 pm #185993mocca
MemberI tried that also but that didn't work either. WP uses the content of the template (good!) but displays all custom posts within "Portfolio" not only those assigned to a specific taxonomy term i.e. "logo".
May 21, 2016 at 6:22 am #186013mocca
MemberI’m a huge step further. Of course it was essential to give the file the correct name taxonomy-work.php (thanks for that hint).
Then I used this snippet to show which template file WP uses on a certain page:
function show_template() { if ( current_user_can( 'create_users' ) ) { global $template; print_r($template); } } add_action('wp_head', 'show_template');
If I use the wrong file name (i.e. taxonomy-portfolio.php) WP uses the index.php as the fallback template. So within Genesis’ standard loop there must be something my custom loop is lacking because the standard loop displays only the custom posts assigned to a certain taxonomy term i.e. “logo”. That’s what my taxonomy-work.php should do also.
Can anybody give me a hint what's wrong in my taxonomy-work.php (see code above “taxonomy-portfolio.php”)?
May 21, 2016 at 6:44 am #186014Victor Font
ModeratorYour loop is not querying for anything taxonomy related. All you are querying is custom post types. You need to include taxonomy in your query:
$taxquery = array( array( 'taxonomy' => 'work', 'field' => 'slug', 'terms' => array( 'logo' ), ) ); $query->set( 'tax_query', $taxquery );
I'm not sure is this will work as is for your loop, but you need to include something like this in your query.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?May 21, 2016 at 8:26 am #186020mocca
MemberOh, wow, cool!!!! Now it works!
This is the code, I use now:
$args = array( 'post_type' => 'portfolio', 'orderby' => 'menu_order', 'order' => 'ASC', // NEW: 'tax_query' => array( array( 'taxonomy' => 'work', 'field' => 'slug', 'terms' => array( 'logo', 'flyer', 'corporate design' ), ), ), );
One little question: what, if the client changes the taxonomy terms i.e. rename an existing one or add new ones? Does she always have to ask me to modify the template file? Is there a solution for this?
May 21, 2016 at 9:17 am #186024Victor Font
ModeratorYou should be able to remove the terms and pull in all terms for the work taxonomy.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?May 21, 2016 at 3:46 pm #186035mocca
MemberI'm sorry, but I don't know what you mean. I want the system to generate automatically all terms without typing in them manually (which is not very practical if the client changes the terms in the future).
I guess that I have to create a solution like this:
'terms' => $terms,
And then probably something like:
$terms = get_the_terms( $post->ID, 'work');
But that's not enough. I need some further php.
May 21, 2016 at 6:33 pm #186045mocca
MemberI could solve it! 🙂
This code works for me (taxonomy-work.php):
function custom_loop() { $taxonomy_terms = get_the_terms( $post->ID, 'work' ); $taxonomy_term = array(); foreach ( $taxonomy_terms as $term ) { $taxonomy_term[] = $term->slug; } $args = array( 'post_type' => 'portfolio', 'orderby' => 'menu_order', 'order' => 'ASC', // Querying for the taxonomy 'tax_query' => array( array( 'taxonomy' => 'work', 'field' => 'slug', 'terms' => $taxonomy_term, ), ), ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ): while ( $loop->have_posts() ): $loop->the_post(); global $post; // Do something endwhile; endif; }
Thank you very much for your help, Victor!
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.