Check wordpress post for specific HTML tag . Else show post thumbnail

I guess this question must have been asked here before, but i couldn’t find the topic so bear with me.

I’m trying to achieve an if/else statement in PHP to find a specific HTML tag with a certain classname. If this HTML tag is found i want it to display the HTML tag otherwise if not found, i want it to show the post thumbnail.

Read More

All this needs to be done in a wordpress template (single.php).

So i have this:

if (strpos($post->post_content, ('img[.ngg_displayed_gallery]') === false)){
        $gallery = false;
          the_post_thumbnail('full');
          echo do_shortcode('[shareaholic app="share_buttons" id="390155"]');
        }
else{
    echo ('img[.ngg_displayed_gallery]');
    echo do_shortcode('[shareaholic app="share_buttons" id="390155"]');
    }

I’m pretty sure i screwed this code up so please help me. I’m very bad with PHP ๐Ÿ˜‰

Thanks for the help!
Edit:

Ok.. i’ll try to be more clear:
When someone edits a wordpress post and adds a Nextgen Gallery in the content editor there is this HTML string:

<img class="ngg_displayed_gallery mceItem" src=รขย€ยœ../nextgen-attach_to_post/preview/id--1443" alt="" data-mce-placeholder="1" /> 

So basically i want to code something like this: When this HTML IMG string appears in the content field, show that INSTEAD of the post thumbnail (featured image).. If that HTML IMG string is NOT in the content field, show the post thumbnail (featured image).

Related posts

1 comment

  1. From what I understand from the question, you need to find a html tag in the contents of string, you are using strpos(). I want to let you know this is bad practice and alternative should be used instead, however, here is an example.

    // First we must get the HTML formated content.
    $content = apply_filters ("the_content", $post->post_content);
    
    // We then must indentifie the needle in the heystack (content)
    $needle = 'ngg_displayed_gallery';
    
    if (strpos($content, $needle) === false)){
        $gallery = false;
        // No, there's no gallery present
        echo "NO, gallery not found";
    } else {
        // Yes there is an gallery present
        echo "YES, gallery found"; 
    }
    

    Q1. Why is this bad practice?

    We can find the id of gallery/image reliability, as each <img class='ngg_displayed_gallery'..> is different in src, id and possible class

Comments are closed.