【问题标题】:Check if Previous post item has a certain Wordpress post format检查上一个帖子项目是否具有特定的 Wordpress 帖子格式
【发布时间】:2016-05-10 19:29:56
【问题描述】:

我在我的主题中使用 WP 帖子格式,并且我想在我的单帖子页面中回显以前帖子的部分内容。呼应它可以正常工作,但是我只希望显示来自特定帖子格式的以前的帖子。我尝试使用带有has_post_format() 函数的if 语句,但它不起作用:

php:

$prev_post = get_previous_post();

$prev_post_id = $prev_post->ID;

$prev_post_title = $prev_post->post_title;

function prev_post_item($object) {  
    if (!has_post_format('gallery', $prev_post_id)) {
        echo $object;
    }
}

html:

<h2><?php prev_post_item($prev_post_title); ?></h2>

【问题讨论】:

    标签: php wordpress get echo post-format


    【解决方案1】:

    两种选择

    <h2>
        <?php
            $prev_post = get_previous_post();
            $prev_post_id = $prev_post->ID;
            if ( ! has_post_format('gallery', $prev_post_id ) ) {
                echo 'Is not a gallery'
            }
         ?>
    </h2>
    

    或在函数中

    function your_prev_post_item() {  
        $prev_post = get_previous_post();
        $prev_post_id = $prev_post->ID;
        if (!has_post_format('gallery', $prev_post_id)) {
            echo 'Is not a gallery'
        }
    }
    

    然后是 HTML

    <h2><?php your_prev_post_item(); ?></h2>
    

    【讨论】:

    • 啊,我需要函数内部的变量吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多