对于类别。php我的自定义查询代码没有显示正确类别中的帖子。
我有一个名为“博客”的父类别,它可以有任意数量的未知子类别。考虑到这一点,我正在尝试自动化我的循环,以便它能够检测类别并仅显示该类别的文章(即使其名称未知)。
<section id="posts">
<div class="inner">
<?php
global $paged;
global $post;
$do_not_duplicate = array();
$categories = get_the_category();
$category = $categories[0];
$cat_ID = $category->cat_ID;
$myposts = get_posts(\'category=\'.$cat_ID.\'&paged=\'.$paged);
$do_not_duplicate[] = $post->ID;
$c = 0;
?>
<?php foreach($myposts as $post) : setup_postdata($post); $c++;
if( $c % 2 == 0) {
$style = \'even animated fadeIn\';
$c = 0;
}
else $style=\'animated fadeIn\'; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class($style) ?>>
<a class="article-block" href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) { ?>
<div class="c-2 post-image">
<span class="cat"><?php $category = get_the_category(); echo $category[1]->cat_name;?></span>
<span class="overlay">
<div class="outer">
<div class="middle">
<div class="c-inner">
<span class="button">Read more</span>
</div>
</div>
</div>
</span>
<?php the_post_thumbnail(\'full\'); ?>
</div>
<?php } ?>
<div class="c-2 snippet">
<h2><?php the_title(); ?></h2>
<?php the_content(\'Read more\'); ?>
</div>
</a>
</article>
<?php endforeach; ?>
<div class="load-more-wrap">
<div id="wait">Loading Posts...</div>
<button class="load-more button">Load More</button>
</div>
</div>
</section>
最合适的回答,由SO网友:Michael 整理而成
在您的代码中,您正在检查一篇文章的类别,这篇文章在循环之外是非常随机的,并且当这些文章有多个类别时;
类别存档页的类别ID通常为:
$cat_ID = get_query_var(\'cat\');