$QUERY->HAVE_POST()出错

时间:2017-05-18 作者:Jay Kapoor

我是wordpress新手,在这里,我正在获取post type=Organization的缩略图,请检查我的$args数组是否有问题,我在这里应用分页,但wp查询不起作用,这是我的代码,请给我一个解决方案。

$paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;

    $args = array(
      \'posts_per_page\' => 5,
      \'post_type\' => \'organisation\',
      \'paged\' => $paged,
      \'meta_query\' => array(
          array(
            \'key\'     => \'title\',
            \'value\'   => \'a\',
            \'compare\' => \'LIKE\',
          ),
        ),
         );

  $query = new WP_Query( $args );
        if ( $query->have_posts() ) {
        foreach ( $query as $post ) 
        {
            setup_postdata ( $post ); 
            ?> 
           <div class="col-md-4 org-logo" style="text-align:center;">
              <div class="pr_image">
                <?php
                 if ( has_post_thumbnail($post->ID))
                  {
                    $thumbnail = get_the_post_thumbnail($postid, array( 80, 80)); 
                    echo \'<a href=\'.get_permalink( $postid ).\' title="\'.get_the_title().\'">\'.$thumbnail.\'</a>\';  
                  }
                ?>    
              </div>
              <?php 
                $shortname =  get_post_meta($post->ID,\'short_name\',true); 
                $title =  get_the_title(); 
                 if(!empty($shortname))
                 {
                  echo \'<span>\' .$shortname. \'</span>\';
                 }
                 else
                 {
                  echo \'<span>\' .$title. \'</span>\';
                 }
              ?>
          </div>
        <?php 
            }
          }
     else 
     {
        ?> 
        <div class="alert">Sorry, No organisation found.</div>
        <?php
     }
?>
<?php
  $big = 99999; // need an unlikely integer
  echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, get_query_var(\'paged\') ),
    \'total\' => $the_query->max_num_pages
  ) );



     ?>

1 个回复
SO网友:Aniruddha Gawade

请使用while ( have_posts() ) : the_post(); 而不是foreach.

请参阅循环文档:https://developer.wordpress.org/themes/basics/the-loop/

如果你想获取技术,你需要循环$query->posts 而不是$query.

结束

相关推荐

Add pagination to blog

我想限制每页3个博客条目片段,并包括其他页面的分页。这是我到目前为止的情况,但分页不起作用。 <div id=\"page-content\"> <?php $args = array( \'posts_per_page\' =>3, \'order\'=> \'ASC\', \'orderby\' => \'title\' ); $postslist = get_posts( $args ); forea