最近的帖子有缩略图,标题,日期和类别 - Wordpress

时间:2018-06-12 13:42:18

标签: wordpress

在Wordpress中,我试图在列表中显示我最近的帖子。我能够列出链接,但不能对帖子缩略图,标题,日期和类别做同样的事情。我究竟做错了什么?

    <?php $recent_posts = get_posts('numberposts=5');
                            if($recent_posts) { ?>
        <ul class="article_list">
            <?php foreach( $recent_posts as $recent ) { ?>
                <li class="regular">
                    <a href="<?php echo get_permalink($recent->ID); ?>">
                        <div class="text">
                            <p class="category"><?php echo the_date();?></p>
                            <h3 class="article_title"><?php echo get_the_title( $post_id ); ?></h3>
                            <p class="date"><?php echo the_date();?></p>
                        </div>
                        <div class="mask">
                            <img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
                        </div>
                    </a>
                </li>
            <?php } ?>
        </ul>
    <?php } ?>

2 个答案:

答案 0 :(得分:1)

尝试以下代码为我工作。

       <?php $recent_posts = get_posts('numberposts=5');
                               if($recent_posts) { ?>
            <ul class="article_list">
               <?php foreach( $recent_posts as $recent ) { ?>
                   <li class="regular">
                       <a href="<?php echo get_permalink($recent->ID); ?>">
                           <div class="text">
                                 <p class="category"><?php echo  get_the_category( $recent->ID );?></p>
                                 <h3 class="article_title"><?php echo get_the_title( $recent->ID); ?></h3>
                               <p class="date"><?php echo  get_the_date( $recent->ID );?></p>
                           </div>
                           <div class="mask">
                               <img src="<?php echo get_the_post_thumbnail_url($recent->ID,'full'); ?>" alt="" class="art_img">
                           </div>
                       </a>
                   </li>
               <?php } ?>
            </ul>

答案 1 :(得分:1)

另一种方法,使用WP_Query

<?php
// args query
$args = array(
    'post_type'      => 'post',
    'posts_per_page' => 5,
    'order'          => 'DESC',
    // display only posts in specifics categories (slug)
    'category_name' => 'cat-a, cat-b'
);

// custom query
$recent_posts = new WP_Query($args);

// check that we have results
if($recent_posts->have_posts()) : ?>

    <ul class="article_list">

        <?php 
        // start loop
        while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>

            <li class="regular">
                <a href="<?php echo get_permalink(); ?>">
                    <div class="text">
                        <p class="category"><?php echo the_category(); ?></p>
                        <h3 class="article_title"><?php echo get_the_title(); ?></h3>
                        <p class="date"><?php echo the_date();?></p>
                    </div>
                    <div class="mask">
                        <img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
                    </div>
                </a>
            </li>

        <?php endwhile; ?>

    </ul>

<?php endif;

// reset query
wp_reset_postdata();
?>