自定义页面模板上的自定义循环的分页不显示任何内容

时间:2015-07-17 作者:Harman Preet

我已经创建了一个页面,用户可以在其中查看他最喜欢的帖子,该帖子已被他添加为书签

get_user_favorites($user_id);
此函数负责将用户在数组中标记的所有收藏夹帖子返回给我。

我使用这种方法从数据库中获取数据,并使其在自定义Html中工作。

    <?php $user_fav = get_user_favorites($user_id); 
      $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
       $args = array(
        \'posts_per_page\'   => 4,
        \'post_type\'        => array(\'communityposts\',\'post\',\'video\'),
        \'post__in\'         => $user_fav,
        \'post_status\'      => \'publish\',
        \'suppress_filters\' => true ,
        \'paged\'            => $paged
      );
      $posts_array = get_posts( $args );
       foreach ($posts_array as $p) :

    ?>

<article class="post-items-latest">
  <a href="<?php echo get_permalink( $p->ID ); ?>" title="<?php echo $p->post_title; ?>">
    <span class="image-wrapper">

      <?php
        $thumb = get_post_thumbnail_id($p->ID);
        $img_url = wp_get_attachment_url( $thumb,\'full\' ); //get full URL to image (use "large" or "medium" if the images too big)
       ?>
        <img src="<?php echo $img_url; ?>" alt="<?php $p->post_title; ?>" width="226" height="117">

    </span>
    <h1><?php echo $p->post_title; ?></h1>
  </a>
  <div class="post-footer">
    <span class="user"><i class="fa fa-user"></i>&nbsp;<?php echo get_the_author_link(); ?></span>
    <span class="data"><i class="fa fa-clock-o"></i>&nbsp;<?php wp_days_ago_v3(); ?></span>
    <span class="likes"><i class="fa fa-eye"></i>&nbsp;<?php echo $meta_values = get_post_meta( get_the_ID(), \'cv_post_views_count\', true ); ?> </span>
  </div>
</article>

<?php endforeach; ?>


<div class="post-nav-container">
  <?php previous_posts_link( __(\'&rarr; Older Posts\',\'circoviral\')); ?>
  <?php next_posts_link( __(\'&larr; Older Posts\',\'circoviral\')); ?>
</div>
使用这种方法,我能够获得用户喜欢的帖子,但我无法next_posts_link();previous_posts_link(); 在我的模板内工作

我做错什么了吗?

2 个回复
最合适的回答,由SO网友:Harman Preet 整理而成

我的方法也是正确的,但唯一的问题是我们没有在发布导航链接之前重置查询。

以下是最新的答案。

  <?php $user_fav = get_user_favorites($user_id); 
    $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
     $args = array(
      \'posts_per_page\'   => 4,
      \'post_type\'        => array(\'communityposts\',\'post\',\'video\'),
      \'post__in\'         => $user_fav,
      \'post_status\'      => \'publish\',
      \'suppress_filters\' => true,
      \'paged\'            => $paged,
      \'order_by\'         => \'date\',
      \'order\'            => \'ASC\' 
    );
    $wp_query = new WP_Query( $args );

    if( $wp_query->have_posts()):while($wp_query->have_posts()):$wp_query->the_post();

  ?>

  <article class="post-items-latest">
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
      <span class="image-wrapper">

        <?php
            $thumb = get_post_thumbnail_id($post->ID);
            $img_url = wp_get_attachment_url( $thumb,\'full\' ); //get full URL to image (use "large" or "medium" if the images too big)
            $image = aq_resize( $img_url, 226, 117,true,true,true); //resize & crop the image
          ?>

          <?php if(!empty($image)): ?>
            <img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" width="226" height="117">
          <?php else: ?>

            <?php if (has_post_thumbnail()): ?>

          <?php the_post_thumbnail( \'large\'); ?>

            <?php endif; ?>
          <?php endif; ?>

      </span>
      <h1><?php the_title(); ?></h1>
    </a>
    <div class="post-footer">
      <span class="user"><i class="fa fa-user"></i>&nbsp;<?php echo get_the_author_link(); ?></span>
      <span class="data"><i class="fa fa-clock-o"></i>&nbsp;<?php wp_days_ago_v3(); ?></span>
      <span class="likes"><i class="fa fa-eye"></i>&nbsp;<?php echo $meta_values = get_post_meta( get_the_ID(), \'cv_post_views_count\', true ); ?> </span>
    </div>
  </article>

  <?php 
    endwhile;
    endif;
  ?>

  <div class="post-nav-container">
    <?php previous_posts_link( __(\'&rarr; Older Posts\')); ?>
    <?php next_posts_link( __(\'Newer Posts &larr; \')); ?>
  </div>
  <?php wp_reset_query(); ?>

SO网友:terminator

哈曼,不要用get_posts() 什么时候pagination is required. 您还可以使用WP_Query.

请参阅Chip Bennett在

How to fix pagination for custom loops?

    <?php $user_fav = get_user_favorites($user_id); 
      $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
       $args = array(
        \'posts_per_page\'   => 4,
        \'post_type\'        => array(\'communityposts\',\'post\',\'video\'),
        \'post__in\'         => $user_fav,
        \'post_status\'      => \'publish\',
        \'suppress_filters\' => true ,
        \'paged\'            => $paged
      );
      $loop = new WP_Query( $args );
      $temp_query = $wp_query;
      $wp_query   = NULL;
      $wp_query   = $loop;
      if( $loop->have_posts()):while($loop->have_posts()):$loop->the_post(); 
    ?>

<article class="post-items-latest">
  <a href="<?php echo get_permalink( $post->ID ); ?>" title="<?php the_title(); ?>">
    <span class="image-wrapper">

      <?php
        $thumb = get_post_thumbnail_id($post->ID);
        $img_url = wp_get_attachment_url( $thumb,\'full\' ); //get full URL to image (use "large" or "medium" if the images too big)
       ?>
        <img src="<?php echo $img_url; ?>" alt="<?php the_title(); ?>" width="226" height="117">

    </span>
    <h1><?php the_title(); ?></h1>
  </a>
  <div class="post-footer">
    <span class="user"><i class="fa fa-user"></i>&nbsp;<?php echo get_the_author_link(); ?></span>
    <span class="data"><i class="fa fa-clock-o"></i>&nbsp;<?php wp_days_ago_v3(); ?></span>
    <span class="likes"><i class="fa fa-eye"></i>&nbsp;<?php echo $meta_values = get_post_meta( get_the_ID(), \'cv_post_views_count\', true ); ?> </span>
  </div>
</article>

<?php 
endwhile;
endif;
wp_reset_postdata();
?>

<div class="post-nav-container">
  <?php previous_posts_link( __(\'&rarr; Older Posts\')); ?>
  <?php next_posts_link( __(\'Newer Posts &larr; \',$loop->max_num_pages)); ?>
</div>

<?php 
$wp_query = NULL;
$wp_query = $temp_query;
?>

结束

相关推荐

WP_QUERY LOOP ELSE语句未执行

我有两个WP_query 在我的frontpage中循环。php文件:<div id=\"aanbod\"> <div class=\"container-fluid section-name-cont\"> <div class=\"container\"> <div class=\"row\"> <div class=\"col-xs-12 section-name\"