我在帖子页面上创建了一个功能帖子复选框。现在我要做的是,如果任何用户选中了该复选框,那么我必须在分类页面上显示该帖子。我尝试了下面的代码,但它没有显示。
Code is to add feature post on post
// add feature post on post page
function register_post_assets(){
add_meta_box(\'featured-post\', __(\'Featured Post\'), \'add_featured_meta_box\', \'post\', \'advanced\', \'high\');
}
add_action(\'admin_init\', \'register_post_assets\', 1);
function add_featured_meta_box($post){
$featured = get_post_meta($post->ID, \'_featured-post\', true);
echo "<label for=\'_featured-post\'>".__(\'Feature this post?\', \'foobar\')."</label>";
echo "<input type=\'checkbox\' name=\'_featured-post\' id=\'featured-post\' value=\'1\' ".checked(1, $featured)." />";
}
function save_featured_meta($post_id){
// Do validation here for post_type, nonces, autosave, etc...
if (isset($_REQUEST[\'_featured-post\']))
update_post_meta(esc_attr($post_id, \'_featured-post\', esc_attr($_REQUEST[\'_featured-post\'])));
// I like using _ before my custom fields, so they are only editable within my form rather than the normal custom fields UI
}
add_action(\'save_post\', \'save_featured_meta\');
// end feature post with check box tag here
现在我在分类页面上显示 if(is_category()){
//check if category is a subcategory
$this_category = get_queried_object();
if( 0 != $this_category->parent ){ //if subcategory then display feature post
//displaying feature post here
$args = array(
\'posts_per_page\' => 5,
\'meta_key\' => \'_featured-post\',
\'meta_value\' => 1
);
$featured = new WP_Query($args);
if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post();
the_title();
the_content();
endwhile; else:
endif;
}
}