自定义帖子类型和自定义分类的固定链接问题

时间:2021-07-08 作者:thegirlinthecafe

问题1永久链接结构

我试图为我的网站实现正确的url/永久链接结构,但我无法让它工作。

我有以下设置:

Custom post type: training

  register_post_type( \'training\',
    array(
        \'labels\' => array(
            \'name\' => __( \'Trainingen\' ),
            \'singular_name\' => __( \'Training\' ),
            \'all_items\'           => __( \'Alle trainingen\' ),
            \'view_item\'           => __( \'Bekijk training\' ),
            \'add_new_item\'        => __( \'Nieuwe training\' ),
            \'add_new\'             => __( \'Nieuwe training\' )
        ),
        \'public\' => true,
        \'has_archive\' => true,
        \'hierarchical\' => false,
        \'rewrite\' => array(\'slug\' => \'our-training\', \'with_front\' => false),
        \'show_in_rest\' => true
        )
    );
**Custom taxonomy : training-category**
    $args = array(
        "label" => ( "training category"),
        "labels" => $labels,
        "rewrite" => array( \'slug\' => \'training-category\', \'with_front\' => true, ),
        "show_admin_column" => true,
        "hierarchical" => false,
        "has_archive" => true,
        "show_ui" => true
        );
    register_taxonomy( "training_category", array( "event", "post", "training", "employee"), $args );
我要建立的permalink结构:

our-training/training-category-1/training-name-1
our-training/training-category-2/training-name-2
our-training/training-category-1/training-name-3
我得到的是:

our-training/training-name-1
our-training/training-name-2
our-training/training-name-3
因此,在permalink中没有正确插入自定义分类法。

(我也在使用Yoast面包屑,并将培训cpt的分类设置为:培训类别(我的自定义税)。)

关于如何获得所需的permalink结构,有什么建议吗?

Question 2 - Redirect

我们的培训/1类培训

必须重定向到:

我们的培训?cat=1类培训

我怎样才能以最好的方式做到这一点?

任何指向正确方向的指针都将受到高度赞赏!

1 个回复
SO网友:nmr

原始答案中的存档页和分页有问题。因此"rewrite" => [\'slug\' => \'our-training/%training-category%\'] 在里面register_post_type()如前所述,添加以下重写规则并更改为CPT创建的链接。

规则将处理以下地址:

显示单个training 支持分页的post

/our-training/training-category-1/training-name/
/our-training/training-category-1/training-name/2/
显示器training 分配给的职位training-category-1 (取消对首次重写规则的注释)note: 这是对分类术语的查询,而不是cpt,因此first available template 将使用列表中的:taxonomy-training_category.php &燃气轮机;taxonomy.php &燃气轮机;archive.php
/our-training/training-category-1/
/our-training/training-category-1/page/2/
默认规则将处理:

全部显示training 帖子(存档)

/our-training/
  • 显示分配给training-category-1
/training-category/training-category-1/
这个代码对我有用。

add_action( \'init\', \'se391612_cpt_permalink\', 20 );
add_filter( \'post_type_link\', \'se391612_term_in_cpt_link\', 20, 4 );

function se391612_cpt_permalink()
{
    //
    // CPT posts from custom taxonomy term (term archive)
    //add_rewrite_rule(
    //    \'our-training/([^/]+)(?:/page/?([0-9]+))?/?$\', 
    //    \'index.php?post_type=training&training_category=$matches[1]&paged=$matches[2]\',
    //    \'top\'
    //);    
    //
    // single post with pagination 
    add_rewrite_rule(
        \'our-training/([^/]+)/([^/]+)(?:/([0-9]+))?/?$\', 
        \'index.php?post_type=training&training=$matches[2]&page=$matches[3]\',
        \'top\'
    );
}

function se391612_term_in_cpt_link( $post_link, $post, $leavename, $sample )
{
    if ( ! $post instanceof \\WP_Post || \'training\' != $post->post_type)
        return $post_link;
    $new_link = $post_link;
    $all_terms = wp_get_post_terms( $post->ID, \'training_category\', [
        \'hide_empty\' => \'0\',
        \'fields\' => \'slugs\',
        \'orderby\' => \'term_id\',
    ] );
    if ( !is_array($all_terms) || 0 == count($all_terms) )
        return $post_link;

    $term = array_shift($all_terms) . \'/\';
    $new_link = str_replace(\'our-training/\', \'our-training/\'.$term, $new_link);

    return $new_link;
}
Important note:
如果取消注释第一个add_rewrite_rule() 支持/our-training/training-category-1 permalink格式,将发生地址冲突。对于WP,这些地址之间没有区别,它如何解释它们取决于重写规则的顺序。

/our-training/training-category-1   // all "training" post from category "training-category-1"
/our-training/some-training-name    // single "training" post
但是,如果您希望这两种地址类型都有效,并且您知道there will be only several categories, 您可以在规则中添加这些类别(手动或自动)。

<小时/>Reference:
register_post_type()
wp_get_post_terms()
post_type_link filter

相关推荐

Wordpress Permalinks

我在最近建立的网站上使用Wordpress Sydney主题。如果我将永久链接更改为post选项,我的网页链接将中断。对于不是技术大师的人来说,有没有简单的方法来解决这个问题。任何(详细的)帮助都将不胜感激。