Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在(WordPress)的主页上的两列中显示最近的两篇博客文章_Php_Wordpress - Fatal编程技术网

Php 在(WordPress)的主页上的两列中显示最近的两篇博客文章

Php 在(WordPress)的主页上的两列中显示最近的两篇博客文章,php,wordpress,Php,Wordpress,我试图在我的主页上显示2篇最新的博客文章 但是,我希望它们出现在两个单独的框中。此代码仅显示同一博客文章两次。如何在第二个框中显示第二篇最新的博客文章 任何帮助都将不胜感激:) 您可以使用offset参数设置参数 <div class="row boxesl"> <div class="c6"> <?php $the_query = new WP_Query( 'posts_per_page=1' ); ?>

我试图在我的主页上显示2篇最新的博客文章

但是,我希望它们出现在两个单独的框中。此代码仅显示同一博客文章两次。如何在第二个框中显示第二篇最新的博客文章

任何帮助都将不胜感激:)



您可以使用
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>   

请尝试以下代码:

<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(); 
    }
?>


非常感谢你!虽然我意识到offset显示了最近的第三篇博文,但当我将offset设置为1时,它成功了。它是否有效?