1
我試圖讓我的分頁與我的網站上的自定義下載頁面一起工作。我使用簡易數字下載作爲我的商店後端,並在自定義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 -->
?>
感謝您的答覆。我要試一試,但我想說明一點,我最初使用的是$ wp_query,而不是$ my_download_query。所以,我的後續問題是:如果我將$ my_download_query更改回$ wp_query,該代碼是否應該正常工作?現在,這是行不通的。 – jkulakowski 2015-04-04 22:49:50
現在您正在使用'new WP_Query($ args);'創建另一個查詢。你可以使用'query_posts($ args)'來設置'$ wp_query',但它是[不推薦](http://wordpress.stackexchange.com/a/1755/69071)。 – d79 2015-04-04 23:10:25
明白了!順便說一句,我成功地更新了函數並將其傳遞給$ my_download_query。它完美的工作!非常感謝! – jkulakowski 2015-04-04 23:12:02