0

I am having trouble getting get_template_part() to work on my index.php page. The template part is in the loop and is referencing a 'standard' post format.

template code:

<section id="content" class="site-content blog-page">

    <div class="container"> 

        <?php if ( is_home() || ( is_front_page() && is_home() ) ) : ?>                 

            <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

                <?php get_template_part( 'template-parts/post/content', get_post_format() ); ?>
                <?php get_template_part( 'template-parts/navigation/navigation', 'pagination' ); ?>             

            <?php endwhile; ?>            

            <?php else : ?>
                <p><?php _e( 'Sorry, no postes matched your criteria' ); ?></p> 
            <?php endif; ?>        

        <?php endif; ?>

    </div><!-- Ends .container -->       

<?php get_footer(); ?>

Url: http://magneton.carbonium.no/

EDIT Changed the index.php code to reflect @rudtek example. Template parts are still not loading.

  • Is there a content.php file in that location? – Milo Apr 28 '17 at 18:47
  • @Milo, Yes i have checked and double checked file names and paths. This is very confusing i have used template parts all over this theme but for some reason this is not working. I get no errors, the template-part is simply not adding the additional code. – Frederick M. Rogers Apr 28 '17 at 18:57
  • Are you sure that it passes the if condition you have get_template_part inside of? – Milo Apr 28 '17 at 19:01
  • @Milo, tested that as well magneton.carbonium.no – Frederick M. Rogers Apr 28 '17 at 19:08
  • maybe I'm not seeing the whole code here so something is missing, but by what I see, you're in a loop asking if a post is home or front page. This posts aren't home page. That pull should be out of the loop? – rudtek Apr 28 '17 at 19:10
1

Thinking about my comment...wouldn't it be simplier to say:

<section id="content" class="site-content blog-page">

<div class="container">              
<?php if ( is_home() || ( is_front_page() && is_home() ) ) : ?>  
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <h2> There are posts but template parts aren't found </h2>         
            <?php get_template_part( 'template-parts/post/content', get_post_format() ); ?>                 
            <?php get_template_part( 'template-parts/navigation/navigation', 'pagination' ); ?>  


    <?php endwhile; ?>

    <?php else : ?>
        <h2> There are no posts.</h2> 
    <?php endif; ?> 
<?php else : ?>
    <h2> THIS IS not home or front </h2>   
<?php endif; ?> 
</div><!-- Ends .container -->       

try this, so now we know if it's not home or if it's no posts or if it can't find your templates..

  • This may very well be a better structure and i have changed my code to match this example. However the template parts are still not loading their respective code blocks. – Frederick M. Rogers Apr 28 '17 at 19:22
  • okay now run the code. it should output an h2 with where there is a problem. – rudtek Apr 28 '17 at 19:29
  • Added the code it repeats as expected, yet no templat-parts. I am baffled. – Frederick M. Rogers Apr 28 '17 at 19:34
  • what error did you get back? 1 of the 3 should have showed – rudtek Apr 28 '17 at 19:40
  • Oh sorry i did not the see the other, i copied the entire code now and the results are three copies of "There are posts but template parts aren't found" – Frederick M. Rogers Apr 28 '17 at 19:47

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.