首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何遍历帖子内容并仅显示特定的id或类

如何遍历帖子内容并仅显示特定的id或类
EN

Stack Overflow用户
提问于 2017-01-14 20:47:34
回答 1查看 270关注 0票数 0

我知道我可以通过CSS或javaScript来做到这一点,但我想知道Wordpress是否有一种原生的方式来做到这一点。

我有这样的代码:

代码语言:javascript
运行
复制
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 

                        <?php the_content(); ?>

但是假设,我只想显示包含在所有这些帖子中的特定id或类。例如:#heading.heading

我该怎么做呢?

EN

Stack Overflow用户

回答已采纳

发布于 2017-01-14 21:27:49

首先,使用PHP只获取包含id=“标题”的帖子:

代码语言:javascript
运行
复制
<?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的内容:

代码语言:javascript
运行
复制
var content = $('#heading').html();

if (content != '') {
        $('#real-content').html(content);
}

小提琴:https://jsfiddle.net/hyxhn7mg/1/

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41650142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档