从帖子中定位和发布块引用

时间:2015-05-11 23:46:27

标签: php jquery html css wordpress

我的Wordpress网站的index.php有一个div,用于发布帖子的标题,类别和精选图片。

index.php HTML

<div>
    <h1><?php the_title(); ?></h1>
    <h3><?php the_category(' '); ?></h3>
</div>

我有一个single.php,它会引入并放置帖子的内容

single.php HTML

<div>
    <?php the_content(); ?>
</div>  

我想将帖子的一部分内容带到我的index.php中(例如&#39;块引用&#39;标记中的任何内容)

EX。

 <div>
    <h1><?php the_title(); ?></h1>

    ... content from block quote    

    <h3><?php the_category(' '); ?></h3>
</div>

2 个答案:

答案 0 :(得分:0)

您可以使用get_the_content()检索内容,然后检查是否有块引用,如果是,请回复它:

// get the content
$content = get_the_content();
// check and retrieve blockquote
if(preg_match('~<blockquote>([\s\S]+?)</blockquote>~', $content, $matches))
    // output blockquote
    echo $matches[1];

答案 1 :(得分:0)

您可以使用DOM模块解析HTML标记中的数据。 This是这样做的绝佳指南。

你也可以使用REGEX(有人已经显示了这个,所以我删除了我要显示的链接)

另一种选择是用爆炸等方式自己解析它。就像这样:

//Get the content
$content = the_content();

//Explode to separate the first tags
$blockquotes = explode("<blockquote>", $content);

//Data array to use
$data = array();

//For each of these
foreach($blockquotes as $x){
    //Find the location of </blockquote>
    $end_loc = strpos($x, "</blockquote>");

    //Remove everything after by only taking everything before it
    $temp = substr($x, 0, $end_loc);

    //Add it to the array
    $data[] = $temp;
}

//Data now contains all of the data, do as you please with it
var_dump($data);