[英]In a wordpress post, display the list of posts that belong to the same category as the current post but exclude the current post itself
[英]Exclude current post from “Other posts of same category” loop
我使用以下代码在页面中显示相同类别的其他帖子:
<?php
global $post;
$categories = get_the_category();
$ceker=false;
foreach ($categories as $category) {
if ($ceker == false){
$ceker=true;
?>
<h3 class="naslovostalih">Ostali članci iz ove kategorije:</h3>
<ul class="clanciostalih">
<?php
$args = array(
'numberposts' => 10,
'offset'=> 1,
'category' => $category->term_id,
'exclude' => $post->ID
);
}
$posts = get_posts($args);
foreach($posts as $pz) { ?>
<li>
<?php
$title = $pz->post_title;
$link = get_permalink($pz->ID);
printf('<a class="linkpost" title="%s" href="%s">%s</a>', $title, $link, $title);
the_post_thumbnail('thumb-232');
echo '<div id="excerptcu">';
echo excerpt(25);
echo '</div>';
?>
<p class="more-link-wrapper2"><a href="<?php $link; ?>" class="read-more button"><?php _e( 'Opširnije »', 'fearless' ); ?></a></p>
</li>
<?php } // end foreach ?>
</ul>
如何从查询中排除客户正在查看的当前帖子,这样它就不会显示在“此类别的其他帖子”列表中?
非常感谢!
get_posts有一个参数“ exclude
”
$args = array(
'numberposts' => 10, 'offset'=> 1,
'category' => $category->term_id,
'exclude' => $post->ID
);
$posts = get_posts($args);
在您的foreach()
循环中使用另一个变量,因为它与全局$post
不明确
编辑
<?php
global $post;
$categories = get_the_category();
$ceker=false;
foreach ($categories as $category) {
if ($ceker == false){
$ceker=true;
?>
<h3 class="naslovostalih">Ostali članci iz ove kategorije:</h3>
<ul class="clanciostalih">
<?php
$args = array(
'numberposts' => 10,
'offset'=> 1,
'category' => $category->term_id,
'exclude' => $post->ID
);
$posts = get_posts($args);
foreach($posts as $pz) { ?>
<li>
<?php
$title = $pz->post_title;
$link = get_permalink($pz->ID);
printf('<a class="linkpost" title="%s" href="%s">%s</a>', $title, $link, $title);
echo get_the_post_thumbnail($pz->ID, 'thumbnail');
echo '<div id="excerptcu">';
echo substr($pz->post_content, 0, 25);
echo '</div>';
?>
<p class="more-link-wrapper2"><a href="<?php $link; ?>" class="read-more button"><?php _e( 'Opširnije »', 'fearless' ); ?></a></p>
</li>
<?php } // end foreach ?>
</ul>
<?php } // end if
} //end foreach
?>
您可以使用continue
跳过当前类别的循环。 在循环中比较所选类别的ID,然后跳过循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.