[英]display recents posts on single post page and exclude active single post
I'm developping a website on wordpess. 我正在开发有关wordpess的网站。
I have a page displaying all the news title (posts titles). 我有一个页面,显示所有新闻标题(帖子标题)。 when clicking on one title, I'm directed to the single posts page. 单击一个标题时,我被定向到单个帖子页面。 I want to display on every single posts page the 5 most recents posts title at the bottom of my single posts pages. 我想在每个帖子页面的底部显示5个最新帖子标题。 it works fine using this code. 使用此代码可以正常工作。
<?php $the_query = new WP_Query( 'showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<?php the_time('y-m-d'); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile;?>
what I would like to do is to exclude the active single post I'm reading of the 5 most recents posts, otherwise my posts is displayed twice... 我想做的是排除我正在阅读的5个最新帖子的活跃的单个帖子,否则我的帖子会显示两次...
anyone know if it's possible and how I can achieve this ? 有人知道这是否有可能,我如何实现?
thanks for your help, 谢谢你的帮助,
$args = array('posts_per_page' => 5,
'post__not_in' => $post->ID
);
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) : $the_query -> the_post();
the_time('y-m-d');
the_title();
the_content();
endwhile; wp_reset_query(); ?>
pass the $post->ID to the post_not_in array 将$ post-> ID传递给post_not_in数组
id will get the current post id id将获取当前帖子ID
You can use get_post for that 您可以使用get_post
Example: 例:
<?php
$post = get_post($id); //assuming $id has been initialized
setup_postdata($post);
// display the post here
the_title();
the_excerpt();
the_post_thumbnail();
wp_reset_postdata();
?>
I've found a solution, here is the code : 我找到了解决方案,这是代码:
<?php $args = $args = array(
'numberposts' => 5,
'offset' => 0,
'post__not_in' => array( $post->ID )
);
$myposts2 = get_posts($args);
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) : $the_query -> the_post();?>
<div class="news">
<?php the_post_thumbnail('full'); ?>
<?php the_time('y-m-d'); ?>
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_query(); ?>
thank you all for your help 谢谢大家的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.