我在我的主题中使用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>
答案 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>