如何从WordPress的特定页面中排除类别
我想从此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要以“hacky 方式”过滤掉 child_categories:
添加以下内容:
以下代码(其中 1,2,3 是您要排除的类别 ID):
您可以执行相同的操作来过滤掉“父”类别。
PS:这绝对不是最好的方法,只是给他一个快速的解决方案。
For filtering out the child_categories in a "hacky way":
Add below:
The following code (where 1,2,3 is the category id you want to exclude):
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.
据我所知,类别可以通过数据库删除,而不是通过您显示的代码删除。
您的 WordPress 的管理部分提供了一种更优雅的方法。尝试将其记录到
/wp-admin
并将其从那里删除。这很容易。如果您想跳过其中的一些内容,则:
其中 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 :
where 2 is the index of item you want to skip