循环中的帖子显示所有分类

时间:2012-11-05 作者:Mister Cairns

我设置了一个循环来显示每个项目都附加了两种分类法的项目。我的问题是,在循环中,我设置了一个功能来选择特定的帖子,为其附加不同的图像/样式,出于某种原因,这些帖子正在打印每个分类法,而不仅仅是与它们具体相关的分类法。循环中的其余帖子正在显示其正确的分类法。有什么线索吗?完整循环代码如下:

<?php $loop = new WP_Query( array( \'post_type\' => \'projects\', \'orderby\' => \'menu_order\', \'order\' => \'ASC\', \'showposts\' => 19 ) ); ?>
            <?php 
            $c = 0;
            while ( $loop->have_posts() ) : $loop->the_post();
            $c++;
        ?>
        <?php if( $c == 5 || $c == 15 || $c == 25 || $c == 35) : ?>
        <li class="left-feature feature">
            <span class="img">
            <a href="<?php the_permalink(); ?>" title="Learn more about <?php the_title(); ?>" rel="bookmark">
                <?php
                    $rows = get_field(\'feature_image\');
                    if($rows)
                    {
                        foreach($rows as $row)
                        {
                            echo \'<img src="\' . $row[\'feature\'] . \'" alt=" " />\';
                        }
                    }
                ?>
            </a>
            </span>
            <h2><?php the_title(); ?></h2>
            <?php
                $terms = get_terms(\'client\');
                $count = count($terms);
                if ( $count > 0 ){
                    echo "<p>Client: ";
                    foreach ( $terms as $term ) {
                    echo "<span>";
                    echo $term->name;
                    echo "</span>";
                }
            echo "</p>";
            }
            ?>
            <?php
                $terms = get_terms(\'type\');
                $count = count($terms);
                if ( $count > 0 ){
                    echo "<p>Type: ";
                    foreach ( $terms as $term ) {
                    echo "<span>";
                    echo $term->name;
                    echo "</span>";
                }
            echo "</p>";
            }
            ?>
        </li>
        <?php elseif( $c == 10 || $c == 20 || $c == 30 || $c == 40) : ?>
        <li class="right-feature feature">
            <span class="img">
            <a href="<?php the_permalink(); ?>" title="Learn more about <?php the_title(); ?>" rel="bookmark">
                <?php
                    $rows = get_field(\'feature_image\');
                    if($rows)
                    {
                        foreach($rows as $row)
                        {
                            echo \'<img src="\' . $row[\'feature\'] . \'" alt=" " />\';
                        }
                    }
                ?>
            </a>
            </span>
            <h2><?php the_title(); ?></h2>
            <?php
            $terms = get_terms(\'client\');
            $count = count($terms);
                if ( $count > 0 ){
                    echo "<p>Client: ";
                    foreach ( $terms as $term ) {
                        echo "<span>";
                        echo $term->name;
                        echo "</span>";
                    }
                echo "</p>";
                }
            ?>
            <?php
            $terms = get_terms(\'type\');
            $count = count($terms);
                if ( $count > 0 ){
                    echo "<p>Type: ";
                    foreach ( $terms as $term ) {
                        echo "<span>";
                        echo $term->name;
                        echo "</span>";
                    }
                    echo "</p>";
                }
            ?>
        </li>
        <?php else : ?>
        <li class="regular">
            <span class="img">
            <a href="<?php the_permalink(); ?>" title="Learn more about <?php the_title(); ?>" rel="bookmark">
                <?php
                    $rows = get_field(\'thumbnail_image\');
                    if($rows)
                    {
                        foreach($rows as $row)
                        {
                            echo \'<img src="\' . $row[\'thumbnail\'] . \'" alt=" " />\';
                        }
                    }
                ?>
            </a>
            </span>
            <h2><?php the_title(); ?></h2>
            <?php
            $terms = get_the_terms($post->ID, \'client\');
            echo \'<p>Client: \';
            foreach ($terms as $taxindex => $taxitem) {
                echo "<span>";
                echo $taxitem->name;
                echo "</span>";
            }
            echo \'</p>\'
            ?>
            <?php
            $terms = get_the_terms($post->ID, \'type\');
            echo \'<p>Type: \';
            foreach ($terms as $taxindex => $taxitem) {
                echo "<span>";
                echo $taxitem->name;
                echo "</span>";
            }
            echo \'</p>\'
            ?>
        </li>
    <?php endif; endwhile; wp_reset_query(); ?>
    </ul>

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

你是WordPress伟大命名方案的受害者。你不需要get_terms(), 但是get_the_terms(). 请注意the? 这有很大的不同。

  • get_terms( $taxonomies, $args = \'\' ): 检索给定分类法或分类法列表中的术语。All 条款。

  • get_the_terms( $id, $taxonomy ): 检索附加到帖子的分类法术语。Only 那职位的条件。

    现在,在循环中尝试以下操作:

    $terms = get_the_terms( get_the_ID(), \'category\' );
    print \'<pre>\' 
        . htmlspecialchars( 
            print_r( $terms, TRUE ), 
            ENT_QUOTES, 
            \'utf-8\', 
            FALSE 
        ) 
        . \'</pre>\';
    
    样本结果:

    Array
    (
        [4] => stdClass Object
            (
                [term_id] => 4
                [name] => aciform
                [slug] => aciform
                [term_group] => 0
                [term_taxonomy_id] => 4
                [taxonomy] => category
                [description] => 
                [parent] => 0
                [count] => 2
                [object_id] => 188
                [cat_ID] => 4
                [category_count] => 2
                [category_description] => 
                [cat_name] => aciform
                [category_nicename] => aciform
                [category_parent] => 0
            )
    
        [10] => stdClass Object
            (
                [term_id] => 10
                [name] => Cat A
                [slug] => cat-a
                [term_group] => 0
                [term_taxonomy_id] => 11
                [taxonomy] => category
                [description] => 
                [parent] => 0
                [count] => 2
                [object_id] => 188
                [cat_ID] => 10
                [category_count] => 2
                [category_description] => 
                [cat_name] => Cat A
                [category_nicename] => cat-a
                [category_parent] => 0
            )
    )
    
    现在可以迭代该数组并使用esc_html( $term->name ).

结束

相关推荐

trouble with my loop

我正在尝试从我的博客页面中排除类别。我已经把代码都设置好了,根据法典,这应该行得通 $string = \'-\' . implode( \',-\', explode( \' \', $data[\'exclude_categories\'] ) ); 测试时,上述行会将其打印到屏幕上-主页-最新新闻这是我试图从博客页面中删除的两个测试类别。然后我像这样开始我的查询 <?php query_posts($query_string . \'&cat=\' . $string);?