0

I have custom post type of portfolio and I would like to be able to re-use most of the templating, but change to content-portfolio when the page is a custom post type named portfolio.

At present this is not working, so I'm not sure if I need to add Custom Post Types in the Main Query or add it inside the while loop?

index.php CODE:

get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php if ( have_posts() ) : ?>

            <header>
                <?php if( single_post_title( '', false ) ) : ?>
                    <h1 class="page-title"><?php single_post_title(); ?></h1>
                <?php else : ?>
                    <h1 class="page-title">
                        <?php echo get_bloginfo( 'name' ); ?>
                    </h1>
                <?php endif; ?>

            </header>


        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>

            <?php
              if(get_post_type()) : {
                      get_template_part( 'template-parts/content', get_post_type() );
                  elseif :
                      get_template_part( 'template-parts/content', get_post_format() );
                endif;
            }
                ?>

        <?php endwhile; ?>

    <?php else : ?>

        <?php get_template_part( 'template-parts/content', 'none' ); ?>

    <?php endif; ?>

    </main><!-- #main -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
  • I see my error. I mean it's not working. – breathee Mar 22 '17 at 17:55
  • This is the part I was hoping would work based on the type. But I'm not sure it's working inside the while loop, or I've not referenced the custom post type. [code] <?php if(get_post_type()) : { get_template_part( 'template-parts/content', get_post_type() ); elseif : get_template_part( 'template-parts/content', get_post_format() ); endif; } ?> – breathee Mar 22 '17 at 18:07
0

Make sure you are using the correct template file:

First, you need to make sure index.php is being used as the template. You may install What The File Plugin, which will show what template file is being used at the top bar when you're logged in.

Set the proper condition for portfolio:

Once you are sure that index.php is used as the template and you have template-parts/content-portfolio file within your active theme, then you may use the following CODE for the get_post_type() check to determine which template-part should be used:

<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>

    <?php
        if( get_post_type() === 'portfolio' ) :
              get_template_part( 'template-parts/content', get_post_type() );
        else :
              get_template_part( 'template-parts/content', get_post_format() );
        endif;
    ?>

<?php endwhile; ?>

Update: CODE swap based on comment:

Replace:

<?php
    if ( has_post_format( array( 'gallery', 'video', 'image' ) ) ) {
        get_template_part( 'template-parts/content', get_post_format() );
    }
    else { 
        get_template_part( 'template-parts/content', 'single' );
    }
?>

with:

<?php
    if( get_post_type() === 'portfolio' ) {
        get_template_part( 'template-parts/content', get_post_type() );
    }
    else if ( has_post_format( array( 'gallery', 'video', 'image' ) ) ) {
        get_template_part( 'template-parts/content', get_post_format() );
    }
    else { 
        get_template_part( 'template-parts/content', 'single' );
    }
?>
  • Thanks for helping Fayaz, but it didn't work. I've installed [wordpress.org/plugins/what-the-file/](What The File Plugin), very useful!. However when I'm on the portfolio single page (details) it's using the template-parts/content-single.php. I can just use single-portfolio.php which does work but I was hoping or my understanding that I can re-use using the get_template_part call just divert it to template-parts/content-portfolio.php. The main /portfolio page which is diaplaying a loop grid of the custom post types is using page.php [template-parts/content-page.php]. – breathee Mar 23 '17 at 10:41
  • So I just found the portfolio-single page is using single.php with a ` <?php if ( has_post_format( array( 'gallery', 'video', 'image' ) ) ) { get_template_part( 'template-parts/content', get_post_format() ); } else { get_template_part( 'template-parts/content', 'single' ); } ?>` I not sure how to re-write that as an elseif? – breathee Mar 23 '17 at 11:29
  • So replacing the original if, else in single.php with the above code answer the single portfolio page is picking up the template-parts/content-portfolio.php. I just need some help with the logic to mix the original if, else to include the get_post_type() === 'portfolio'` – breathee Mar 23 '17 at 13:18
  • okay, scrap that. I just realised my code does do a get_post_format() check. So the above worked I just had it in the wrong file. [wordpress.org/plugins/what-the-file/] (What The File Plugin) really helped here! Thanks Fayaz – breathee Mar 23 '17 at 13:26
0

So I'm using get_template_part with template-parts\content-___.php to be able re-use parts of my template structure. I was using a custom post type portfolio and I wanted my template to automatically pick up the -portfolio.php rather than use single-portfolio.php. Using the What The File Plugin I finally realised it was using single.php file with the template-part\content-single.php. The logic needed changing inside the file single.php which originally was:

<?php 
  if ( has_post_format( array( 'gallery', 'video', 'image' ) ) ) {
   get_template_part( 'template-parts/content', get_post_format() );
  } else {
   get_template_part( 'template-parts/content', 'single' );
  } 
?>

and changed this to:

<?php 
  if( get_post_type() === 'portfolio' ) :
    get_template_part( 'template-parts/content', get_post_type() );
  else :
    get_template_part( 'template-parts/content', get_post_format() );               
  endif;
?>

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.