Forum Replies Created

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thank you very much!! That was actually the problem.

    Having the same issue here …

    Thread Starter blackdolphin

    (@blackdolphin)

    I could solve it ??

    This works for me:

    $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',??
    ?	                'tax_query' => array(
    ?		           array(
    ?			     'taxonomy' => 'work',
    ?			     'field'    => 'slug',
    ?			     'terms'    => $taxonomy_term,
    ?		),
    ?	),
    ?);?

    Without you I would have give up much earlier. Thanks again for your help! Now I’m very proud to get this issue solved finally ?? I’m sure I can use this for other projects, too.

    For all who might have the same question, this is the final code:

    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
     *
     * https://codex.www.ads-software.com/Function_Reference/register_taxonomy
     */
    
    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' ),
    		),
    // Control the slugs used for this taxonomy
    		'rewrite' => array(
    			'slug' => 'work',
    			'with_front' => false,
    			'hierarchical' => true
    		),
    	));
    }
    add_action( 'init', 'add_custom_taxonomies', 0 );

    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;
    
    			// My custom loop
    
    		endwhile;
    
    	endif;
    }

    single-portfolio.php (this code is Genesis specific. The shortcode represents the get_the_term_list() function:

    // Customize the entry meta
    add_filter( 'genesis_post_meta', 'ms_entry_meta' );
    function ms_entry_meta( $post_meta ) {
    	$post_meta = '[post_terms taxonomy="work" Before="" ] ';
    	return $post_meta;
    }

    Non Genesis solution:

    $post_meta = get_the_term_list( $post->ID, ‘work’, ”, ‘, ‘, ” );

    Thread Starter blackdolphin

    (@blackdolphin)

    Finally I could resolve the issue. Your little code snippet helped me a lot as it shows me that WP falls back to the index.php with the Genesis standard loop when the file name of the taxonomy-work.php is wrong i.e. because it’s called taxonomy-portfolio.php. I assumed that the mistake must be within my custom loop.

    I have to insert this code to include the taxonomy to the custom loop:

    'tax_query' => array(
      array(
        'taxonomy' => 'work',
        'field'    => 'slug',
        'terms'    => array( 'logo',  'flyer',  'corporate design' ),
      ),
    ),

    My new challenge is a variable for “terms” for the case the client adds new taxonomy terms or renames existing ones. The solution above doesn’t work for this case. But this is a new question. My inital problem is solved now.

    Thank you very much for your extensive help and patience which I really appreciate and don’t take for granted!!!!

    Thread Starter blackdolphin

    (@blackdolphin)

    I guess that the correct name of the file is taxonomy-work.php (taxonomy-{taxonomy}.php) and not taxonomy-portfolio.php (“portfolio” is the name of the CPT). Then everything is displayed properly. But for any reason WP displays not only the custom posts assigned to the specific taxonomy term i.e. “logo” but all posts within the CPT “Portfolio”. So the question is probably: how to fix THIS issue?

    Thread Starter blackdolphin

    (@blackdolphin)

    I (almost) managed it! ??

    There’s only one little thing left: if I rename the template file in taxonomy.php my custom loop is displayed properly and everything looks pretty – except the fact that there’s not only a list of i.e. all custom posts filed under “logo” but a list of ALL custom posts regardless their taxonomy term. I guess that’s the correct behavior.

    So I renamed the file in taxonomy-portfolio.php. Now the list is correct (only custom posts assigned to “logo”). But WP ignores my customizations in taxonomy-portfolio.php and obviously uses the parent theme’s fallback template (of the Genesis framework).

    What could be my mistake?

    Thread Starter blackdolphin

    (@blackdolphin)

    Wow, thank you very much for the detailed response. I read it several times ??

    First of all I renamed the taxonomy from “portfolio” to “work” to not mixing it up with the CPT’s name which is “portfolio”.

    In fact I don’t want the “portfolio” CPT use the standard (blog) categories which will be completely different. The categories of the portfolio and the blog should be completely independent from each other which isn’t the case at the moment. I can also see the CPT category terms within the posts section (not good!).

    I guess that I don’t have to change

    'taxonomies' => array( ?work’ ),?

    to

    'taxonomies' => array( ?work‘,’category','post_tag' ),?

    … because I don’t want the CPT to be able to use the standard categories from the blog (posts section). Is this right?

    Registering the CPT: yes, of course I registered it. I just omit to copy the code which comes immediately after the other code block (sorry!)

    When I refer to ‘category’ I mean the single category term like logo, corporate design, etc. As mentioned above I want to use this category terms independently from the category terms within the blog. Even in the case the client should also have a blog category “logo” the posts listed on the “logo“ archive page (blog) shouldn’t include the “logo“ posts of the portfolio (CPT).

    In fact I don’t need a category.tpl or an archive.tp but obviously a taxonomy-work.tpl as the blog will have its own template files for the category term lists and I want to display the posts grouped by a single term i.e. ?logo“. But if I rename the category.tpl to taxonomy-work.tpl WorPress doesn’t use this template to show the posts filed under a certain category term (i.e. logo). WP uses the standard loop instead of my custom loop (all my customizations are gone). But now it displays the correct posts i.e. all posts filed under “logo” (success!!!!).

    Could you help me with this issue?

    Thank you very much for your patience!

Viewing 7 replies - 1 through 7 (of 7 total)