admin管理员组文章数量:1406312
I have created a feature post check box on the post page. Now what I am doing is if any user-selected that checkbox then I have to show that post on the category page. I tried below code but it's not displaying.
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
Now I an displaying on the category page
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;
}
}
I have created a feature post check box on the post page. Now what I am doing is if any user-selected that checkbox then I have to show that post on the category page. I tried below code but it's not displaying.
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
Now I an displaying on the category page
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;
}
}
Share
Improve this question
edited Nov 14, 2019 at 11:20
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Nov 14, 2019 at 10:28
Naren VermaNaren Verma
2491 gold badge6 silver badges19 bronze badges
1 Answer
Reset to default 1The problem is not in code to Displaying Feature post in category page. the problem is to add feature post meta into post.
First, checked()
by default echo, so you have to pass false
attribute to prevent echo.
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,false)." />";
}
second, remove esc_attr()
from update_post_meta
, and you have to save empty value beacuse unchecked checkboxes are not set in the $_POST, so you'd have to empty their meta field.
function save_featured_meta($post_id){
// Do validation here for post_type, nonces, autosave, etc...
$featured_post = ( isset( $_POST['_featured-post'] ) ) ? $_POST['_featured-post'] : "";
update_post_meta($post_id, '_featured-post', $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');
本文标签: pluginsHow to display the featured post on the category page
版权声明:本文标题:plugins - How to display the featured post on the category page? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745004832a2637197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论