简体   繁体   English

在(WordPress)主页的2列中显示2个最新博客文章

[英]Show 2 Recent Blog Posts in 2 Columns on Homepage in (WordPress)

I'm trying to show 2 latest blog posts on my homepage. 我想在首页上显示2条最新博客文章。

However, I want them to appear in 2 separate boxes. 但是,我希望它们出现在2个单独的框中。 This code only shows the same blog post twice. 此代码仅显示同一博客文章两次。 How do I get the second most recent blog post to show in my second box? 如何在第二个框中显示第二个最近的博客帖子?

Any help would be much appreciated :) 任何帮助将非常感激 :)

  <div class="row boxesl"> <div class="c6"> <?php $the_query = new WP_Query( 'posts_per_page=1' ); ?> <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?> <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a> <p><?php the_excerpt(__('(more…)')); ?></p> <?php endwhile; wp_reset_postdata(); ?> </div> <div class="c6 last"> <?php $the_query = new WP_Query( 'posts_per_page=1' ); ?> <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?> <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a> <p><?php the_excerpt(__('(more…)')); ?></p> <?php endwhile; wp_reset_postdata(); ?> </div> 

You can use offset parameter to arguments. 您可以将offset参数用作参数。

<div class="row boxesl">
        <div class="c6">


        <?php $the_query = new WP_Query( 'posts_per_page=1' ); ?>

        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

       <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
             <p><?php the_excerpt(__('(more…)')); ?></p>

       <?php endwhile; wp_reset_postdata(); ?>

              </div>




  <div class="c6 last">

        <?php $the_query = new WP_Query( 'posts_per_page=1&offset=1' ); ?>

        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

       <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
             <p><?php the_excerpt(__('(more…)')); ?></p>

       <?php endwhile; wp_reset_postdata(); ?>


      </div>   

https://codex.wordpress.org/Class_Reference/WP_Query https://codex.wordpress.org/Class_Reference/WP_Query

Try Below Code : 尝试以下代码:

<div class="row boxesl">                     
<?php 
    $the_query = new WP_Query( array(
                                        'post_status' => 'publish',
                                        'orderby' => 'publish_date',
                                        'order' => 'DESC',
                                        'posts_per_page' => 2) 
                             ); 
    if($the_query->have_posts())
    {
        $cnt=1;
        while ($the_query -> have_posts()) : $the_query -> the_post(); 
            if($cnt==2)
                $class=" last";
            else
                 $class="";
?>
        <div class="c6 <?php echo $class;?>"> 
        <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
        <p><?php the_excerpt(__('(more…)')); ?></p>
        </div>
<?php 
        $cnt++;
        endwhile; 
        wp_reset_postdata(); 
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM