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

发布于 2024-12-05 19:09:40 字数 6938 浏览 1 评论 0原文

我想从此 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(); ?>

i want to exclude some categories from this wp template page, but having problem .. please help me to finish this. codes are following..

<?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(); ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼角的笑意。 2024-12-12 19:09:40

要以“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:这绝对不是最好的方法,只是给他一个快速的解决方案。

For filtering out the child_categories in a "hacky way":

Add below:

foreach ($child_cats as $child_cat) { ?>

The following code (where 1,2,3 is the category id you want to exclude):

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

You can do the same for filtering out the "parent" categories.

PS: This is definitely not the best way to do this, just giving him a quick solution.

梦与时光遇 2024-12-12 19:09:40

据我所知,类别可以通过数据库删除,而不是通过您显示的代码删除。

您的 WordPress 的管理部分提供了一种更优雅的方法。尝试将其记录到 /wp-admin 并将其从那里删除。这很容易。

如果您想跳过其中的一些内容,则:

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

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

AFAIK the categories can be removed via the database and not by the code that you showed..

A more elegant way is provided by the admin section of your wordpress. try logging it to /wp-admin and remove it from there. Its easy.

and if you want to skip a few in this then :

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

where 2 is the index of item you want to skip

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文