2015-10-24 40 views
1

我想这个问题一定是在这里问过的,但是我找不到这个话题。检查特定HTML标记的WordPress的帖子。其他的显示帖子缩略图

我想在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字符串增加了一个NextGen的画廊:

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

所以基本上我想代码如下所示:当这个HTML IMG字符串出现在内容字段中时,显示发布缩略图(精选图像)的INSTEAD ..如果该HTML IMG字符串不在内容字段中,则显示发布缩略图(精选图片)。

+0

'IMG [.ngg_displayed_gallery]'是一个选择器。 ''是一个html标记。 – Kivylius

+0

含义?我应该像这样纠正它? if(strpos($ post-> post_content,('')=== false)){?? – Donorbrain

+0

你需要描述你想要更准确地做什么,所以我们可以帮助你。 – Kivylius

回答

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"; 
} 

Q1。为什么这是不好的做法?

我们可以发现画廊/图像可靠性的id,因为每个<img class='ngg_displayed_gallery'..>是在srcid不同并且可能class

+0

非常感谢您的帮助,它的工作! – Donorbrain

相关问题