gpt4 book ai didi

php - wp_query->max_num_pages 在自定义帖子类型上总是返回 0

转载 作者:行者123 更新时间:2023-12-02 00:13:11 27 4
gpt4 key购买 nike

我的分页出现问题,似乎是因为 $wp_query->max_num_pages 返回 0

我认为这是因为有 0 个默认的 wordpress 帖子。

所以,我正在使用 WP Download Manager Pro plugin创建自定义帖子类型 wpdmpro

我有每个类别的页面,并使用循环,我使用 wpdmpropost-type 遍历每个帖子。

<?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 */

尽管如此,我实际上在 wpdmpro post_type 中有 11 个帖子。那么为什么 echo 不执行以下操作?

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 与类别名称冲突。

最佳答案

此外...请确保您没有在 WP_Query 参数中将 no_found_rows 设置为 true

关于php - wp_query->max_num_pages 在自定义帖子类型上总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58183593/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com