从某些类别中断中排除帖子自定义分页

时间:2014-08-05 作者:tepkenvannkorn

我只想显示某些类别的帖子,以便在我的WP_Query() 查询执行此操作时,自定义分页无法正常工作。单击分页本身中的每个数字仍然显示相同的帖子。

我的自定义分页:

function glean_numeric_posts_nav() {

  if( is_singular() )
    return;

  global $wp_query;

  /** Stop execution if there\'s only 1 page */
  if( $wp_query->max_num_pages <= 1 )
    return;

  $paged = get_query_var( \'paged\' ) ? absint( get_query_var( \'paged\' ) ) : 1;
  $max   = intval( $wp_query->max_num_pages );

  /** Add current page to the array */
  if ( $paged >= 1 )
    $links[] = $paged;

  /** Add the pages around the current page to the array */
  if ( $paged >= 3 ) {
    $links[] = $paged - 1;
    $links[] = $paged - 2;
  }

  if ( ( $paged + 2 ) <= $max ) {
    $links[] = $paged + 2;
    $links[] = $paged + 1;
  }

  echo \'<ul class="pagination">\' . "\\n";

  /** Previous Post Link */
  if ( get_previous_posts_link() )
    printf( \'<li>%s</li>\' . "\\n", get_previous_posts_link(\'&laquo;\') );

  /** Link to first page, plus ellipses if necessary */
  if ( ! in_array( 1, $links ) ) {
    $class = 1 == $paged ? \' class="active"\' : \'\';

    printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( 1 ) ), \'1\' );

    if ( ! in_array( 2, $links ) )
      echo \'<li>…</li>\';
  }

  /** Link to current page, plus 2 pages in either direction if necessary */
  sort( $links );
  foreach ( (array) $links as $link ) {
    $class = $paged == $link ? \' class="active"\' : \'\';
    printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
  }

  /** Link to last page, plus ellipses if necessary */
  if ( ! in_array( $max, $links ) ) {
    if ( ! in_array( $max - 1, $links ) )
      echo \'<li>…</li>\' . "\\n";

    $class = $paged == $max ? \' class="active"\' : \'\';
    printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
  }

  /** Next Post Link */
  if ( get_next_posts_link() )
    printf( \'<li>%s</li>\' . "\\n", get_next_posts_link(\'&raquo;\') );

  echo \'</ul>\' . "\\n";

}
在我的博客页面中,我使用

$the_query = new WP_Query(\'cat=-10\');
然后循环它们。接下来在循环之后,我调用glean_numeric_posts_nav() 作用

1 个回复
SO网友:Pieter Goosen

您需要复制的每个实例$wp_query 并将其更改为$the_query. 实例

global $wp_query, $the_query;

  /** Stop execution if there\'s only 1 page */
  if( $wp_query->max_num_pages <= 1 || $the_query->max_num_pages <= 1 )
     return;

EDIT

这是我使用的分页函数。现在没有时间编写代码或真正深入研究代码。相应更改。$cat_query$cpt_query 是用于自定义查询的变量

function pietergoosen_pagination($pages = \'\', $range = 2) {   
    $showitems = ($range * 2)+1;  

    global $paged;
    if(empty($paged)) $paged = 1;

    if($pages == \'\') {
        global $wp_query, $cat_query, $cpt_query;
        if(is_page_template( \'page-pop.php\' )) {
            $pages = $cat_query->max_num_pages;
            if(!$pages) {
                $pages = 1;
            }
        }elseif(is_page_template( \'page-cpt.php\' )) {
            $pages = $cpt_query->max_num_pages;
            if(!$pages) {
                $pages = 1;
            }
        }else{
            $pages = $wp_query->max_num_pages;
            if(!$pages) {
                $pages = 1;
            }
        }   
    }   

    if(1 != $pages) {
        $string = _x( \'Page %1$s of %2$s\' , \'%1$s = current page, %2$s = all pages\' , \'pietergoosen\' );
        echo "<div class=\'pagination\'><span>" . sprintf( $string, $paged, $pages ) . "</span>";
        if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link(1)."\'>" . __( \'&laquo; First\', \'pietergoosen\' ) . "</a>";
        if($paged > 1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link($paged - 1)."\'>" . __( \'&lsaquo; Previous\', \'pietergoosen\' ) . "</a>";

        for ($i=1; $i <= $pages; $i++) {
            if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
                echo ($paged == $i)? "<span class=\\"current\\">".$i."</span>":"<a href=\'".get_pagenum_link($i)."\' class=\\"inactive\\">".$i."</a>";
            }
        }

        if ($paged < $pages && $showitems < $pages) echo "<a href=\'" . get_pagenum_link($paged + 1)."\'>" . __( \'Next &rsaquo;\', \'pietergoosen\' ) . "</a>";
        if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>" . __( \'Last &raquo;\', \'pietergoosen\' ) . "</a>";
        echo "</div>\\n";
    }
}

结束

相关推荐

Show Pages in Categories

通过将此代码添加到函数中,我创建了category函数。php:function page_category() { register_taxonomy_for_object_type(\'category\', \'page\'); } // Add to the admin_init hook of your theme functions.php file add_action( \'init\', \'page_category\' ); 但问