如何使用帖子编辑屏幕中的复选框设置特色帖子?

时间:2011-09-17 作者:Bialy

我想通过选中编辑屏幕中的复选框来选择一篇特色文章,并能够从特定类别中检索这些特色文章?

简而言之,我在寻找:

设置特色帖子

循环抛出特定类别中的特色帖子(并非所有帖子)

有什么帮助吗?并提前感谢:)

2 个回复
SO网友:Brian Fegter

您可以按照以下步骤执行此操作:

将自定义元框添加到帖子中创建一个函数,使用save\\u post操作保存元数据,将“meta\\u key”查询参数添加到您正在使用的任何查询中将其放在主题功能中。php文件:

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\');
然后在模板文件中,将以下内容用于主循环:

    $args = array(
        \'meta_key\' => \'_featured-post\', // include underscore prefix in key name
        \'meta_value\' => 1
    );
    // The number of posts displayed would be determined under Settings->Reading
    query_posts($args);

    if(have_posts()): while(have_posts()): the_post();
        // Do your bidding here

    endwhile; else:

    endif;
仅用于示例目的:对于自定义循环(如果在一个页面上运行多个循环),可以使用以下选项:

    $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;

SO网友:user164332

我试着运行这段代码,这对我在主页上显示我的特色帖子,以供案例研究自定义帖子类型有很大帮助。非常感谢。

         <?php $args = array( 
                \'post_type\' => \'case_studies\',
                \'posts_per_page\' => 1,
                \'meta_key\' => \'featured_post\',
                \'meta_value\' => 1
            );

            $featured = new WP_Query($args);

            if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post(); ?>
                <div class="c-image"><?php the_post_thumbnail(\'large\'); ?></div>
                <div class="cstext">
                     <article>
                        <h2><?php the_title(); ?></h2>  
                        <span class="sub-title"> USAF SOCONS </span>
                        <?php the_content(); ?>
                        <a href="<?php echo get_permalink(); ?>" class="readmore">Learn More</a>
                    </article> 

                    <a href="/case-studies" class="btn-primary">View all Case Studies</a>

                </div> 

            <?php 
                endwhile; 
                    else: \'No Content Added\';
            endif; ?>
    </div>
</div>  

结束

相关推荐

分配给特定自定义分类术语的GET_POSTS,而不是该术语的子项

假设我有以下分类术语:Term 1 Term 1.1 Term 1.2 Term 2 Term 2.1 我如何才能只获得分配给第1学期的职位,而不包括分配给第1.1学期或第1.2学期的职位?例如:$pages = get_posts(array( \'post_type\' => \'page\', \'numberposts\' => -1, \'tax_query\' => array(