• Resolved ordresser

    (@ordresser)


    There is an article on the WordPress blog, and it has a gallery, and the gallery image opens as an attachment using an attachment.php (actually an image.php in this particular case).

    The goal is to print the content (paragraphs) that exist on the blog onto the attachment page.

    I’ve managed to print the title from the original blog on the attachment page, but am stumped trying to print the content – everything I try (a LOT of things, including suggestions from others) just prints the image ‘description’.

    Any idea what code will reach back to the post_parent that has the gallery and grab its content and print it on the attachment?

    Coding level: beginner – not good at php.

Viewing 9 replies - 1 through 9 (of 9 total)
  • In your image.php template file you could:

    <?php echo apply_filters( 'the_content', get_post_field( 'post_content', $post->post_parent ) ); ?>

    The reason you’re getting the current post description is because the current post is the image. The above grabs the content of the attachment posts parent and runs it the the_content filter which will add the HTML and format it as it would a normal post.

    Thread Starter ordresser

    (@ordresser)

    You’re right. Thanks very much.

    Thread Starter ordresser

    (@ordresser)

    Is it possible to add something to this to also print the (linked) category of the original post?

    You can use wp_get_object_terms() passing in the $post->post_parent and whatever the taxonomy is ( ‘category’ for Post Categories ) which will return an array of WP_Term objects. You could loop through them and display them:

    $terms = wp_get_object_terms( $post->post_parent, 'category' );
    if( ! empty( $terms ) ) {
        foreach( $terms as $term ) {
            printf( '<a href="%1$s">%2$s</a>', get_term_link( $term ), $term->name );
        }
    }

    Something like that.

    Thread Starter ordresser

    (@ordresser)

    I’m seeing that

    <?php echo apply_filters( 'the_content', get_post_field( 'post_content', $post->post_parent ) ); ?>

    carries over the fact that there are paragraphs, and puts them inside <p>’s, but it doesn’t retain the styles for p’s or allow me to add css to do so.

    Thread Starter ordresser

    (@ordresser)

    Also, this is really helpful stuff. If you have email and PayPal, we could maybe do something where I could ask you this type of basic question, which occurs every once in a while when I’m working on a theme for my own projects, and maybe just give you like $20cad for each simple answer via PayPal? Not a great budget, but they’re mostly just small sites with no real revenue.

    carries over the fact that there are paragraphs, and puts them inside <p>’s, but it doesn’t retain the styles for p’s or allow me to add css to do so.

    It could be that the other pages CSS styles are derrived from a container class that isn’t brought over to the image.php script. For example, a post page may have a container of <div class="content"> or something where you imaage template may not have that. The easiest thing to do is use your in-browser inspector to tell the differences. Without seeing the two pages side-by-side it would be difficult to suggest a a solution.

    While I appreciate the compensation offer, I really do, I’m just here to help when I have time. There’s plenty of resources I could recommend to learn and get your questions answered:

    1. www.remarpro.com Forums ( you’re here! )
    2. WordPress Live Support Chat ( IRC )
    3. Stack Overflow ( Any code related questions )
    4. WordPress Stack Exchange ( WordPress related code questions )
    5. Local WordPress Meetups ( always support your local WordPress Community! )
    6. Theme Development Handbook

    Many of the questions you have already have answers which can be found by utilizing the above resources and as always the community is here to help where we can but you also must put forth effort in learning!

    Thread Starter ordresser

    (@ordresser)

    Thanks. Yes, I’d been using the inspector, and actually css-ing is the one thing I’m good at webcode-wise, but it just wouldn’t apply the styles nomatter what. I ended up removing some styles from the .css that were somehow overriding anything else I tried.

    OK, no probs. Just after getting headaches on these little code questions for days and then coming on here and someone helping and it working, you just feel like you want to at least offer something.

    Resolved.

    Moderator bcworkz

    (@bcworkz)

    Hi @ordresser

    Please do not offer to pay for assistance in these forums. There’s a slight chance someone who’s not in your best interest will take you up on the offer. While most members here only want the best for you, certainly Howdy is one of them, we cannot vouch for everyone here. Offering payment is against our guidelines. For your own safety, please ignore any offers of paid help that might arise from your kind offer to Howdy.

    Since this topic is resolved, in an abundance of caution, I’m closing this topic.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How can you print the content from a post_parent in an attachment?’ is closed to new replies.