如何在帖子中获取任何第一个标题标签并显示结果?

时间:2018-08-25 11:21:29

标签: php html regex wordpress

我想将the_title()更改为我在帖子中放置/书写的标题标签,我已经创建了带有表代码的列表循环帖子,并被卡在这里...有什么帮助吗?

格式标题:<h3 style="text-align: center;">Title Here</h3>

<div class="chapter_release">
<table><tr>
    <th>Chapter</th> 
</tr>
<?php
// Posts are found
if ( $posts->have_posts() ) {
    while ( $posts->have_posts() ) {
        $posts->the_post();
        global $post;
?>
<tr><td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td></tr>
<?php
    }
}
?></table></div>

1 个答案:

答案 0 :(得分:0)

我将假设标题为h2:

<div class="chapter_release">
<table><tr>
    <th>Chapter</th> 
</tr>
<?php
// Posts are found
if ( $posts->have_posts() ) {
    while ( $posts->have_posts() ) {
        $posts->the_post();
        global $post;
        if( preg_match( "'<h2>(.*?)</h2>'si", get_the_content(), $match ) == 1 ) {
            $new_title = $match[1];
        } else {
            $new_title = get_the_title();
        }
?>
<tr><td><a href="<?php the_permalink(); ?>"><?php echo $new_title; ?></a></td></tr>
<?php
    }
}
?></table></div>