如何在主页上显示最近的帖子,包括标题、发帖日期、作者和特色图片?

时间:2017-06-13 作者:J.Lee

我希望在我的主页横幅上显示一些尚未确定数量的最近帖子(不超过5篇)。对于每个帖子,我想显示标题、发布日期、作者和特色图片作为内容缩略图的背景。

我想知道WordPress的功能是什么。我还想自定义HTML格式。我得到的最接近的结果是

<?php    
<!-- Post carousel -->
            <div class="cs-post-carousel-layout">
                <div class="cs-container swiper-container">
                    <div class="swiper-wrapper">
                    <?php

    // define query arguments
    $args = array(
        \'posts_per_page\' => 8, // your \'x\' goes here
        \'nopaging\' => true
        // possibly more arguments here
    );

    // set up new query
    $tyler_query = new WP_Query( $args );

    // loop through found posts
    while ( $tyler_query->have_posts() ) : $tyler_query->the_post();
                        //Post item -->
                        echo \'<div class="swiper-slide">\'.
                            \'<div class="cs-post-item">\'.
                                \'<div class="cs-post-category-icon">\'.
                                    \'<a href="\'.
                                    get_permalink().
                                    \'"></i></a>\'.
                                \'</div>\'.
                                \'<div class="cs-post-thumb">\'.
                                    \'<a href="post_standard.html"><img src="demo/carousel/1.jpg" alt="UniqMag"></a>\'.
                                \'</div>\'.
                                \'<div class="cs-post-inner">\'.
                                    \'<h3><a href="\'.
                                    get_permalink().
                                    \'">\'.
                                    get_the_title().
                                    \'</a></h3>\'.
                                    //\'<div class="cs-post-meta cs-clearfix">\'.
                                       // \'<span class="cs-post-meta-author"><a href="post_standard.html">J. Doe</a></span>\'.
                                       // \'<span class="cs-post-meta-date">Sep 19, 2015</span>\'.
                                    //\'</div>\'.
                                \'</div>\'.
                            \'</div>\'.
                        \'</div>\';
                        endwhile;

    // reset post data
    wp_reset_postdata();

?>

                    </div>
                </div>
            </div>

1 个回复
SO网友:Johansson

您正在寻找的功能包括:

the_post_thumbnail_url(); // For featured image
the_author(); // For author name
get_author_posts_url(); // For author link
the_date(); // For post\'s date
因此,您的代码应该是这样的:

<div class="cs-post-carousel-layout">
    <div class="cs-container swiper-container">
        <div class="swiper-wrapper"><?php

            // define query arguments
            $args = array(
                \'posts_per_page\' => 5, // your \'x\' goes here
                \'nopaging\' => true
                // possibly more arguments here
            );

            // set up new query
            $tyler_query = new WP_Query( $args );

            // loop through found posts
            if ($tyler_query->have_posts()) {
                while ( $tyler_query->have_posts() ) {
                    $tyler_query->the_post();
                    //Post item -->
                    ?><div class="swiper-slide">
                        <div class="cs-post-item">
                            <div class="cs-post-category-icon">
                                <a href="<?php the_permalink(); ?>"></i></a>
                            </div>
                            <div class="cs-post-thumb">
                                <a href="<?php the_permalink();?>"><img src="<?php the_post_thumbnail_url();?>" alt="<?php the_title();?>"></a>
                            </div>
                            <div class="cs-post-inner">
                                <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
                                <div class="cs-post-meta cs-clearfix">
                                   <span class="cs-post-meta-author"><a href="<?php echo get_author_posts_url(get_the_author_meta(\'ID\'));?>"><?php the_author();?></a></span>
                                   <span class="cs-post-meta-date"><?php the_date();?></span>
                                </div>
                            </div>
                        </div>
                    </div><?php
                }
            }
            // reset post data
            wp_reset_postdata();?>
        </div>
    </div>
</div>
我还为您重新格式化了HTML代码,并添加了缩进。

结束

相关推荐

Recent posts on homepage

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