在自定义模板页面上显示某些类别

时间:2013-08-03 作者:shopowner

我复制了公文包列页面模板。。。在此页面上,我只想显示一个类别,而不是所有类别。。。我试了很多,但都没成功。。。

这是显示所有类别的原始代码:

<div class="container">

    <ul id="ublportfolio" class="grid cs-style-3">

        <?php 
            query_posts( array( \'post_type\' => \'ublalfieportfolio\', \'posts_per_page\' => -1) );
            if ( have_posts() ) : while ( have_posts() ) : the_post(); 

            global $post;
            $terms = get_the_terms($post->ID, \'ublalfieportfolio-categories\');
            $taxclassadd = \'\';
            foreach($terms as $t){$taxclassadd .= \' \' . $t->slug;}

            $taxclass = \'thisportfolioitem twocol\' . $taxclassadd; $str= ltrim ($taxclassadd,\' \');
        ?>

        <li <?php post_class($taxclass); ?> id="post-<?php the_ID(); ?>" data-category="<?php echo $str; ?>">
            <figure>

                <?php if(has_post_thumbnail()) { 
                $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'full\', false);
                if($thumbnail[\'1\'] > 1024){$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'large\', false);} ?>
                <img src="<?php echo $thumbnail[\'0\']; ?>" alt="<?php the_title(); ?>">
                <?php } ?>

                <figcaption>
                    <h3><?php the_title(); ?></h3>
                    <span><?php if($post->post_excerpt){$getcontent = get_the_excerpt();echo $getcontent;} ?></span>
                    <a href="<?php the_permalink(); ?>"><?php _e(\'View Item\',\'ublalfie\'); ?></a>
                </figcaption>

            </figure>
        </li>

        <?php endwhile; endif; ?>

    </ul>

</div>

例如,我只想显示:类别slug=“parketten”和ID=3

我试过这个:

<?php 
          query_posts( array( \'post_type\' => \'ublalfieportfolio\', \'category_name\' => \'terrassen\', \'posts_per_page\' => -1) );
          if ( have_posts() ) : while ( have_posts() ) : the_post(); 

          global $post;
          $terms = get_the_terms($post->ID, \'parketten\');
          $taxclassadd = \'\';
          foreach($terms as $t){$taxclassadd .= \' \' . $t->slug;}

          $taxclass = \'span4 portfolioitem text-center\' . $taxclassadd; $str= ltrim ($taxclassadd,\' \');
      ?>
结果:未显示任何内容

<?php 
            query_posts( array( \'post_type\' => \'ublalfieportfolio\', \'category\' => array(3), \'posts_per_page\' => -1) );
            if ( have_posts() ) : while ( have_posts() ) : the_post(); 

            global $post;
            $terms = get_the_terms($post->ID, \'parketten\');
            $taxclassadd = \'\';
            foreach($terms as $t){$taxclassadd .= \' \' . $t->slug;}

            $taxclass = \'span4 portfolioitem text-center\' . $taxclassadd; $str= ltrim ($taxclassadd,\' \');
        ?>
结果:显示所有类别。。

谁能给我一个解决方案。。。非常感谢你

1 个回复
最合适的回答,由SO网友:Vinod Dalvi 整理而成

您不应该使用category参数来查询自定义分类法,而应该使用tax\\u查询参数或直接使用自定义分类法名称,如以下查询所示。

query_posts( array( \'post_type\' => \'ublalfieportfolio\', \'ublalfieportfolio-categories\' => \'parketten\', \'posts_per_page\' => -1) );
参考this page 了解如何在自定义查询中使用自定义分类参数。

结束