自定义分类和自定义POST类型-分页通向404

时间:2017-11-04 作者:Torben

我有一个与此设置一起引入的自定义帖子类型:

$args = array(
            \'label\'                 => __( \'Location\', \'genesis\' ),
            \'description\'           => __( \'Locations\', \'genesis\' ),
            \'labels\'                => $labels,
            \'supports\'              => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'genesis-cpt-archives-settings\'),
            \'taxonomies\'            => array( \'location\', \'type_of_location\', \'post_tag\' ),
            \'hierarchical\'          => true,
            \'public\'                => true,
            \'show_ui\'               => true,
            \'show_in_menu\'          => true,
            \'menu_position\'         => 5,
            \'show_in_admin_bar\'     => true,
            \'show_in_nav_menus\'     => true,
            \'can_export\'            => true,
            \'has_archive\'           => \'location\',
            \'exclude_from_search\'   => false,
            \'publicly_queryable\'    => true,
            \'capability_type\'       => \'page\',
            \'rewrite\'       => array(\'slug\' => \'location/%locations%\',\'with_front\' => false),
        );
        register_post_type( \'location\', $args );
此外,我还使用此命令向此自定义帖子类型添加了三个分类法:

register_taxonomy( \'kind_of_location\', \'location\', array(
        \'hierarchical\' => true,
        \'labels\' => $labels,
        \'query_var\' => true,
        \'show_admin_column\' => true,
        \'rewrite\' => array(
            \'slug\' => \'location/kind\', // This controls the base slug that will display before each term
        ),
    ) );
然后,我添加了此重写逻辑,以替换我的CPT URL设置中的%locations%:

add_filter(\'post_link\', \'category_permalink_locations\', 1, 3);
add_filter(\'post_type_link\', \'category_permalink_locations\', 1, 3);

function category_permalink_locations($permalink, $post_id, $leavename) {
    if (strpos($permalink, \'%locations%\') === FALSE) return $permalink;
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, \'kind_of_location\');
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
            $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = \'sonstiges\';

    return str_replace(\'%locations%\', $taxonomy_slug, $permalink);
}
除非我在我的taxanomy归档页面上,点击链接查看之前的帖子,否则这一切都很好——然后我会得到一个404。

我的设置中一定有问题;-)有人知道为什么吗?

谢谢

1 个回复
最合适的回答,由SO网友:Torben 整理而成

我自己解决了-我必须注册分类法BEFORE 帖子类型。。。

结束

相关推荐