我目前正在智能杂志主题工作,在主页newsticker所有最新的新闻都是默认发布的。但是我想在新闻贴纸中只显示选定的帖子。为此,我安装了插件"Meta box"。并编写了一个自定义元字段
add_filter( 'rwmb_meta_boxes', 'breaking_news_radio_demo' );
function breaking_news_radio_demo( $meta_boxes )
{
$prefix = 'rw_';
$meta_boxes[] = array(
'title' => __( 'Breaking news', '$prefix' ),
'fields' => array(
array(
'name' => __( 'Show', 'rw' ),
'id' => 'radio',
'pages' => array('post-new'),
'type' => 'radio',
// Array of 'value' => 'Label' pairs for radio options.
// Note: the 'value' is stored in meta field, not the 'Label'
'options' => array(
'YES' => __( 'Yes', '$prefix' ),
'NO' => __( 'No', '$prefix' ),
),
),
)
);
return $meta_boxes;
}
在'Add new post'中显示良好的元框。但是使用单选按钮,我想控制哪些帖子显示在新闻列表中。使用以下代码
显示主题中的新闻提示器<?php if (!Bunyad::options()->disable_topbar_ticker): ?>
<div class="trending-ticker">
<span class="heading"><?php echo Bunyad::options()->topbar_ticker_text; // filtered html allowed for admins ?></span>
<ul>
<?php $query = new WP_Query(apply_filters('bunyad_ticker_query_args', array('orderby' => 'date', 'order' => 'desc', 'posts_per_page' => 8))); ?>
<?php while($query->have_posts()): $query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</ul>
</div>
<?php endif; ?>
必须在循环中添加比较rwmb_meta( 'radio' )
的条件有关rwmb_meta的详细信息,请查看文档。
可以是这样的:
<?php while($query->have_posts()): $query->the_post(); ?>
<?php if( rwmb_meta( 'radio' ) == 'Yes' ): ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endif; ?>
<?php endwhile; ?>
另一种可能更好的方法是通过添加查找的键和元值来更改WP_Query。
<?php $query = new WP_Query(apply_filters('bunyad_ticker_query_args', array('meta_key' => 'radio', 'meta_value' => 'Yes','orderby' => 'date', 'order' => 'desc', 'posts_per_page' => 8))); ?>