如何在wordpress中显示每个类别的自定义帖子类型的最新帖子

时间:2015-12-02 20:59:34

标签: php wordpress

这个问题似乎已经在一些品种中提出过,但我的问题非常具体。

我需要显示所有类别的自定义帖子类型的最新帖子,名为" products"。我需要显示类别而不是标题,并链接到该猫的档案。

问题是,我得到的只是一般帖子类型的一个帖子。有什么想法吗?

这是我的出发点:

<?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; ?>

自定义帖子类型是通过第3个插件创建的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' ) ) ) );

$ products-&gt; add_to_columns(&#39; product-category&#39;);

2 个答案:

答案 0 :(得分:1)

这应该会帮助你。以下代码显示每个类别的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>';
    } 
    ?>

答案 1 :(得分:0)

<?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();
    }

?>