WordPress.org

Ready to get started?Download WordPress

Forums

removing wp-caption div (7 posts)

  1. mamulea
    Member
    Posted 4 years ago #

    Hi there. I would like to know how can I eliminate the wp-caption div from some posts, according to an "if" statement. For example, I would like to be able to display the post pictures only for the articles posted in the last week... I used a function that filters the images for older posts, but it only clears the picture and leaves the wp-caption div unaltered. Is there a way to remove it too? 10x.

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Assuming the element is empty, you could use CSS to hide it from the modern browsers (CSS selector won't work for some older browsers).

    div.wp-caption:empty { display:none }

    ..for the sake of providing a quick fix... :)

  3. mamulea
    Member
    Posted 3 years ago #

    Ok, thank you for your quick answer, but how can I do this? The wp-caption div is something inside the wordpress files, automatically integrated inside the_content. How can I insert div.wp-caption:empty after a certain If statement?

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    The code would go inside your theme's stylesheet assuming the suggestion works. You of course couldn't do a conditional statement in the stylesheet, but if the element you were referring to is indeed empty, the above CSS should work (in theory) without the need for any PHP code.

  5. mamulea
    Member
    Posted 3 years ago #

    My code is very simple:
    <div class="entry"><?php the_content(); ?> </div>
    As I said, the wp-caption div is part of the_content function, but that div still has to be shown for some posts. I only want to eliminate the picture and the caption div around it for certain posts, so I cannot make the div empty as a general rule. Is there a way to do this?

  6. mamulea
    Member
    Posted 3 years ago #

    still no idea?

  7. s_ha_dum
    Member
    Posted 3 years ago #

    Open a page that has a WP generated caption on it and view-source in your browser. WordPress adds the divs and classes mentioned. You don't have to do that.

    If you have some kind of logic to determine which posts have the caption and which don't then you can use a the_content filter to strip the caption, but you have to be able to identify the posts by some mechanism-- by category, by tag, something. You could probably also use some targeted css to simply hide the caption.

Topic Closed

This topic has been closed to new replies.

About this Topic