首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在分类页面上显示特写文章?

如何在分类页面上显示特写文章?
EN

WordPress Development用户
提问于 2019-11-14 10:28:52
回答 1查看 204关注 0票数 0

我已经在文章页面上创建了一个功能帖子复选框。现在我要做的是,如果任何用户选中了该复选框,那么我必须在类别页面上显示该帖子。我试过在下面的代码,但它没有显示。

Code是在post上添加特性帖子

代码语言:javascript
复制
// 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 "".__('Feature this post?', 'foobar')."";
    echo "";
}

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

现在我在类别页面上显示

代码语言:javascript
复制
 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;

    }
}
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2019-11-14 12:19:29

问题不在类别页面中显示功能帖子的代码中。问题是在post中添加功能post元。

首先,默认情况下是回送( checked() ),因此必须传递false属性以防止回显。

代码语言:javascript
复制
function add_featured_meta_box($post){
    $featured = get_post_meta($post->ID, '_featured-post', true);
    echo "".__('Feature this post?', 'foobar')."";
    echo "";
}

其次,从esc_attr()中删除update_post_meta,您必须保存空值,因为$_POST中没有设置未选中的复选框,因此您必须清空它们的元字段。

代码语言:javascript
复制
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');
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/352549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档