0

Not sure what I'm doing wrong here. I am a beginner coder, but I've tried a lot of things already:

I'm trying to print the_content from the post_parent of an image. Here's what I'm using right now. I am able to post the title, but not the content (all I can succeed in printing content-wise is the image's content (which is actually the image's 'description' which is not what I'm looking for.

<?php echo wp_get_attachment_image( $post->ID, 'post-image' ); ?>

<h4 class="attach-title"><?php the_title(); ?></h4>

<p>
    <a
        href="<?php echo get_permalink( $post->post_parent ); ?>"
        title="<?php esc_attr( printf( __( 'Return to %s', APP_TD ), get_the_title( $post->post_parent ) ) ); ?>"
        rel="gallery">
        <?php
            printf(
                '<span class="meta-nav">' . __( '&larr; Return to %s', APP_TD ) . '</span>',
                get_the_title( $post->post_parent )
            );
        ?>
    </a>
</p>

<p>
    <a
        href="<?php echo get_permalink( $post->post_parent ); ?>"
        the_content="<?php esc_attr( printf( __( 'Return to %s', APP_TD ), get_the_content( $post->post_parent ) ) ); ?>"
        rel="gallery">
        <?php
            printf(
                '<span class="meta-nav">' . __( '&larr; Return to %s', APP_TD ) . '</span>',
                get_the_content( $post->post_parent )
            );
        ?>
    </a>
</p>

The first 2 pieces of code work correctly, but the last paragraph is where I want to pull the content. But this code just prints the description of the image itself rather than the post_parent's content.

2 Answers 2

0

As stated by the other answer, you can not pass an ID to get_the_content() function. You could fetch the post using get_post(), but there's also another handy function that you can use to fetch the content:

The get_post_field( $field, $post_id, $context ) function is what you're looking for:

$content = apply_filters( 'the_content', get_post_field( 'post_content', $post->ID ) );

The the_content filter also makes sure that some features like shortcodes or auto-paragraphs will work as intended.

Simply replace your second <a>...</a> with this:

<?php echo apply_filters( 'the_content', get_post_field( 'post_content', $post->post_parent ) ); ?>
5
  • As you can see in your reformatted code, the the_content is not a valid attribute for the <a> element. But the usage of get_post_field() is the same as get_content(). You put an echo behind it, and it simply echoes the content.
    – Johansson
    Sep 15, 2018 at 23:28
  • I think I'm not really understanding what are probably clear instructions due to my noobness. I just get the image description again when I use <?php echo $content = apply_filters( 'the_content', get_post_field( 'post_content', $post->ID ) ); ?> Sep 15, 2018 at 23:37
  • Thanks again. Any idea why the content it prints is not responsive to css-ing? It prints the paragraphs and the gallery (again, which I have to get rid of), but I can't add internal css to display:none the gallery or other items inside the content, or add css to the paragraphs or the gallery thumbnail sizes Sep 16, 2018 at 3:55
  • Any element can be affected by CSS, you just have to find the proper selector. Also, you should not wrap this function inside a <p> element, because the output is already wrapped in <p>, and a paragraph element can't be a child of another paragraph.
    – Johansson
    Sep 16, 2018 at 3:57
  • My bad this time. It was a typo, which no code can overcome Sep 16, 2018 at 4:00
0

get_the_content() is a function to be used in the loop and doesn't take a post id as parameter: https://codex.wordpress.org/Function_Reference/get_the_content

You could try get_post() to get the parent post object and then access it's content.

1
  • Why was this downvoted?
    – Hans
    Sep 16, 2018 at 20:06

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.