自定义帖子未在归档页面上分页并返回404

时间:2012-08-03 作者:Osu

我拼命想让我的自定义post-type归档页面上的分页正常工作,但每次单击时都会出现404错误<?php next_posts_link(\'&laquo; Older Entries\') ?> 对于结果的p2。

以下是我的情况:

我创建了一个名为“archive bvdirectory”的文件。php,用于我的自定义帖子类型“bvdirectory”(请参阅下面我如何在我的functions.php中进行设置)和“single bvdirectory”。php’用于查看单个自定义帖子,我可以很好地查看这两个帖子

  • 我已经删除了我在阅读时永久创建的名为“目录”的页面,这可能会导致WP的重写规则出现问题(URL应该是相同的,这样才有意义)
  • 我已经无限刷新了我的永久链接,但它没有任何更改
  • 是否有人发现我的代码有问题,或者建议我尝试一下?

    感谢您抽出时间,

    Osu

    CTP and taxonomies are set up like this:

    function osu_bv_company() {
    
        // Create Directory custom post type
        register_post_type(
            \'bvdirectory\',
            array(
                \'label\'                 => __( \'Directory\' ),
                \'public\'                => true,
                \'has_archive\'           => \'directory\',
                \'show_ui\'               => true,
                \'query_var\'             => true,
                \'supports\'              => array(
                    \'title\',
                    \'editor\',
                    \'author\',
                    \'excerpt\',
                    \'post-thumbnails\',
                    \'custom-fields\',
                    \'revisions\',
                    \'page-attributes\',
                    \'thumbnail\'
                    // \'trackbacks\',
                    // \'comments\'
                ),
                \'rewrite\' => array(\'slug\'=>\'directory\')
            )
        );
    
        // Create Industry taxonomy
        $bvindustrylabels = array(
            \'name\'                      => \'Industry\',
            \'singular_name\'             => \'Industry\',
            \'search_items\'              => \'Search industries\',
            \'all_items\'                 => \'All industries\',
            \'parent_item\'               => \'Parent industry\',
            \'edit_item\'                 => \'Edit industry\',
            \'update_item\'               => \'Update industry\',
            \'add_new_item\'              => \'Add new industry\',
            \'new_item_name\'             => \'New industry name\',
            \'choose_from_most_used\'     => \'Choose from most used industries\'
        );
        register_taxonomy(
            \'bvindustry\',
            \'bvdirectory\',
            array(
                \'hierarchical\' => true,
                \'labels\' => $bvindustrylabels,
                \'sort\' => true,
                \'args\' => array(\'orderby\' => \'term_order\'),
                \'rewrite\' => array(\'slug\' => \'industry\')
            )
        );
        // Create Company Size taxonomy
        $bvcosizelabels = array(
            \'name\'                      => \'Company size\',
            \'singular_name\'             => \'Company size\',
            \'search_items\'              => \'Search company sizes\',
            \'all_items\'                 => \'All company sizes\',
            \'parent_item\'               => \'Parent company size\',
            \'edit_item\'                 => \'Edit company size\',
            \'update_item\'               => \'Update company size\',
            \'add_new_item\'              => \'Add new company size\',
            \'new_item_name\'             => \'New company name\',
            \'choose_from_most_used\'     => \'Choose from most used company sizes\'
        );
        register_taxonomy(
            \'bvsize\',
            \'bvdirectory\',
            array(
                \'hierarchical\' => true,
                \'labels\' => $bvcosizelabels,
                \'sort\' => true,
                \'args\' => array(\'orderby\' => \'term_order\'),
                \'rewrite\' => array(\'slug\' => \'size\')
            )
        );
    }
    add_action(\'init\', \'osu_bv_company\');
    

    I have the following arguments set for my loop in archive-bvdirectory.php

    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
    $args = array(
        // General args
        \'post_type\'             =>  \'bvdirectory\',
        \'posts_per_page\'        =>  2,
        \'orderby\'               =>  \'title\', // set this in form
        \'order\'                 =>  \'ASC\', // Set this in form
        \'paged\'                 =>  $paged  
    );
    
    // Filter posts
    query_posts($args);
    

    1 个回复
    SO网友:Milo

    您的错误很可能get_query_var(\'paged\') 应该是get_query_var(\'page\').

    但是

    你应该避免打电话query_posts 在模板中,这是对资源的浪费,因为您只是丢弃已经发生的原始查询。

    pre_get_posts 要在查询发生之前更改查询,请执行以下操作:

    function wpa60728_pre_get_posts( $query ) {
        if ( \'bvdirectory\' == get_post_type() && is_main_query() )
            $query->set( \'posts_per_page\', 2 );
    }
    
    add_action( \'pre_get_posts\', \'wpa60728_pre_get_posts\' );
    

    结束

    相关推荐

    How to use pagination?

    我有一个页面的以下代码,在这里我拉一张照片,标题和最后10篇文章的摘录。问题是,我不知道如何添加分页?我也安装了WP PageNavi。我已经看到一些类似的问题得到了解决,但我根本无法真正理解这个概念,我首先尝试确保这是可能的?<?php /* Template Name: Opinion */ ?> <?php get_header(); ?> <div id=\"primary\"> <div id=\"

    自定义帖子未在归档页面上分页并返回404 - 小码农CODE - 行之有效找到问题解决它

    自定义帖子未在归档页面上分页并返回404

    时间:2012-08-03 作者:Osu

    我拼命想让我的自定义post-type归档页面上的分页正常工作,但每次单击时都会出现404错误<?php next_posts_link(\'&laquo; Older Entries\') ?> 对于结果的p2。

    以下是我的情况:

    我创建了一个名为“archive bvdirectory”的文件。php,用于我的自定义帖子类型“bvdirectory”(请参阅下面我如何在我的functions.php中进行设置)和“single bvdirectory”。php’用于查看单个自定义帖子,我可以很好地查看这两个帖子

  • 我已经删除了我在阅读时永久创建的名为“目录”的页面,这可能会导致WP的重写规则出现问题(URL应该是相同的,这样才有意义)
  • 我已经无限刷新了我的永久链接,但它没有任何更改
  • 是否有人发现我的代码有问题,或者建议我尝试一下?

    感谢您抽出时间,

    Osu

    CTP and taxonomies are set up like this:

    function osu_bv_company() {
    
        // Create Directory custom post type
        register_post_type(
            \'bvdirectory\',
            array(
                \'label\'                 => __( \'Directory\' ),
                \'public\'                => true,
                \'has_archive\'           => \'directory\',
                \'show_ui\'               => true,
                \'query_var\'             => true,
                \'supports\'              => array(
                    \'title\',
                    \'editor\',
                    \'author\',
                    \'excerpt\',
                    \'post-thumbnails\',
                    \'custom-fields\',
                    \'revisions\',
                    \'page-attributes\',
                    \'thumbnail\'
                    // \'trackbacks\',
                    // \'comments\'
                ),
                \'rewrite\' => array(\'slug\'=>\'directory\')
            )
        );
    
        // Create Industry taxonomy
        $bvindustrylabels = array(
            \'name\'                      => \'Industry\',
            \'singular_name\'             => \'Industry\',
            \'search_items\'              => \'Search industries\',
            \'all_items\'                 => \'All industries\',
            \'parent_item\'               => \'Parent industry\',
            \'edit_item\'                 => \'Edit industry\',
            \'update_item\'               => \'Update industry\',
            \'add_new_item\'              => \'Add new industry\',
            \'new_item_name\'             => \'New industry name\',
            \'choose_from_most_used\'     => \'Choose from most used industries\'
        );
        register_taxonomy(
            \'bvindustry\',
            \'bvdirectory\',
            array(
                \'hierarchical\' => true,
                \'labels\' => $bvindustrylabels,
                \'sort\' => true,
                \'args\' => array(\'orderby\' => \'term_order\'),
                \'rewrite\' => array(\'slug\' => \'industry\')
            )
        );
        // Create Company Size taxonomy
        $bvcosizelabels = array(
            \'name\'                      => \'Company size\',
            \'singular_name\'             => \'Company size\',
            \'search_items\'              => \'Search company sizes\',
            \'all_items\'                 => \'All company sizes\',
            \'parent_item\'               => \'Parent company size\',
            \'edit_item\'                 => \'Edit company size\',
            \'update_item\'               => \'Update company size\',
            \'add_new_item\'              => \'Add new company size\',
            \'new_item_name\'             => \'New company name\',
            \'choose_from_most_used\'     => \'Choose from most used company sizes\'
        );
        register_taxonomy(
            \'bvsize\',
            \'bvdirectory\',
            array(
                \'hierarchical\' => true,
                \'labels\' => $bvcosizelabels,
                \'sort\' => true,
                \'args\' => array(\'orderby\' => \'term_order\'),
                \'rewrite\' => array(\'slug\' => \'size\')
            )
        );
    }
    add_action(\'init\', \'osu_bv_company\');
    

    I have the following arguments set for my loop in archive-bvdirectory.php

    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
    $args = array(
        // General args
        \'post_type\'             =>  \'bvdirectory\',
        \'posts_per_page\'        =>  2,
        \'orderby\'               =>  \'title\', // set this in form
        \'order\'                 =>  \'ASC\', // Set this in form
        \'paged\'                 =>  $paged  
    );
    
    // Filter posts
    query_posts($args);
    

    1 个回复
    SO网友:Milo

    您的错误很可能get_query_var(\'paged\') 应该是get_query_var(\'page\').

    但是

    你应该避免打电话query_posts 在模板中,这是对资源的浪费,因为您只是丢弃已经发生的原始查询。

    pre_get_posts 要在查询发生之前更改查询,请执行以下操作:

    function wpa60728_pre_get_posts( $query ) {
        if ( \'bvdirectory\' == get_post_type() && is_main_query() )
            $query->set( \'posts_per_page\', 2 );
    }
    
    add_action( \'pre_get_posts\', \'wpa60728_pre_get_posts\' );
    

    相关推荐

    Count posts for pagination

    我正在为一个网站分页<;上一页(页码)下一页>很简单,已经完成。但是现在我需要添加一个选择器来直接转到页面(例如:转到第7页),要这样做,我需要知道有多少页面,为此我需要计算在查询中找到了多少帖子。问题是这个网站有太多的帖子(>13.000),查询所有帖子都会减慢页面加载速度,这就像。。。10秒后页面才能加载。显然,这是不可接受的。分页解决了这个问题,因为一次只加载50或100篇文章,但我无法将它们全部计算在内。我可以在不加载的情况下统计某个查询中的帖子吗?或者我可以通过其他方式获得页数吗