Recent posts on homepage

时间:2013-01-29 作者:Niels Jansen

我已经做了一个页面,你们可以从我的帖子中找到所有的挑逗者(用阅读更多标签制作的挑逗者)。(通过单击其中一个,您可以“打开”整篇文章,包括照片,…。这很好。在我的主页上,我想要一个简短的欢迎文本,并在侧栏中显示我最近的3篇帖子。我首先尝试使用以下代码在我的主页上获取最近的帖子

<?php
function recentPosts() {
    $rPosts = new WP_Query();
    $rPosts->query(\'showposts=3\');
        while ($rPosts->have_posts()) : $rPosts->the_post(); ?>

                <a href="<?php the_permalink();?>"><?php the_post_thumbnail(\'recent-thumbnails\'); ?></a>
                <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
                <?php the_content(); ?>

        <?php endwhile; 
    wp_reset_query();
}
?>
这是很好的工作,但在我的主页上,我从我的文章中得到了所有的内容,我只想要一个挑逗。(例如,2行)。我不知道在这里怎么做。下面是主页的代码。

<?php
/**
 * @content not visible
 * @content not visible
 *
 * Template Name: homepage
 */
?>
<?php get_header(); ?>
<?php echo recentPosts(); ?>
<?php the_content(); ?>
<?php get_footer(); ?>

3 个回复
SO网友:Chip Bennett

首先,确保模板文件命名为home.php.

其次,在此上下文中不需要使用自定义查询循环。如果您只想在博客帖子索引(即“主页”)上显示3篇帖子,请通过以下方式过滤主循环查询pre_get_posts:

function wpse83660_filter_pre_get_posts( $query ) {
    if ( is_home() && $query->is_main_query() ) {
        $query->set( \'posts_per_page\', \'3\' );
    }
}
add_action( \'pre_get_posts\', \'wpse83660_filter_pre_get_posts\' );
您可以替换整个recentPosts() 具有正常循环的函数。

瑟德,你打电话来the_content() 既在循环实例化之外,又希望它返回除返回内容以外的内容:

<?php get_header(); ?>
<?php the_content(); ?>
<?php get_footer(); ?>
此呼叫get_content() 有2个问题:

尚未实例化循环:

if ( have_posts() ) : while ( have_posts() ) : the_post();
在博客帖子索引上下文中,此调用the_content() 将返回博客帖子索引中帖子的内容,not 指定为的*页的内容page_for_posts

通过解决第二个问题,我们也可以解决第一个问题。

获取指定为的页面的post\\u内容page_for_posts, 使用get_page(), 然后通过它get_option( \'page_for_posts\' ):

$page_object = get_page( get_option( \'page_for_posts\' ) );
然后,输出其结果:

if ( ! is_null( $page_object ) ) {
    echo apply_filters( \'the_content\', $page_object->post_content );
}
因此,整个模板文件如下所示:

/**
 * Blog posts index template file
 *
 * Displays the blog posts index
 * 
 * @filename: home.php
 */

get_header();

if ( have_posts() ) : while ( have_posts() ) : the_post();

    <a href="<?php the_permalink();?>"><?php the_post_thumbnail(\'recent-thumbnails\'); ?></a>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
    <?php the_content(); ?>

endwhile; endif;

$page_object = get_page( get_option( \'page_for_posts\' ) );
if ( ! is_null( $page_object ) ) {
    echo apply_filters( \'the_content\', $page_object->post_content );
}

get_footer();

SO网友:Lea Cohen

您的代码正在调用the_content() 函数,该函数返回整个帖子。

如果您只需要一个“摘要”,那么您的代码应该调用the_excerpt() 作用

SO网友:AndyWarren

添加<?php global $more; $more = 0; ?> 右后/下while ($rPosts->have_posts()) : $rPosts->the_post(); ?> 你应该让你的“更多”标签起作用。

结束

相关推荐

Sidebar missing from Homepage

这个home page 在所有最新帖子的其余部分下方显示侧边栏,为大部分(长)页面留下空白侧边栏区域。侧栏在任何single post.我的index.php 和single.php 除了“下一篇文章”和“上一篇文章”中的细微差别外,它们实际上是相同的。主题侧栏中没有部署小部件,称为侧栏1;没有其他提要栏。侧栏的所有元素都编码到侧栏中。php,并且没有条件检查哪个页面正在调用侧栏,因此它应该(并且正在)在索引页面和单个帖子上显示相同的内容,只是在不同的位置。类别页面也会在正确的位置显示侧栏。我想可能会有&