[英]How to loop through posts content and show only a specifc id or class
我知道我可以通过CSS或javaScript来做到这一点,但我想知道Wordpress是否具有本机的实现方式。
我有以下代码:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
但可以说,我只想显示所有这些帖子中包含的特定ID或类。 例如: #heading
或.heading
我怎样才能做到这一点?
首先,仅使用PHP获得包含id =“ heading”的帖子:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
$post_content = get_the_content();
$find_this = 'id="heading"';
$find_this_single_quote = "id='heading'";
if (strpos($post_content, $find_this) !== false || strpos($post_content, $find_this_single_quote ) !== false) {
the_content();
}
然后,使用jQuery过滤结果以仅获取#heading的内容:
var content = $('#heading').html();
if (content != '') {
$('#real-content').html(content);
}
小提琴: https : //jsfiddle.net/hyxhn7mg/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.