从自定义帖子类型查询帖子,并在当前帖子上显示最多两个类别

时间:2014-07-08 00:05:32

标签: wordpress

我想查询添加了不同帖子类型的帖子:

原始查询是这样的:

 query_posts( "post_type=market-item&posts_per_page=".$post_per_page."&paged=".$paged ); 

我不知道在哪里或如何指定仅为自定义帖子类型显示一个或两个类别的逻辑

以下是代码的一部分:

<ul class="item-list list"> 
 <?php  
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;    
 $post_per_page=16;
//query_posts( "post_type=market-item&posts_per_page=".$post_per_page."&paged=".$paged    ); 
//query_posts('post_type=market-item',  );  

$args = array('post_type' => 'market-item','cat'=>13);

 query_posts($args );


 if(have_posts()) :while ( have_posts() ) : the_post();
 $image1= get_post_meta( $post->ID, 'sangvish_Image1', true );
 $image2= get_post_meta( $post->ID, 'sangvish_Image2', true );
  $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'item-cat', '',        ' / ', '' ) );
 ?>      
 <li class="index-page-thumbnail">
 <div class="thumbnail">
 <a href="<?php the_permalink(); ?>">
 <img src="<?php echo $image1; ?>"
 border="0"
alt="<?php the_title(); ?>"
title="<?php the_title(); ?>"
height="80"
width="80"
class="thumbnail-image-popup preload no_preview"
image-preview-width=""
image-preview-height=""
data-item-name="<?php the_title(); ?>"
data-item-author="<?php the_author(); ?>"
data-item-category="<?php echo $terms_as_text; ?>"
 data-item-cost="<sup><?php echo sangvish_currency_simb(); ?></sup><?php echo     get_post_meta( $post->ID, 'sangvish_price', true ); ?>"
 popup-large="<?php echo $image2; ?>" />
 </a>
</div>

1 个答案:

答案 0 :(得分:0)

尝试以下代码

$cnt = 1;           
$categories = get_the_category();
$separator = ' | ';
$output = '';
if($categories){
    foreach($categories as $category) {
        if($cnt<=2) {
            $output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
        }
       $cnt++;
    }
echo trim($output, $separator);
}