摘录未显示在 "Latest posts" 区域。为什么?
Excerpt not showing on "Latest posts" area. Why?
我正在开发一个 wordpress 主题,在主页的主要内容下方有一个类似 "latest post" 的区域。自定义主题基于 Adamos 主题。到目前为止,我在打印主要内容后使用它:
<?php
wp_reset_query();
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ){
setup_postdata($recent);
?>
<div class="index_recent_post">
<div class="index_recent_title">
<h3><?php echo '<a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a>'; ?></h3>
</div>
<?php
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"], 'frontpage-thumbnail');
}
echo '<p>' . get_the_excerpt($recent["ID"]) . '</p>';
echo '<a href="' . get_permalink($recent["ID"]) . '" class="recent_link">MORE</a>';
?>
</div>
<?php
}
wp_reset_query();
?>
然而,虽然最新 post 的每一项(标题、缩略图、link)都完美无缺,但摘录却没有:它显示为空。如果我删除 wp_reset_query()
和 setup_postdata()
行,它会显示 MAIN post 的摘录,但似乎没有任何方法可以让它显示最新的摘录一个,即使最新 posts 的其余信息完美显示。
此外,无论 post 是否有自定义摘录,摘录都不会显示,因此问题不在于寻找自定义摘录但找不到的函数。我可以通过 $recent["post_excerpt"]
获取自定义摘录,但它只会在自定义摘录时获取 - 如果自定义摘录不存在,它不会根据内容构建摘录,这不太理想。
有没有人处理过这个问题,你能帮我看看问题出在哪里吗?
你能不能使用下面的codes.I 在我的本地测试此代码 server.It 工作正常。
wp_reset_query();
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ){
setup_postdata($recent);
?>
<div class="index_recent_post">
<div class="index_recent_title">
<h3><?php echo '<a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a>'; ?></h3>
</div>
<?php
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"], 'frontpage-thumbnail');
}
//echo '<p>' . get_the_excerpt($recent["ID"]) . '</p>';
$content= $recent["post_content"];
$excerpt = wp_trim_words( $content, $num_words = 55, $more = null );
echo $excerpt;
echo '<a href="' . get_permalink($recent["ID"]) . '" class="recent_link">MORE</a>';
?>
</div>
<?php
}
wp_reset_query();
我正在开发一个 wordpress 主题,在主页的主要内容下方有一个类似 "latest post" 的区域。自定义主题基于 Adamos 主题。到目前为止,我在打印主要内容后使用它:
<?php
wp_reset_query();
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ){
setup_postdata($recent);
?>
<div class="index_recent_post">
<div class="index_recent_title">
<h3><?php echo '<a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a>'; ?></h3>
</div>
<?php
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"], 'frontpage-thumbnail');
}
echo '<p>' . get_the_excerpt($recent["ID"]) . '</p>';
echo '<a href="' . get_permalink($recent["ID"]) . '" class="recent_link">MORE</a>';
?>
</div>
<?php
}
wp_reset_query();
?>
然而,虽然最新 post 的每一项(标题、缩略图、link)都完美无缺,但摘录却没有:它显示为空。如果我删除 wp_reset_query()
和 setup_postdata()
行,它会显示 MAIN post 的摘录,但似乎没有任何方法可以让它显示最新的摘录一个,即使最新 posts 的其余信息完美显示。
此外,无论 post 是否有自定义摘录,摘录都不会显示,因此问题不在于寻找自定义摘录但找不到的函数。我可以通过 $recent["post_excerpt"]
获取自定义摘录,但它只会在自定义摘录时获取 - 如果自定义摘录不存在,它不会根据内容构建摘录,这不太理想。
有没有人处理过这个问题,你能帮我看看问题出在哪里吗?
你能不能使用下面的codes.I 在我的本地测试此代码 server.It 工作正常。
wp_reset_query();
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ){
setup_postdata($recent);
?>
<div class="index_recent_post">
<div class="index_recent_title">
<h3><?php echo '<a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a>'; ?></h3>
</div>
<?php
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"], 'frontpage-thumbnail');
}
//echo '<p>' . get_the_excerpt($recent["ID"]) . '</p>';
$content= $recent["post_content"];
$excerpt = wp_trim_words( $content, $num_words = 55, $more = null );
echo $excerpt;
echo '<a href="' . get_permalink($recent["ID"]) . '" class="recent_link">MORE</a>';
?>
</div>
<?php
}
wp_reset_query();