【问题标题】:How to get any first heading tags in post and display the result?如何在帖子中获取任何第一个标题标签并显示结果?
【发布时间】:2018-08-25 22:16:42
【问题描述】:

我想将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>

【问题讨论】:

    标签: php html regex wordpress


    【解决方案1】:

    我假设标题是 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>
    

    【讨论】:

    • 我通常使用H3并居中对齐。
    • 我自己修好了,谢谢。我仍然会批准您的回答,因为我只更改 preg_match。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 2018-01-18
    • 1970-01-01
    • 2014-12-22
    • 2021-11-07
    相关资源
    最近更新 更多