Php 检查wordpress post中的特定HTML标记。否则显示帖子缩略图

Php 检查wordpress post中的特定HTML标记。否则显示帖子缩略图,php,wordpress,if-statement,thumbnails,nextgen-gallery,Php,Wordpress,If Statement,Thumbnails,Nextgen Gallery,我猜这个问题以前一定在这里被问过,但我找不到这个话题,所以请容忍我 我试图在PHP中实现一个if/else语句,以查找具有特定类名的特定HTML标记。如果找到这个HTML标记,我希望它显示HTML标记,否则如果找不到,我希望它显示帖子缩略图 所有这些都需要在wordpress模板(single.php)中完成 所以我有这个: if (strpos($post->post_content, ('img[.ngg_displayed_gallery]') === false)){

我猜这个问题以前一定在这里被问过,但我找不到这个话题,所以请容忍我

我试图在PHP中实现一个if/else语句,以查找具有特定类名的特定HTML标记。如果找到这个HTML标记,我希望它显示HTML标记,否则如果找不到,我希望它显示帖子缩略图

所有这些都需要在wordpress模板(single.php)中完成

所以我有这个:

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"]');
    }
我很确定我把这个密码搞糟了,所以请帮帮我。我对PHP非常不熟悉;)

谢谢你的帮助! 编辑:

好的。。我会尽量说得更清楚: 当有人编辑wordpress帖子并在内容编辑器中添加下一个图库时,会出现以下HTML字符串:

<img class="ngg_displayed_gallery mceItem" src=“../nextgen-attach_to_post/preview/id--1443" alt="" data-mce-placeholder="1" /> 


因此,基本上我想这样编码:当这个HTML IMG字符串出现在内容字段中时,显示它,而不是帖子缩略图(特征图像)。。如果HTML IMG字符串不在内容字段中,请显示文章缩略图(特色图像)。

根据我从问题中了解的情况,您需要在字符串的内容中找到HTML标记,您正在使用
strpos()
。我想让你知道这是一个坏习惯,应该使用替代方法,但是,这里有一个例子

// 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。为什么会有这种坏习惯?


我们可以找到gallery/image的id可靠性,因为每个
id
和可能的

img[.ngg\u显示的\u gallery]
都是一个选择器<代码>
是一个html标记。什么意思?我应该像这样把它放下?如果(strpos($post->post_content,('')==false)){您需要更准确地描述您试图做的事情,以便我们可以帮助您。我添加了更详细的描述请…有人能提供答案吗?