【问题标题】:Wordpress - Custom Taxonomy PaginationWordpress - 自定义分类法分页
【发布时间】:2012-06-21 16:30:56
【问题描述】:

我正在使用:

  • Wordpress 3.4
  • WP-PageNavi 2.82

我注册了自定义分类法(目录

<?php

add_action('init', 'pca_register_taxonomy', 0);

function pca_register_taxonomy()
{
    register_taxonomy('catalog', null,
       array(
            'label' => __('Catalogs', __),
            'labels' => array(
                'name' => __('Catalogs', __),
                'singular_name' => __('Catalog', __),
                'search_items' => __('Search Catalogs', __),
                'popular_items' => __('Popular Catalogs', __),
                'all_items' => __('All Catalogs', __),
                'parent_item' => __('Parent Catalog', __),
                'parent_item_colon' => __('Parent Catalog', __),
                'edit_item' => __('Edit Catalog', __),
                'update_item' => __('Update Catalog', __),
                'add_new_item' => __('Add New Catalog', __),
                'new_item_name' => __('New Catalog Name', __),
                'separate_items_with_commas' => __('Separate catalogs with commas', __),
                'add_or_remove_items' => __('Add or remove catalogs', __),
                'choose_from_most_used' => __('Choose from the most used catalogs', __),
                'menu_name' => __('Catalogs', __)
            ),
            'public' => true,
            'show_in_nav_menus' => true,
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true,
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'catalog',
                'with_front' => true,
                'hierarchical' => true
            ),
            'capabilities' => array(
                'manage_terms',
                'edit_terms',
                'delete_terms',
                'assign_terms'
            )
        )
    );
}

?>

我注册了自定义帖子类型(产品

<?php

add_action('init', 'pca_register_post_type');

function pca_register_post_type()
{
    register_post_type('product',
        array(
            'label' => __('Products', __),
            'labels' => array(
                'name' => __('Products', __),
                'singular_name' => __('Product', __),
                'add_new' => __('Add New', __),
                'add_new_item' => __('Add New Product', __),
                'edit_item' => __('Edit Product', __),
                'new_item' => __('New Product', __),
                'all_items' => __('All Products', __),
                'view_item' => __('View Product', __),
                'search_items' => __('Search Products', __),
                'not_found' =>  __('No products found', __),
                'not_found_in_trash' => __('No products found in Trash', __), 
                'parent_item_colon' => '',
                'menu_name' => __('Products', __)
            ),
            'description' => '',
            'public' => true,
            'exclude_from_search' => false,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_nav_menus' => true,
            'show_in_menu' => true,
            'show_in_admin_bar' => true,
            'menu_position' => 20,
            'capability_type' => 'post',
            'meta_cap' => true,
            'hierarchical' => false,
            'supports' => array(
                'title',
                'editor',
                'thumbnail',
                'page-attributes',
                'post-formats'
            ),
            'taxonomies' => array('catalog'),
            'has_archive' => false,
            'rewrite' => array(
                'slug' => 'products',
                'with_front' => true,
                'feeds' => false,
                'pages' => true
            ),
            'query_var' => true,
            'can_export' => true
        )
    );
}

?>

然后我为税收创建一个新文件 -> taxonomy-catalog.php

在这个文件中,我从指定目录(tax)查询所有产品(自定义帖子类型):

<?php

$paged = get_query_var('paged');
$paged = ($paged) ? $paged : 1;

$products = new WP_Query(array(
    'catalog' => $catalog_data->slug, // $catalog_data is the current taxonomy (woman)
    'post_type' => 'product',
    'posts_per_page' => 12,
    'paged' => $paged
));

?>

<?php while ($products->have_posts()) : $products->the_post(); ?>
// Show title, content ... everything ok
<?php endwhile; ?>

<?php if (function_exists('wp_pagenavi')) wp_pagenavi(array('query' => $products)); ?>
<?php wp_reset_postdata(); ?>

分页显示正确,但是当我点击第 2 页或更多页面时出现 404 错误。

  • 作品 -> mywebsite.com/catalog/woman
  • 不工作 -> mywebsite.com/catalog/woman/page/2

我已经刷新了固定链接。

有什么办法解决这个问题吗?谢谢

【问题讨论】:

  • 您的问题解决了吗?如果是,请分享。谢谢

标签: wordpress pagination taxonomy


【解决方案1】:

基本上就是这样

get_query_var('page') 

保存页码

来自工作代码:

global $query_string;

$page_index = max(1, get_query_var('page'));

parse_str($query_string, $queryParams);
$queryParams['paged'] = $page_index;
$queryParams['posts_per_page'] = $posts_per_page;
query_posts($queryParams);

【讨论】:

【解决方案2】:

使用 query_posts 代替 wp_query。它应该可以工作。

这是我的代码。

  <?php             

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

             $args = array(

            'paged' => $paged,

            'orderby' => 'asc',

                'tax_query' => array(

                    array(

                        'taxonomy' => "$term->taxonomy",

                        'terms' => "$term->name",

                        'field' => 'slug'  

                    )

                )

            );

            $query = new WP_Query( $args );
            if($query->have_posts() ) :   
// your code
?>  

<?php endwhile; endif; 
             wp_pagenavi();?>

【讨论】:

    【解决方案3】:

    将其放入您的“functions.php”中,然后重新生成永久链接。 它对我有用!

    function taxonomy_rewrite_fix($wp_rewrite) {
        $r = array();
        foreach($wp_rewrite->rules as $k=>$v){
            $r[$k] = str_replace('catalog=$matches[1]&paged=','catalog=$matches[1]&page=',$v);
        }
        $wp_rewrite->rules = $r;
    }
    add_filter('generate_rewrite_rules', 'taxonomy_rewrite_fix');
    

    关键是将“paged”替换为“page”到自定义分类的重写规则中。

    这是我在这里的第一个贡献。希望能帮到你。

    【讨论】:

    • Daymn,你拯救了我的 DAYS。但是让我建议也许更好的解决方案,我后来想出了一点: add_action('init', function() { add_rewrite_rule( '^catalog/(.+)/page/?([0-9]{1,}) /?$', 'index.php?catalog=$matches[1]&page=$matches[2]', 'top' ); });
    • 给他一枚勋章!
    【解决方案4】:


    当您创建任何 CPT 时,您需要添加此功能。

    flush_rewrite_rules();
    它将管理/调整/重新分配您的内存。
    所以请添加此行,您的代码将起作用。
    注意事项。
    1: Add this function : flush_rewrite_rules();<br>2:Your pagination function (wp_pagenavi()) will call just below the endwhile;
    我'我确定,您的分页会起作用,如果不起作用,请告诉我。
    感谢您。

    【讨论】:

      【解决方案5】:

      一旦我遇到了这方面的问题并通过拉头发来度过艰难的几个小时。我用谷歌搜索并没有找到关于这些主题的任何具体解决方案。我找到了几个天才的文章,但他们并没有解决我的问题。实际上,自定义分类档案页面的分页取决于相关函数的一些参数设置。所以我实际上是在这里分享我解决分类档案分页问题的想法。

      自定义分类归档页面分页完美运行所需的五件事:

      ( 1 ) 不要将exclude_from_search 参数键作为register_post_type 参数或 如果提到设置它'exclude_from_search' =&gt; false。如果未提及,默认设置为false

      ( 2 ) 将与自定义帖子类型集一起使用的分类法 'taxonomies' =&gt; 'custom_taxonomy_name' 作为 register_post_type 参数参数或
      直接使用register_taxonomy_for_object_type()。 自定义分类法仍然需要在register_taxonomy() 注册。

      (3)new WP_Query ($args)内查询时

      i ) If not set in admin `static front page` use before `new WP_Query($args)` 
      
      $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
       and use $query = new WP_Query( array( 'paged' => $paged ) );
      
      ii ) If set in admin static front page use before 'new WP_Query($args)'
        $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
        and use $query = new WP_Query( array( 'page' => $paged ) );
      

      记得在new WP_Query($arg) 参数数组中使用posts_per_pagepaged 参数。 如果没有设置static front page,那么你应该在new WP_Query ($arg)参数数组中使用page参数

      ( 4 ) 使用如下例的Wordpress paginate_links( $args ) 函数在归档模板文件中呈现分页。

      <?php $big = 999999999; // need an unlikely integer
      echo paginate_links( array(
                        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                        'format' => '?paged=%#%',  or   '/paged=%#%',  // if using pretty permalink
                         'current' => max( 1, get_query_var('paged') ),
                         'total' => $query->max_num_pages ) ); // Here $max_num_pages is the properties of  new WP_Query() object . It is total number of pages. Is the result of $found_posts / $posts_per_page
       ?>
      

      ( 5 ) paginate_links() 函数输出带有page-numbers 类的ul li 列表。 如果您在 javascript 或 jquery 的帮助下使用引导程序将 pagination 类注入到 ul 中,则会输出漂亮的分页。

      希望您现在可以在分类档案模板中享受分页,而不会出现任何 404 问题 :-)

      【讨论】:

      • 我有完全相同的问题,但仍然得到 404 页。
      【解决方案6】:

      帮助我的是在阅读设置中将“博客页面最多显示”设置为 1。然后它就可以工作了。

      使用默认的 10,它会在第 2 页抛出 404 错误。在第 1 页上,一切都很完美。

      因此,鉴于此方法可行,解决方案是将 “博客页面最多显示” 设置为 1,仅针对您需要的那些分类法或类别。您应该在 functions.php 中放置的代码在这里:https://forums.envato.com/t/wordpress-custom-page-type-taxonomy-pagination/76549/12

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-05
        • 2017-04-16
        • 1970-01-01
        • 1970-01-01
        • 2021-07-25
        • 1970-01-01
        • 1970-01-01
        • 2019-05-10
        相关资源
        最近更新 更多