[英]How to display the most recent post of each category of a custom post type in wordpress
This question seems to have been asked in some varieties, But mine is quite specific. 这个问题似乎在某些品种中已经问过了,但是我的却很具体。
I need to display the most recent post of all categories of a custom post type called "products". 我需要显示自定义帖子类型“产品”的所有类别的最新帖子。 I need to display the category instead of the title and link to that cat's archive.
我需要显示类别而不是标题,并链接到那只猫的档案。
The problem is, What I am getting instead is only one post of that general post type. 问题是,我得到的只是该普通职位类型的职位。 Any thoughts?
有什么想法吗?
Here is my starting point: 这是我的出发点:
<?php
$query = new WP_Query( array(
'posts_per_page' => 12,
'post_type' => 'products',
'post_status'=>'publish',
'orderby'=>'post_date',
'order' => 'DESC',
'cat' => $category->term_id,
'count'=>1,
));
if ( have_posts() ) : while ( $query->have_posts()) : $query->the_post(); ?>
<div class="col-sm-6 col-md-4">
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="pos-rel"> <?php the_post_thumbnail('product', array('class' => 'img-responsive center-block')); ?><h3><?php the_title(); ?></h3></a>
<?php else : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="featured-products pos-rel"><!--<img class="img-responsive center-block" src="<?php echo THEME_DIR; ?>/img/default-thumb.png" alt="<?php the_title(); ?>" />--><img class="img-responsive " data-src="holder.js/100%174/auto" alt="Generic placeholder image"><h3><?php the_title(); ?></h3></a>
<?php endif; ?>
</div><!-- col-sm-6 -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div><!-- end row -->
<ul class="pager">
<li class="previous"><?php previous_posts_link(); ?></li>
<li class="next"><?php next_posts_link(); ?></li>
</ul>
<?php else : ?>
<h1>Not Found</h1>
<p><a href="<?php echo home_url(); ?>" title="Home">Click Here to return to the Home Page</a></p>
<?php endif; ?>
The custom post type was created via a 3rd plug into Super CPT Plugin 自定义帖子类型是通过Super CPT插件中的第3个插件创建的
Here is the code: 这是代码:
add_action( 'after_setup_theme', 'setup_data_structures' ); function setup_data_structures() { if ( ! class_exists( 'Super_Custom_Post_Type' ) ) return;$products = new Super_Custom_Post_Type( 'products', 'Product', 'Products' );
# Test Icon. Should be a square grid. $products->set_icon( 'suitcase' ); # Taxonomy test, should be like categories $product_category = new Super_Custom_Taxonomy( 'product-category', 'Product Category', 'Product Categories', 'category' ); # Connect both of the above taxonomies with the post type connect_types_and_taxes( $products, array($product_category) ); # Add a meta box with every field type $products->add_meta_box( array( 'id' => 'product-fields', 'context' => 'normal', 'fields' => array( 'client-name' => array('column' => true ), 'product-description' => array( 'type' => 'wysiwyg' ) ) ) );
$products->add_to_columns( 'product-category' ); $ products-> add_to_columns('product-category');
This should help you out. 这应该可以帮助您。 The follow code displays 5 of each category.
以下代码显示每个类别中的5个。 Just switch the 5 to how many ever you wish to display from each category.
只需将5切换到每个类别中希望显示的数量即可。
<?php
$cat_args = array(
'orderby' => 'name',
'post_type' => 'products',
'order' => 'ASC',
'child_of' => 0
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<dl>';
echo '<dt> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></dt>';
$post_args = array(
'numberposts' => 5,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
<?php
}
echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
echo '</dl>';
}
?>
<?php
$cats = array('3d-mail', 'boxes', 'folders', 'binders');
$exclude_posts = array();
foreach( $cats as $cat )
{
// build query argument
$query_args = array(
'category_name' => $cat,
'showposts' => 1,
'post_type' => 'products',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
);
// exclude post that already have been fetched
// this would be useful if multiple category is assigned for same post
if( !empty($exclude_posts) )
$query_args['post__not_in'] = $exclude_posts;
// do query
$query = new WP_Query( $query_args );
// check if query have any post
if ( $query->have_posts() ) {
// start loop
while ( $query->have_posts() ) {
// set post global
$query->the_post();
// add current post id to exclusion array
$exclude_posts[] = get_the_ID();
<?php the_content(); ?>
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.