0
votes

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

2
Because there is nothing in your content-about.php telling it to get the content from that page. If you look in your file, you are using the_content(). Since that is in the loop, it will get the content from whatever page you are on. You can use get_the_content() and pass the about page id to that function. Just because the file is named content-about doesn't mean that it gets content from that pate.disinfor
thanks can you show me an example, I am new to this part of wordpress.Claudio Martinez
Sure. I'll add an answer.disinfor

2 Answers

1
votes

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
1
votes

You don't even need the get_template_part for this if you don't want.

You can do this:

<div id="slideOut"> 
    <div class="slideout-content">
        <?php echo get_the_content('','','your-about-page-id-here'); ?>
    </div><!-- .slideout-content -->
</div>  

get_template_part() is a way to include files with markup that may rely on the loop, but keep things separated. You don't necessarily need it here.

You can also use: echo get_post_field('post_content', your-post-id-here );