如何在wordpress中显示自定义帖子类型的每个类别的最新帖子
这个问题似乎已经在某些品种中提出过,但我的具体情况很明确。如何在wordpress中显示自定义帖子类型的每个类别的最新帖子
我需要显示称为“产品”的自定义帖子类型的所有类别的最新帖子。我需要显示类别而不是标题,并链接到该档案的档案。
问题是,我所得到的只是该普通帖子类型的一个帖子。有什么想法吗?
这里是我的出发点:
<?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; ?>
的自定义后类型是通过第三插头创建成Super CPT插件
这里是代码:
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') ) ));
$产品 - > add_to_columns('product-category');
这应该会帮助你。以下代码显示每个类别的5个。只需将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>';
}
?>
正确。将帖子类型添加到$ cat_args 'post_type'=>'产品' –
谢谢。仍生成帖子的结果,而不是自定义帖子类型。 – psyxx
<?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();
}
?>
谢谢。但是这列出了主要类别。不属于我的自定义帖子类型。 – psyxx
正确。将帖子类型添加到$ cat_args'post_type'=>'产品' –
我已更新代码以反映您的自定义帖子类型。 –
我不知道有关该插件,因为我通过职能文件创建自己的插件。 add_action('init','create_post_type'); 功能create_post_type(){ register_post_type( '产品', 阵列( '标签'=>数组( '名称'=> __( '产品'), 'singular_name'=> __( '产品') ), 'public'=> true, 'has_archive'=> true, ) ); } –
这就是我会做的。但是我有一个以前的开发人员在这里工作。 – psyxx