Wordpress 模板自定义 > 相关帖子循环输出当前 post
Wordpress Template customization > Related Posts Loop outputting from current post
所以我试图输出一个循环,其中包含来自同一类别的 3 posts。当它输出每个项目的循环时,它似乎正确地拉动了一切 除了 只有 get_the_content 是正确的。其他数据(图像、标题、link)正在输出当前 post 的值。
所以基本上我需要让这个循环获得 the_post_thumbnail 和 the_title 和 get_permalink 的正确值。
<?php
/**
* The template used for displaying page content
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
?>
<?php
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 3, 'post__not_in' => array($post->ID) ) );
if( $related ) {
?>
<div class="related-posts">
<h2>Related Posts</h2>
<?php foreach( $related as $related_post ) {
setup_postdata($related_post);
?>
<article>
<div class="post-thumbnail">
<?php the_post_thumbnail('thumbnail'); ?>
</div><!-- .post-thumbnail -->
<?php the_title( '<h3>', '</h3>' ); ?>
<p>
<?= wp_trim_words( get_the_content(), 10, '<a href="'. get_permalink() .'"> Read More...</a>' ); ?>
</p>
</article>
<?php
}
?>
</div>
<?php
}
wp_reset_postdata();
?>
由于 setup_postdata
函数,您遇到了问题。这个函数接受一个对象作为参数,它应该是 $post
。
检查文档。 https://codex.wordpress.org/Function_Reference/setup_postdata
<?php
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 3, 'post__not_in' => array($post->ID) ) );
if( $related ) {
?>
<div class="related-posts">
<h2>Related Posts</h2>
<?php
global $post;
foreach( $related as $post ) {
setup_postdata($post);
?>
<article>
<div class="post-thumbnail">
<?php the_post_thumbnail('thumbnail'); ?>
</div><!-- .post-thumbnail -->
<?php the_title( '<h3>', '</h3>' ); ?>
<p>
<?= wp_trim_words( get_the_content(), 10, '<a href="'. get_permalink() .'"> Read More...</a>' ); ?>
</p>
</article>
<?php
}
?>
</div>
<?php
}
wp_reset_postdata();
?>
所以我试图输出一个循环,其中包含来自同一类别的 3 posts。当它输出每个项目的循环时,它似乎正确地拉动了一切 除了 只有 get_the_content 是正确的。其他数据(图像、标题、link)正在输出当前 post 的值。
所以基本上我需要让这个循环获得 the_post_thumbnail 和 the_title 和 get_permalink 的正确值。
<?php
/**
* The template used for displaying page content
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
?>
<?php
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 3, 'post__not_in' => array($post->ID) ) );
if( $related ) {
?>
<div class="related-posts">
<h2>Related Posts</h2>
<?php foreach( $related as $related_post ) {
setup_postdata($related_post);
?>
<article>
<div class="post-thumbnail">
<?php the_post_thumbnail('thumbnail'); ?>
</div><!-- .post-thumbnail -->
<?php the_title( '<h3>', '</h3>' ); ?>
<p>
<?= wp_trim_words( get_the_content(), 10, '<a href="'. get_permalink() .'"> Read More...</a>' ); ?>
</p>
</article>
<?php
}
?>
</div>
<?php
}
wp_reset_postdata();
?>
由于 setup_postdata
函数,您遇到了问题。这个函数接受一个对象作为参数,它应该是 $post
。
检查文档。 https://codex.wordpress.org/Function_Reference/setup_postdata
<?php
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 3, 'post__not_in' => array($post->ID) ) );
if( $related ) {
?>
<div class="related-posts">
<h2>Related Posts</h2>
<?php
global $post;
foreach( $related as $post ) {
setup_postdata($post);
?>
<article>
<div class="post-thumbnail">
<?php the_post_thumbnail('thumbnail'); ?>
</div><!-- .post-thumbnail -->
<?php the_title( '<h3>', '</h3>' ); ?>
<p>
<?= wp_trim_words( get_the_content(), 10, '<a href="'. get_permalink() .'"> Read More...</a>' ); ?>
</p>
</article>
<?php
}
?>
</div>
<?php
}
wp_reset_postdata();
?>