1

I want to list all posts with a corresponding template for a custom taxonomy term of a custom post type. To make it easier to understand:

The custom post type is called PUBLICATIONS and has a custom taxonomy called LISTS. Each list template is slighty different, so when all posts are listed on the archive-publications page, posts in term LIST A shall be displayed using Template A, LIST B Template B, and so on.

I've tried this:

<?php
function publikationen_archive() {
    if ( is_archive('publikationen') && is_tax('downloads') ) { get_template_part( 'templates/content-downloads' );
    } elseif ( is_archive('publikationen') && is_tax('sonderbaende-kataloge') ) { 
    get_template_part( 'templates/content-sonderbaende-kataloge' );
    } elseif ( is_archive('publikationen') && is_tax('neuerscheinungen') ) { 
    get_template_part( 'templates/content-neuerscheinungen' );
    } elseif ( is_archive('publikationen') && is_tax('untersuchungen') ) { 
    get_template_part( 'templates/content-untersuchungen' );
    } elseif ( is_archive('publikationen') && is_tax('studien-materialien') ) { 
    get_template_part( 'templates/content-studien-materialien' );
    }
} ?>

which didn't work. Then I came across the filter single_template, template_redirect and template_include but don't really know what to do with them.

3
  • is_post_type_archive is for checking if the view is a custom post type archive. has_term is for checking if a post has a particular taxonomy term. – Milo Jan 16 '15 at 17:22
  • Please post your solution as an answer and accept it. :-) – Pieter Goosen Jan 17 '15 at 16:54
  • done, as suggested :) – gfaw Jan 17 '15 at 17:58
1

I solved it.

SOLUTION:

<?php 
if ( has_term( 'downloads', 'listen', $post->ID ) ) {
    get_template_part( 'templates/content-downloads-vergriffener-baende' ); 
} 
elseif ( has_term( 'untersuchungen', 'listen', $post->ID ) ) { 
    get_template_part( 'templates/content-untersuchungen' ); 
} 
elseif ( has_term( 'studien-materialien', 'listen', $post->ID ) ) { 
    get_template_part( 'templates/content-studien-materialien' ); 
} 
elseif ( has_term( 'sonderbaende-kataloge', 'listen', $post->ID ) ) { 
    get_template_part( 'templates/content-sonderbaende-kataloge' ); 
}
?>

This post: http://wpquestions.com/question/show/id/2038 has been greatly helpful in the process.

1
  • Of course you are right. Forgot to mention that the loop is in another template-part: – gfaw Jan 19 '15 at 15:06
0

in your post loop. use this code.

<?php while (have_posts()) : the_post(); 
            $post_type = get_post_type(get_the_ID());
            if($post_type !='post'){
                get_template_part('content-' . $post_type, get_post_format());
            }
            else {
               get_template_part( 'content', get_post_format() );
            }
            //If comments are open or we have at least one comment, load up the comment template.
                    // if ( comments_open() || get_comments_number() ) {
                    //  comments_template( '', true );
                    // }
                //endwhile; ?>
            <?php comments_template( '', true ); ?>
        <?php endwhile; // end of the loop. ?>

then create content-publication.php for your post type page structure.

1
  • 1
    Of course you are right. Forgot to mention that the loop is in another template-part. – gfaw Jan 26 '15 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.