2016-01-26 2 views
1

Я оставляю новое для разработки темы WordPress. Я пытаюсь показать только первые две последние сообщения на моей домашней странице, но независимо от того, что я делаю, код всегда проходит через все сообщения. Любая помощь приветствуется.Как показывать только первые два сообщения?

index.php:

<?php get_header(); ?> 
<?php $args = array(
    'numberposts' => 10, 
    'offset' => 0, 
    'category' => 0, 
    'orderby' => 'post_date', 
    'order' => 'DESC', 
    'post_type' => 'post', 
    'post_status' => 'draft, publish, future, pending, private', 
    'suppress_filters' => true); 
    $recent_posts = wp_get_recent_posts($args); 
?> 
<?php if(have_posts()) : 
     if(have_posts()) : the_post(); ?> 
      <div class="content"> 
       <div class="container-fluid"> 
        <div class="row"> 
         <div class="col-md-4"> 
          <h2><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h2> 
          <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('banner-image') ?></a> 
         </div> 
         <div class="col-md-4"> 
          <h2><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h2> 
          <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('banner-image') ?></a> 
         </div> 
         <div class="col-md-4"> 
          <div class="pull-right marTop20"> 
           <h4>Most recent posts</h4> 
           <?php $recent_posts = wp_get_recent_posts($args); ?> 
           <?php foreach($recent_posts as $recent){ ?> 
           <ul class="list-unstyled"> 
            <?php echo '<li class="list-item"><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a> </li> '; ?> 
           </ul> 
           <?php } ?> 
          </div> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="col-md-4"> 
          <p><?php echo get_the_excerpt(); ?> 
          <a href="<?php the_permalink(); ?>">Read more</a></p> 
         </div> 
         <div class="col-md-4"> 
          <p><?php echo get_the_excerpt(); ?> 
          <a href="<?php the_permalink(); ?>">Read more</a></p> 
         </div> 
        </div> 
       </div> 
      </div> 
     <?php 
      endif; 
      endif; 
     ?> 

<?php get_footer(); ?> 
+2

Вы пытались установить 2 в ' 'numberposts'' параметра? –

+1

@bhelmet Это для боковой панели, это первые 10 названий сообщений. Теперь я хочу показать первые два сообщения, а также – Raymond

ответ

0

Вы должны перебрать в wp_get_recent_posts возвращаемое значение:

foreach($recent_posts as $recent_post) { 
    // stuff 
} 

Кроме того, установите в аргnumberpost до 2.

1

Используйте этот

$args = array ( 
      'order' => 'DESC', 
      'post_type' => 'post', 
      'post_status' => 'draft, publish, future, pending, private', 
      'posts_per_page' => 2, 
      'orderby' => 'post_date', 
     ); 
$query = new WP_Query($args); 
if ($query->have_posts()) : 
    while ($query->have_posts()) : $query->the_post(); 
    /* Your Code */ 
    endwhile; 
endif ; 
+0

Это работает, но теперь у меня есть новая проблема. Я использую bootstrap в качестве моей рамки, и я показываю их сеткой. Посты отображаются как блокированные, а не встроенные. – Raymond

+0

Я думаю, что это была какая-то проблема css или какое-то изменение класса в bootstrap, поэтому вам нужно проверить свой HTML-код, другие мудрые данные придут, как вы хотите, – Harsh