如何从wordpress的特定页面中排除类别

时间:2011-09-19 08:42:03

标签: php wordpress function loops categories

我想从此wp模板页面中排除某些类别,但遇到问题..请帮我完成此操作。代码如下......

<?php
/*
Template Name: Menu card
*/
?>
<?php global $more, $post, $wpdb, $pageid;
get_header();
if (!$pageid) {
    $pageid = $post->ID;
}
if (is_category() ) {
    $cat_ID = get_query_var('cat');
}
$pagetitle = get_the_title($pageid);
$categories = get_post_meta($pageid, "categories", true);
?>
    <div id="content-top"></div>
    <div id="content-border">
        <div id="content" class="menucard">
            <div class="ribbon-container">
                <div class="title-container">
                    <div class="title">
                        <div class="bar-left"></div>
                        <div class="bar-right"></div>
                        <h1 class="post-title"><?php echo $pagetitle; ?></h1>
                    </div>
                </div>
            </div>
            <a href="" id="card-prev">
                <div class="arrow_bit_bottom"></div>
                <div class="arrow_bit_top"></div>
                <div class="arrow_bit_left"></div>
                <div class="arrow_bit_right"></div>
                <div class="arrow_bit_middle"></div>
            </a>
            <a href="" id="card-next">
                <div class="arrow_bit_bottom"></div>
                <div class="arrow_bit_top"></div>
                <div class="arrow_bit_left"></div>
                <div class="arrow_bit_right"></div>
                <div class="arrow_bit_middle"></div>
            </a>
            <?php 
            if ($cat_ID > 0) {
                $card_cats = explode(',',$categories);
                $count = 1;
                for ($x=0; $x < count($card_cats); $x=$x+2) {
                    for ($i=0;$i<2;$i++) { 
                        $array_location = $x+$i;
                        if ($cat_ID == $card_cats[$array_location]) {
                            $activepage = $count;
                        }
                    }
                    $count++;
                }
            } else {
                $activepage = 1;
            } ?>
            <div id="card-container" activepage="<?php echo $activepage; ?>">
                <div id="card-slider">
                    <?php $card_cats = explode(',',$categories);
                    $count = 1;
                    for ($x=0; $x < count($card_cats); $x=$x+2) { ?>
                        <div id="cardpageid-<?php echo $count; ?>" class="card-page">
                            <div class="menucard-devider"></div>
                            <?php for ($i=0;$i<2;$i++) { 
                                $array_location = $x+$i;
                                if (isset($card_cats[$array_location])) { ?>
                                    <div class="card-cat" id="cardcatid-<?php echo $card_cats[$array_location]; ?>" catid="<?php echo $card_cats[$array_location]; ?>">
                                        <h2><?php echo get_cat_name($card_cats[$array_location]); ?></h2>
                                        <?php $cat_desc = category_description( $card_cats[$array_location] );
                                        if ($cat_desc) { ?>
                                            <div class="cat-desc">
                                            <?php echo $cat_desc; ?>
                                            </div>
                                        <?php } ?>
                                        <?php $child_cats = get_categories('child_of='.$card_cats[$array_location]);
                                        $cat_array = '';
                                        foreach ($child_cats as $child_cat) {
                                            if ($cat_array != '') {
                                                $cat_array .= ',';
                                            }
                                            $cat_array .= '-'.$child_cat->term_id;
                                        }
                                        query_posts('cat='.$card_cats[$array_location].','.$cat_array.'&showposts=-1');
                                        if ( have_posts() ) { 
                                            while ( have_posts() ) { 
                                                the_post();
                                                $more = 0;
                                                include('menuitem.php');
                                            }
                                        }
                                        wp_reset_query();
                                        if ($child_cats) { 
                                            foreach ($child_cats as $child_cat) { ?>
                                                <h3><?php echo __($child_cat->cat_name); ?></h3>
                                                <div class="devider"></div>
                                                <?php $cat_desc = category_description( $child_cat->term_id );
                                                if ($cat_desc) { ?>
                                                    <div class="cat-desc">
                                                    <?php echo $cat_desc; ?>
                                                    </div>
                                                <?php }
                                                query_posts('cat='.$child_cat->term_id.'&showposts=-1');
                                                if ( have_posts() ) { 
                                                    while ( have_posts() ) { 
                                                        the_post();
                                                        $more = 0;
                                                        include('menuitem.php');
                                                    }
                                                }
                                                wp_reset_query();
                                            }
                                        } ?>
                                    </div>
                                <?php }
                            } ?>
                        </div>
                        <?php $count++;
                    } ?>
                </div>
            </div>
        </div><!-- #content -->
    </div>
    <div id="content-bottom"></div>
<?php get_footer(); ?>

2 个答案:

答案 0 :(得分:1)

以“hacky方式”过滤掉child_categories:

在下面添加:

foreach ($child_cats as $child_cat) { ?>

以下代码(其中1,2,3是您要排除的类别ID):

<?php if(in_array($child_cat->term_id, array(1,2,3))) continue;

您可以执行相同操作以过滤掉“父”类别。

PS:这绝对不是最好的方法,只是给他一个快速解决方案。

答案 1 :(得分:-1)

AFAIK可以通过数据库删除类别,而不是通过您显示的代码删除..

wordpress的管理部分提供了更优雅的方式。尝试将其记录到/wp-admin并从那里删除它。很容易。

如果你想在此跳过几个:

unset($child_cats[2]);
$child_cats= array_values($child_cats);

其中2是您要跳过的项目的索引