我需要为Wordpress创建一段PHP代码,以显示当前页面的3个兄弟页面,其特色图像和标题链接到相关页面。目前我有以下代码输出所有兄弟页面名称和缩略图,但我需要调整它,因此它只显示三个,帖子标题和缩略图都链接到兄弟页面,并有一个链接和他们周围的h4 。有任何想法吗...? 感谢
<div class="list_related">
<?php global $post; //not neccessary if used in the loop
$parent = $post->post_parent;
if( $parent ) :
$siblings = get_pages( 'child_of=' . $parent . '&parent=' . $parent . '&exclude=' . $post->ID);
if( $siblings ) foreach( $siblings as $sibling ) :
//start of whatever you need to output//
echo get_the_post_thumbnail($sibling->ID,'thumbnail');
echo $sibling->post_title;
//end of whatever you need to output//
endforeach;
endif; //ends if( $parent ) //
?>
</div>
答案 0 :(得分:1)
首先,要将其限制为3,您可以将get_pages()
行更改为:
$siblings = get_pages( array(
'child_of' => $parent,
'parent' => $parent,
'exclude' => $post->ID,
'number' => 3 //this part limits it to 3
));
然后你可以改变你的foreach
循环看起来像这样
if($siblings): foreach($siblings as $sibling):?>
<h4>
<a href="<?php echo get_permalink($sibling->ID);?>">
<?php echo $sibling->post_title;?>
<?php echo get_the_post_thumbnail($sibling->ID, 'thumbnail');?>
</a>
</h4>
<?php endforeach; endif;?>