0

I have the following, which is successfully pulling the title, thumbnail, and custom field data (YouTube link) from a referenced post, but the content displayed is that of the parent:

function woo_videos_tab_content() {
$posts = get_field('videos');
if( $posts ): ?>
    <div class="row">
    <?php foreach( $posts as $p ): ?>
    <div class="col-sm-4">
        <?php
        if ( has_post_thumbnail()) : ?>
        <a data-remodal-target="modal-<?php echo $p->ID; ?>" class="video-link" title="<?php echo get_the_title( $p->ID ); ?>" >
            <?php echo get_the_post_thumbnail( $p->ID, 'video-thumb' ); ?>
            <i class="fa fa-youtube-play fa-4x"></i>
        </a>
        <h4><?php echo get_the_title( $p->ID ); ?></h4>

        // Issue with the line below
        <?php echo get_the_content( $p->ID ); ?>

        <div class="remodal" data-remodal-id="modal-<?php echo $p->ID; ?>">
            <button data-remodal-action="close" class="remodal-close"></button>
            <h2><?php echo get_the_title( $p->ID ); ?></h2>
            <iframe src="<?php the_field('youtube_link', $p->ID); ?>?enablejsapi=1&version=3" allowfullscreen="" width="560" height="315" frameborder="0" allowscriptaccess="always"></iframe>
        </div>
        <?php endif; ?>
    </div>
  <?php endforeach; ?>
</div>
<?php endif;
}

Should I be pulling the content via another method? I had assumed what worked for title / thumbnail / custom field would work for the content.

2

Function get_the_content doesn't take post_ID as param. It has 2 params:

  • $more_link_text - (string) (optional) Content for when there is more text.
  • $stripteaser - (boolean) (optional) Strip teaser content before the more text.

So you can't use it as in your code get_the_content( $p->ID ); - this will get content of current post and use $p->ID as more link text.

So how to get the content of any post based on post ID?

You can use get_post_field function:

echo apply_filters( 'the_content', get_post_field( 'post_content', $p->ID ) );
1

You are using the methods with incorrect parameter.

First of all, get_the_content function doesn't take id of the post as parameter. here is the ref

Second, in the loop, you are using has_post_thumbnail without parameter. In that case, you must use the_post() method, because it actually change the global post variable in the loop.

  • Ok, I see what you mean about get_the_content. Regarding your second point, I'm not following. Is it relevant to the question (aside from being accurate)? I have added more context to the code, as I thought I should include the whole function for clarity. – user2265915 Jun 7 '18 at 13:50
  • 1
    @user2265915 yes, get_the_content uses global post variable as well, so that doesn't need post id. So if you use get_the_content without the_post(), it won't work properly. – idpokute Jun 7 '18 at 14:27

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.