我想在帖子模板中为特定类别添加一个Wordpress循环,以取消当前帖子 .
我被建议使用:
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
但我无法让它发挥作用 .
我的循环目前看起来像这样 .
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
3 回答
试试这个代码 .
使用
两个代码块使用两种不同的技术用于Wordpress自定义循环...第一个修改全局查询,第二个创建新的自定义查询 . 我已经在下面概述了你的循环模板 .
Example with suggested code, global query:
循环遍历循环代码中的全局$ wp_query对象:
Example with original code, custom query :
循环遍历自定义查询,添加“post__not_in”:
对不起,如果我的原始答案不清楚,我最初认为你是在组合这两个代码块 .