首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >赋予特定类别它自己的permalink结构

赋予特定类别它自己的permalink结构
EN

WordPress Development用户
提问于 2018-03-13 21:35:38
回答 2查看 2.5K关注 0票数 3

为了我的网站的目的,我把我的Permalinks设置为/blog/%postname%/所有的帖子。

但是,我需要为具有特定类别(在本例中为“推荐”)的帖子提供自己的永久链接结构,其中每个帖子都将类别“推荐”返回为/testimonials/%postname%/,类别存档页面返回为/testimonials/

下面是我到目前为止掌握的代码:

代码语言:javascript
复制
//Rewrite URLs for "testimonial" category
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
    // Get the category for the post
    $category = get_the_category($post->ID);
    if (  !empty($category) && $category[0]->cat_name == "Testimonials" ) {
        $cat_name = strtolower($category[0]->cat_name);
        $permalink = trailingslashit( home_url('/'. $cat_name . '/' . $post->post_name .'/' ) );
    }
    return $permalink;
}

add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
    add_rewrite_rule(
        'testimonials/([^/]+)(?:/([0-9]+))?/?这成功地返回了类别为“/testimonials/%postname%/”的每个个人帖子。但是,类别存档页以/blog/category/testimonials的形式返回。我需要它以/testimonials/的形式返回,同时还以/blog/%postname%/的形式返回所有其他帖子我尝试使用这个插件,https://wordpress.org/plugins/custom-permalinks/,它解决了分类存档页面问题,但打破了每个单独的证明帖子,返回作为一个404错误。在这种情况下,我不能将推荐信注册为自定义Post类型,因为它会影响其他站点的功能,,
        'index.php?category_name=testimonials&name=$matches[1]&page=$matches[2]',
        'top' // The rule position; either 'top' or 'bottom' (default).
    );
}

这成功地返回了类别为“D4”的每个个人帖子。但是,类别存档页以D5的形式返回。我需要它以D6的形式返回,同时还以D7的形式返回所有其他帖子

我尝试使用这个插件,C8,它解决了分类存档页面问题,但打破了每个单独的证明帖子,返回作为一个404错误。

在这种情况下,我不能将推荐信注册为自定义Post类型,因为它会影响其他站点的功能,

EN

回答 2

WordPress Development用户

回答已采纳

发布于 2018-03-13 23:09:15

尝试以下步骤:

Step #1:替换如下:

代码语言:javascript
复制
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
    add_rewrite_rule(
        'testimonials/([^/]+)(?:/([0-9]+))?/?..with这个:add_filter( 'category_link', 'custom_category_permalink', 10, 2 );
function custom_category_permalink( $link, $cat_id ) {
    $slug = get_term_field( 'slug', $cat_id, 'category' );
    if ( ! is_wp_error( $slug ) && 'testimonials' === $slug ) {
        $link = home_url( user_trailingslashit( '/testimonials/', 'category' ) );
    }
    return $link;
}

add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
    add_rewrite_rule(
        'testimonials(?:/page/?([0-9]{1,})|)/?Step 2:转到Permalink设置页面,单击Settings按钮,而不实际进行任何更改。,
        'index.php?category_name=testimonials&name=$matches[1]&page=$matches[2]',
        'top' // The rule position; either 'top' or 'bottom' (default).
    );
}..with这个:A1Step 2:转到Permalink设置页面,单击Settings按钮,而不实际进行任何更改。,
        'index.php?category_name=testimonials&paged=$matches[1]',
        'top' // The rule position; either 'top' or 'bottom' (default).
    );

    add_rewrite_rule(
        'testimonials/([^/]+)(?:/([0-9]+))?/?Step 2:转到Permalink设置页面,单击Settings按钮,而不实际进行任何更改。,
        'index.php?category_name=testimonials&name=$matches[1]&page=$matches[2]',
        'top' // The rule position; either 'top' or 'bottom' (default).
    );
}

..with这个:

A1

Step 2:转到Permalink设置页面,单击Settings按钮,而不实际进行任何更改。

, 'index.php?category_name=testimonials&name=$matches[1]&page=$matches[2]', 'top' // The rule position; either 'top' or 'bottom' (default). ); }

Step 2:转到Permalink设置页面,单击Settings按钮,而不实际进行任何更改。

, 'index.php?category_name=testimonials&name=$matches[1]&page=$matches[2]', 'top' // The rule position; either 'top' or 'bottom' (default). ); }

..with这个:

A1

Step 2:转到Permalink设置页面,单击Settings按钮,而不实际进行任何更改。

票数 6
EN

WordPress Development用户

发布于 2018-03-14 06:12:48

那为什么不直接添加post id呢?如果这就是你所需要的?你只要用query_vars就行了

代码语言:javascript
复制
add_filter('query_vars', 'my_query_vars' );

您只需检查是否属于“特殊”类别,如果是,则添加var。然后,使用pre_get_posts钩子,检查您的帖子是否包含ID,如果有,则更改查询以获得所需的帖子。

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/296699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档