首先,您需要一种快速的方式来切换帖子是否具有特色。我写了一个插件,可以做到这一点:
http://wordpress.org/extend/plugins/featured-item-metabox/
但我觉得自己是个十足的傻瓜,因为我刚刚发现这个插件已经在这么做了!
http://wordpress.org/extend/plugins/featured-post呃。无论如何,您希望在主页上为它们添加功能的方式将影响您查询它们的方式。
从我的插件的使用文档中,这将得到所有特色“公文包”的列表。您可以根据自己的喜好调整UL列表。
// Get any existing copy of our transient data
if ( false === ( $featured_portfolios = get_transient( \'featured_portfolios\' ) ) ) {
// It wasn\'t there, so regenerate the data and save the transient
// params for our query
$args = array(
\'post_type\' => \'portfolio\'
\'posts_per_page\' => 5,
\'meta_key\' => \'_featured\',
\'meta_value\' => \'yes\'
);
// The Query
$featured_portfolios = new WP_Query( $args );
// store the transient
set_transient( \'featured_portfolios\', $featured_portfolios );
}
// Use the data like you would have normally...
// The Loop
if ( $featured_portfolios ) :
echo \'<ul class="featured">\';
while ( $featured_portfolios->have_posts() ) :
$featured_portfolios->the_post();
echo \'<li>\' . get_the_title() . \'</li>\';
endwhile;
echo \'</ul>\';
else :
echo \'No featured portfolios found.\';
endif;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren\'t stomping on the
* original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
因为我使用瞬态来存储查询,所以每当您更新公文包帖子时,我们都需要更新瞬态。
// Create a function to delete our transient when a portfolio post is saved
function save_post_delete_featured_transient( $post_id ) {
if ( \'portfolio\' == get_post_type( $post_id ) )
delete_transient( \'featured_portfolios\' );
}
// Add the function to the save_post hook so it runs when posts are saved
add_action( \'save_post\', \'save_post_delete_featured_transient\' );