阻止显示子类别

时间:2012-07-05 作者:Rasha Nour Eldin

我使用本教程创建了可过滤的公文包

http://wp.tutsplus.com/tutorials/creating-a-filterable-portfolio-with-wordpress-and-jquery/

一切都很好,但它抓住了子标签,它的帖子也不仅仅是主标签。我怎样才能阻止这个问题?

        <?php
             $terms = get_terms("tagportifolio");
             $count = count($terms);
             echo \'<ul id="portfolio-filter">\';
                echo \'<li><a href="#all" title="">All</a></li>\';
             if ( $count > 0 ){

                    foreach ( $terms as $term ) {

                        $termname = strtolower($term->name);
                        $termname = str_replace(\' \', \'-\', $termname);
                        echo \'<li><a href="#\'.$termname.\'" title="" rel="\'.$termname.\'">\'.$term->name.\'</a></li>\';
                    }
             }
             echo "</ul>";
        ?>
        <?php 
            $loop = new WP_Query(array(\'post_type\' => \'project\', \'posts_per_page\' => -1));
            $count =0;
        ?>

        <div id="portfolio-wrapper">
            <ul id="portfolio-list">

            <?php if ( $loop ) : 

                while ( $loop->have_posts() ) : $loop->the_post(); ?>

                    <?php
                    $terms = get_the_terms( $post->ID, \'tagportifolio\' );

                    if ( $terms && ! is_wp_error( $terms ) ) : 
                        $links = array();

                        foreach ( $terms as $term ) 
                        {
                            $links[] = $term->name;
                        }
                        $links = str_replace(\' \', \'-\', $links); 
                        $tax = join( " ", $links );     
                    else :  
                        $tax = \'\';  
                    endif;
                    ?>

                    <?php $infos = get_post_custom_values(\'_url\'); ?>

                    <li class="portfolio-item <?php echo strtolower($tax); ?> all">
                        <div class="thumb"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail( array(215, 215) ); ?></a></div>
                        <div class="projectcontent">
                        <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
                        <p class="excerpt"><a href="<?php the_permalink() ?>"><?php echo get_the_excerpt(); ?></a></p>
                        <p class="links"><a href="<?php the_permalink() ?>">More Details &rarr;</a></p>

                        </div>
                    </li>

                <?php endwhile; else: ?>

                <li class="error-not-found">Sorry, no portfolio entries for while.</li>

            <?php endif; ?>


            </ul>

            <div class="clearboth"></div>

        </div> <!-- end #portfolio-wrapper-->


        <script>
            jQuery(document).ready(function() { 
                jQuery("#portfolio-list").filterable();
            });
        </script>


                </div><!-- .entry-content -->

            </div><!-- #post -->

2 个回复
SO网友:Pontus Abrahamsson

要仅获取父术语,必须使用参数parent 和设置lvl 0. From codex:

如果通过0,则只返回顶级术语。默认值为空字符串。

$terms = get_terms( \'tagportifolio\', array( \'parent\' => 0 ) );
请参见此处的另一个问题和答案:How can I get only parent terms?

SO网友:Rasha Nour Eldin

使用此循环成功隐藏导航

<?php
$args = array(\'hide_empty\'=>false,\'parent\'=>0);
$terms = get_terms("tagportifolio", $args );
$count = count($terms);
echo \'<ul id="portfolio-filter">\';
echo \'<li><a href="#all" title="">All</a></li>\';
if ( $count > 0 ){

    foreach ( $terms as $term ) {

        $termname = strtolower($term->name);
        $termname = str_replace(\' \', \'-\', $termname);
        echo \'<li><a href="#\'.$termname.\'" title="" rel="\'.$termname.\'">\'.$term->name.\'</a></li>\';
    }
}
echo "</ul>";
?>
<?php 
$loop = new WP_Query(array(\'post_type\' => \'project\', \'posts_per_page\' => -1));
$count =0;
?>
但仍在处理posts查询

结束

相关推荐