【问题标题】:wp_query->max_num_pages always returns 0 on custom post typewp_query->max_num_pages 在自定义帖子类型上始终返回 0
【发布时间】:2020-01-30 16:12:12
【问题描述】:

我的分页出现问题,这似乎源于$wp_query->max_num_pages 返回0

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

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

我有每个类别的页面,并使用循环,我用post-typewpdmpro 循环浏览每个帖子。

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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您应该尽可能避免使用query_posts(),而是选择WP_Query class。像这样的:

    <?php
    // Args
    $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
    $wpdmpro_query = new WP_Query( $args );
    
    // The Loop
    if( $wpdmpro_query->have_posts() ) : while( $wpdmpro_query->have_posts() ) : $wpdmpro_query->the_post();
    ?>
        <!-- Do stuff -->
    
    <?php endwhile; ?>
    
        <!-- Do stuff if there are no posts -->
    
    endif; wp_reset_postdata(); ?>
    

    然后要测试max_num_pages,您将使用查询:

    echo $wpdmpro_query->max_num_pages
    

    希望这会有所帮助!

    【讨论】:

    • 抱歉,我应该提一下,我确实尝试过 WP_Query 课程,但点击 /page/2/ 仍然会给我一个 404,无论我使用什么
    • 用我的永久链接等更新了我的答案。
    • 嗯 - 切换回课程肯定是第 1 步,但您可以尝试删除这两个过滤器并重新保存永久链接设置以刷新重写规则并查看它是否有效。如果没有,请检查以确保 Query 变量(在我的情况下为$wpdmpro_query)包含在分页链接中。 This post may help.
    • 我删除了过滤器并刷新了重写规则,但不幸的是没有运气。此外,不确定检查分页链接中的变量。我一直在使用&lt;?php next_posts_link('&lt;span aria-hidden="true"&gt;&lt;/span&gt; Older Entries') ?&gt;
    • 啊。如果您已切换回课堂,请尝试&lt;?php next_posts_link( '&lt;span aria-hidden="true"&gt;&lt;/span&gt; Older Entries' , $wpdmpro_query-&gt;max_num_pages ); ?&gt;
    【解决方案2】:

    另外...确保您的 WP_Query 参数中没有将 no_found_rows 设置为 true

    【讨论】:

      【解决方案3】:

      此功能可让您找到您拥有的自定义帖子的数量:

      $num_posts = wp_count_posts('your_custom_post_type')->publish;
      

      (参见documentation

      所以,你可以通过这个和posts_per_page值之间的简单操作来获得max_num_pages值:

      $max_num_pages = $num_posts / $your_posts_per_page;
      if ($num_posts % $your_posts_per_page > 0) $max_num_pages++;
      

      【讨论】:

        【解决方案4】:

        以防万一有人偶然发现这一点,我找到了解决方案。

        第一步是创建一个archive.php 页面

        在那里我会访问循环。

        functions.php

        当我点击/category-name/page/2 时,我有一个类别重写过滤器来更新类别重写规则以使用以下示例 url index.php?wpdmcategory=category-name&amp;paged=2

        以前,它使用以下示例 url,index.php?category_name=category-name&amp;paged=2,因此它实际上使用了类别分页的默认帖子类型。

        add_filter( 'category_rewrite_rules', 'vipx_filter_category_rewrite_rules' );`
        
        function vipx_filter_category_rewrite_rules($rules) {
            $categories = get_terms('wpdmcategory', 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?wpdmcategory=$matches[1]&feed=$matches[2]';
                        $rules['('.$slug.
                            ')/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?wpdmcategory=$matches[1]&feed=$matches[2]';
                        $rules['('.$slug.
                            ')(/page/(\d+)/?)?$'] = 'index.php?wpdmcategory=$matches[1]&paged=$matches[3]';
                    }
                }
            }
            return $rules;
        }
        

        【讨论】:

          猜你喜欢
          • 2015-03-07
          • 2018-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多