我正在尝试创建许多WordPres帖子和页面。我将帖子和页面包含在各种类别中。在每个类别中,我都添加了帖子和页面。在这种情况下,我需要在特定类别下显示帖子和页面。我想在类别下对帖子和页面进行升序或降序排序。我需要PHP编码这个目的。请给我帮助。我已经用下面的代码创建了category.php。
<div class="cate-top ">
<h1 class="cat-page-title"><?php printf( __( ' Your are Browsing: %s', 'twentythirteen' ), single_cat_title( '', false ) ); ?></h1>
<?php if ( category_description() ) : // Show an optional category description ?>
<div class="archive-meta"><?php echo category_description(); ?></div>
<?php endif; ?>
<?php while(have_posts()): the_post();?>
</div>
<div class="category-page">
<div class="cate-inn ">
<h2><a href="<?php the_permalink();?>"> <?php the_title();?></a></h2>
<div class="cat-image fix">
<a href="<?php the_permalink();?>"> <?php the_post_thumbnail();?></a>
</div>
<div class="cat-read-more fix">
<?php read_more(0);?><a href="<?php the_permalink();?>">Read More</a>
</div>
</div>
<?php endwhile;?>
答案 0 :(得分:1)
您可以使用get_posts或WP_Query获取页面并发布您想要的类别,例如
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
您只需在args中更改类别名称,
即可如果您使用自定义分类而不是默认类别,则可以使用以下代码
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
endwhile;
}
}