检查上一篇文章是否具有某种Wordpress帖子格式

时间:2016-05-10 19:29:56

标签: php wordpress get echo post-format

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

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>

1 个答案:

答案 0 :(得分:0)

两个选项

<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>