Php 从循环中排除当前帖子
我想在一个帖子模板中为一个特定的类别添加一个Wordpress循环,该模板可以导出当前帖子 有人建议我使用:Php 从循环中排除当前帖子,php,wordpress,while-loop,categories,Php,Wordpress,While Loop,Categories,我想在一个帖子模板中为一个特定的类别添加一个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(
<?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>
M
O
R
E
这两个代码块对Wordpress自定义循环使用了两种不同的技术。。。第一个修改全局查询,第二个创建新的自定义查询。我在下面用循环模板概述了这两个方面
带有建议代码的示例,全局查询:
循环代码中的全局$wp_查询对象:
<div class="video">
<?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'));
?>
<!-- use the global loop here -->
<?php while ( have_posts() ) : 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>
M
O
R
E
M
O
R
E
抱歉,如果我的原始答案不清楚,我最初以为您是在组合这两个代码块。使用
'post__not_in' => array($post->ID)
试试这个代码
$postid = get_the_ID();
$args=array(
'post__not_in'=> array($postid),
'post_type' => 'post',
'category_name'=>'video',
'post_status' => 'publish',
'posts_per_page' => 4
);
<div class="video">
<?php
$catquery = new WP_Query( $args );
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>
$postid=get_the_ID();
$args=数组(
'post__not_in'=>数组($posted),
“post_type”=>“post”,
“类别名称”=>“视频”,
“发布状态”=>“发布”,
“每页帖子数”=>4
);
M
O
R
E
如何将其添加到上面的循环中?
$postid = get_the_ID();
$args=array(
'post__not_in'=> array($postid),
'post_type' => 'post',
'category_name'=>'video',
'post_status' => 'publish',
'posts_per_page' => 4
);
<div class="video">
<?php
$catquery = new WP_Query( $args );
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>