检查 wordpress post 的特定 HTML 标签。否则显示 post 缩略图

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

我想这个问题之前一定有人在这里问过,但我找不到主题,所以请耐心等待。

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

所有这些都需要在 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 post 并在内容编辑器中添加 Nextgen Gallery 时,会出现这个 HTML 字符串:

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

所以基本上我想编写这样的代码:当这个 HTML IMG 字符串出现在内容字段中时,显示 post 缩略图(特色图片)的 INSTEAD.. 如果那个 HTML IMG 字符串不在内容字段中,显示 post 缩略图(特色图片)。

据我从问题中了解到,你需要在字符串的内容中找到一个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/imagereliability的id,因为每个<img class='ngg_displayed_gallery'..>srcidid中都是不同的可能 class