结合advanced custom fields,custom post type-ui这两款插件,wordpress可以扩展出很强大的功能。下面这段代码新建了一个文章类型,查询出这个文章类型下推荐分类中的推荐文章。
<?php $args=array( 'orderby' => 'name', 'order' => 'ASC', 'number' => '200', 'post_type' => 'pmacnews', 'taxonomy' => 'yourtaxonomy name', 'hide_empty' => '0', 'parent' => 0 ); $categories=get_categories($args); foreach($categories as $category) : ?> <?php $recommend = get_field('recommend', 'pmacnews_categories_'.$category->cat_ID); if ($recommend == 'Yes') :?> <?php echo $category->name ?> <?php global $wp_query; $search_args = array( 'post_type' => 'pmacnews', 'posts_per_page' => '6', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'yourtaxonomy name', 'field' => 'slug', 'terms' => array( $category->slug, ) ) ) , 'meta_query' => array( array( 'key' => 'recommend', 'value' => 'Yes', 'compare' => 'in' ) ) ); query_posts($search_args); ?> <?php while ( have_posts() ) : the_post(); ?> <?php the_title(); ?> <?php endwhile ?> <?php wp_reset_query(); ?> <?php endif; ?> <?php endforeach ?>