【问题标题】:Check wordpress post for specific HTML tag . Else show post thumbnail检查 wordpress 帖子以获取特定的 HTML 标记。否则显示帖子缩略图
【发布时间】:2023-03-30 20:14:01
【问题描述】:

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

我试图在 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 帖子并在内容编辑器中添加 Nextgen Gallery 时,会出现以下 HTML 字符串:

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

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

【问题讨论】:

  • img[.ngg_displayed_gallery] 是一个选择器。 &lt;img class='...'&gt; 是一个 html 标签。
  • 意义?我应该这样改正吗? if (strpos($post->post_content, ('') === false)){ ??
  • 您需要更准确地描述您想要做什么,以便我们为您提供帮助。
  • 我添加了更详细的描述
  • 请...有人可以提供答案吗?

标签: php wordpress if-statement thumbnails nextgen-gallery


【解决方案1】:

根据我从问题中了解到的情况,您需要在字符串的内容中找到一个 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"; 
}

第一季度。为什么这是不好的做法?

我们可以找到gallery/image reliability的id,因为&lt;img class='ngg_displayed_gallery'..&gt;srcidclass中可能是不同的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 2015-01-25
    相关资源
    最近更新 更多