簡體   English   中英

輕松進行數字下載和分頁

[英]Easy Digital Downloads and Pagination

我正在嘗試使用我網站上的自定義下載頁面進行分頁。 我使用Easy Digital Downloads作為我的商店后端,並在自定義wp_query中調用商店商品。 我使用相同的代碼來對檔案進行分頁,但是由於某些原因,它在這里不起作用。 我將不勝感激可以提供的任何幫助。 這是我的代碼:

從我的functions.php文件

/* Pagination numbers function 
------------------------------------
Allows Google-like page numbers to post pages by calling function */

function custom_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 '<div class="navigation"><ul>' . "\n";

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

    /** 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() );

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

}

我的查詢

<?php

  // Allow Pagination
  if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
  elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
  else { $paged = 1; }

  // Arguments
  $type = 'download';
  $args=array(
    'post_type' => $type,
    'posts_per_page' => -1,
    'posts_per_page' => 16,
    'paged' => $paged
  );

  // Get results
  $my_download_query = new WP_Query($args);

  // The Loop
  if( $my_download_query->have_posts() ) {
    while ($my_download_query->have_posts()) : $my_download_query->the_post(); ?>

      <div id="store-item">

        <?php
    // Check for thumbnail 
    if ( has_post_thumbnail() ) {
      echo '<a href="' . the_permalink() . '" title="' . the_title_attribute() . '" >';
        echo the_post_thumbnail('store-thumb');
      echo '</a>';
        }?>

        <?php
          $content = get_the_content();
          echo string_limit_words($content,20) . '...';
        ?>

    <!-- Get Price -->
    <p class="price"><?php echo edd_price_range( $download_id ); ?></p>

    <div class="call-to-action-button">
         <a type="button" href="<?php the_permalink()?>">Details</a>
    </div>

    <?php edit_post_link(); ?> 

      </div> <!-- END #store-item -->

    <?php endwhile; ?>

    <!-- Call custom numbered pages -->
    <?php custom_numeric_posts_nav(); ?>
    <div class="navigation"><p><?php posts_nav_link(); ?></p></div>

  <?php } ?> <!-- endif -->

?>

這是因為里面custom_numeric_posts_nav()你使用全局變量$wp_query ,而$my_download_query在您的自定義查詢。

您可以更改custom_numeric_posts_nav()以接受變量$my_download_query作為可選參數,如果傳遞了該變量,則使用該變量:

function custom_numeric_posts_nav( $query = '' ) {

    if(!$query)
        $query = $GLOBALS['wp_query'];

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

    // ... and change the rest of the function accordingly ...

}

而當您調用它時:

<!-- Call custom numbered pages -->
<?php custom_numeric_posts_nav( $my_download_query ); ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM