我最近更新了wordpress,现在除了"the_excerpt()“之外,所有的内容都为这个模块输出了。
<?php
function blog_feed_content(){
?>
<ul id="blog_list" class="jscroll">
<?php
global $post;
$args = array('category' => 4 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li class="post clearfix" id="post-<?php the_ID(); ?>">
<div class="post-content clearfix">
<h2 class="post-title"><?php the_title(); ?></h2>
<div class="post-date"><?php the_time('F Y') ?></div>
<div class="post-text">
<?php the_excerpt(); ?>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php
}
function widget_blog_feed($args){
extract($args);
echo $before_widget;
echo $before_title;?>Blog Feed<?php echo $after_title;
blog_feed_content();
echo $after_widget;
}
function init_blog_feed() {
register_sidebar_widget(__('blog_widget'), 'widget_blog_feed');
}
add_action("plugins_loaded", "init_blog_feed");
?>为什么它不输出这一条内容呢?
你们都太棒了。谢谢。
发布于 2013-05-14 04:20:59
setup_postdata与使用带有the_post()的普通WP_Query并不完全相同,因此并不是所有的模板标记都能在这种显示帖子的方法中按预期工作。
您应该重写代码以使用自定义的WP_Query和传统的循环,而不是使用foreach来迭代post对象。
类似于:
$myposts = new WP_Query('cat=4');
if( $myposts->have_posts() ) : while( $myposts->have_posts() ) : $myposts->the_post(); ?>
<li class="post clearfix" id="post-<?php the_ID(); ?>">
<div class="post-content clearfix">
<h2 class="post-title"><?php the_title(); ?></h2>
<div class="post-date"><?php the_time('F Y') ?></div>
<div class="post-text">
<?php the_excerpt(); ?>
</div>
</div>
</li>
<?php endwhile;
wp_reset_query;
endif; ?>这应该会给你指明正确的方向。如果您坚持使用foreach和get_posts方法,则可以始终使用一些简单的字符串函数(例如,使用substr()截断$post对象的post_content属性中的一小段摘录,如将<?php the_content(); ?>行替换为:
<?php echo substr($post->the_content, 0, 80); // displays first 80 chars of post ?>发布于 2021-07-07 00:39:57
您应该定义id。
<?php echo get_the_excerpt($post->ID); ?https://stackoverflow.com/questions/16529440
复制相似问题