【发布时间】:2020-01-30 16:12:12
【问题描述】:
我的分页出现问题,这似乎源于$wp_query->max_num_pages 返回0。
我相信这是因为有 0 默认的 wordpress 帖子。
所以,我使用WP Download Manager Pro plugin 创建自定义帖子类型wpdmpro。
我有每个类别的页面,并使用循环,我用post-type 或wpdmpro 循环浏览每个帖子。
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'post_type' => 'wpdmpro',
'wpdmcategory' => $category->category_nicename,
'tag' => $cat_tag
);
query_posts($args)
?>
<?php if (have_posts()) : ?>
/* content goes here */
<?php endif; ?>
<?php else : ?>
<div class="row">
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
</div>
<?php endif; ?>
echo $wp_query->max_num_pages /* returns 0 */
echo $wp_query->found_posts /* returns 0 */
虽然,我实际上在wpdmpropost_type 中有11 的帖子。那么为什么回声不做以下事情呢?
echo $wp_query->max_num_pages /* Should return 2 because I have 11 posts with 10 per page */
echo $wp_query->found_posts /* Shoudl return 11 because I have 11 posts */
尝试以下方法看看是否可行
add_action( 'pre_get_posts', 'action_pre_get_posts' );
function action_pre_get_posts( $q )
{
$q->set('max_num_pages', 20);
}
但是当导航到 page/2/ 时,我仍然得到一个 404 页面。
我点击的网址是/category-name/page/2,它返回一个404。
一定有办法解决这个问题?
如何让我的网站忽略默认的 WordPress 帖子类型并使用我实际定义的 wpdmpro 帖子类型?
编辑:
我也尝试过使用 WP_Query 类,但在导航到 /page/2 时仍然会出现 404 页面
只需添加一些东西,以防它们以某种方式链接。
在我的永久链接设置中,我有一个自定义结构
/%category%/%postname%/
我的默认类别基数是.
在我的 WP 下载管理器设置中,我的 WPDM Category URL Base 是 .
我也有这 2 个过滤器函数,它们是从这里的答案中复制而来的,而且我对 Wordpress 还很陌生,所以我不确定使用这些函数的潜在影响是什么?
add_filter('category_rewrite_rules', 'vipx_filter_category_rewrite_rules');
add_filter('user_trailingslashit', 'remove_category', 100, 2);
function vipx_filter_category_rewrite_rules($rules) {
$categories = get_categories(array('hide_empty' => false));
if (is_array($categories) && !empty($categories)) {
$slugs = array();
foreach($categories as $category) {
if (is_object($category) && !is_wp_error($category)) {
if (0 == $category - > category_parent) {
$slugs[] = $category - > slug;
} else {
$slugs[] = trim(get_category_parents($category - > term_id, false, '/', true), '/');
}
}
}
if (!empty($slugs)) {
$rules = array();
foreach($slugs as $slug) {
$rules['('.$slug.
')/feed/(feed|rdf|rss|rss2|atom)?/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$rules['('.$slug.
')/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$rules['('.$slug.
')(/page/(\d+)/?)?$'] = 'index.php?category_name=$matches[1]&paged=$matches[3]';
}
}
}
return $rules;
}
function remove_category($string, $type) {
if ($type != 'single' && $type == 'category' && (strpos($string, 'category') !== false)) {
$url_without_category = str_replace("/wpdmcategory/", "/", $string);
return trailingslashit($url_without_category);
}
return $string;
}
我还确保没有任何帖子 slug 与类别名称冲突。
【问题讨论】: