Skip to content
Advertisement

get_template_part – WordPress not working

I am trying to place the content of the about page inside a div on the header , the template part is located on folder template-parts/content-about.php

on the header the code is:

<div id="slideOut"> 
<div class="slideout-content">

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

        get_template_part( 'template-parts/content', 'about' );

    endwhile; // End of the loop.
    ?>
    
      <?php include 'content-about.php'; ?>
</div><!-- .slideout-content -->
</div>  

And the content-about.php looks like this:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-content">
    <?php
    the_content();

    wp_link_pages(
        array(
            'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'about' ),
            'after'  => '</div>',
        )
    );
    ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->

The issue is that is showing on the div the same content as the current page for example if I’m located on home page it shows only the content of the home page inside the div when it should always show the content of the about page. why is not working? Thanks

Advertisement

Answer

get_template_part() is working as expected. Your content-about.php is included normally.

However, it prints the content of the current post/page because you’re calling the_content().

You could replace the_content() with get_the_content(), passing the post id of your about page as the third parameter.

Note that:

An important difference from the_content() is that get_the_content() does not pass the content through the the_content filter. This means that get_the_content() will not auto-embed videos or expand shortcodes, among other things.

So, you might want to use apply_filters() like this:

<?php
echo apply_filters( 'the_content', get_the_content( null, false, $about_page_id ) );
// where $about_page_id is the post id of your about page
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement