这就是我想到的。我不认为这是编写代码的理想方式,但它正是我所需要的。
通过排除父类别,我获得了侧栏中“相关项目”子类别部分的标签:
More in
<?php
$category = get_the_category();
$catname = $category[0]->cat_name;
if ($catname == \'News\' || $catname == \'Featured\' || $catname == \'Nav Bar News\') {
$catname = $category[1]->cat_name;
if ($catname == \'News\' || $catname == \'Featured\' || $catname == \'Nav Bar News\') {
$catname = $category[2]->cat_name;
if ($catname == \'News\' || $catname == \'Featured\' || $catname == \'Nav Bar News\') {
$catname = $category[3]->cat_name;
}
}
} else {
$catname = $category[0]->cat_name;
}
echo $catname;
?>
这就是它变得有点凌乱的地方,但代码适用于我需要实现的目标。我排除了父类别,然后使用循环显示相关子类别中的帖子:
wp_reset_query();
$this_post = $post->ID;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
if ($category == \'30\' || $category == \'28\' || $category == \'99\') {
$category = $category[1]->cat_ID;
if ($category == \'30\' || $category == \'28\' || $category == \'99\') {
$category = $category[2]->cat_ID;
if ($category == \'30\' || $category == \'28\' || $category == \'99\') {
$category = $category[3]->cat_ID;
}
}
}
$args = array(
\'category__in\' => array($category),
\'post__not_in\' => array($this_post)
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() && !(in_category(\'32\'))) {
$i = 0; while ( $the_query->have_posts() && $i < 1) {
$the_query->the_post(); ?>
<?php the_post_thumbnail(\'medium\'); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<a href="<?php the_permalink(); ?>"><?php echo excerpt(30); ?></a>
<?php the_author(); ?>
<?php $i++; }
} else {
//echo \'no posts\';
}
wp_reset_postdata();
wp_reset_query();
$this_post = $post->ID;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
if ($category == \'30\' || $category == \'28\' || $category == \'99\') {
$category = $category[1]->cat_ID;
if ($category == \'30\' || $category == \'28\' || $category == \'99\') {
$category = $category[2]->cat_ID;
if ($category == \'30\' || $category == \'28\' || $category == \'99\') {
$category = $category[3]->cat_ID;
}
}
}
$args = array(
\'category__in\' => array($category),
\'post__not_in\' => array($this_post),
\'offset\' => 1
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() && !(in_category(\'32\'))) {
$i = 0; while ( $the_query->have_posts() && $i < 3) {
$the_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php if ($i < 2) { ?>
<hr style="width: 92%;">
<?php } ?>
<?php $i++; }
} else {
//echo \'no posts\';
}
wp_reset_postdata();
我试图在一个循环中完成这一切,但我想不出一种方法来仅显示第一篇文章的特色图像,然后将该文章从再次显示中排除。如果有人有更干净的方式写这篇文章,请让我知道。