为特定类别的帖子提供其自己的永久链接结构返回404

时间:2019-09-26 作者:Praveen Kumar

我试图给一个特定类别的帖子它自己的永久链接。遵循本主题中的步骤(Give specific category its own permalink structure) 但帖子给出了404错误。

已清除缓存,已在“设置”下保存永久链接,但无效。

希望您能帮助解决此问题。

谢谢

这是我的代码:

//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 == "Foodguide" ) {
        $cat_name = strtolower($category[0]->cat_name);
        $permalink = trailingslashit( home_url(\'/\'. $cat_name . \'/\' . $post->ID . \'/\' . $post->post_name .\'/\' ) );
    }
    return $permalink;
}

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 ) && \'foodguide\' === $slug ) {
        $link = home_url( user_trailingslashit( \'/foodguide/\', \'category\' ) );
    }
    return $link;
}

add_action( \'init\', \'custom_rewrite_rules\' );
function custom_rewrite_rules() {
    add_rewrite_rule(
        \'foodguide(?:/page/?([0-9]{1,})|)/?$\',
        \'index.php?category_name=foodguide&paged=$matches[1]\',
        \'top\' // The rule position; either \'top\' or \'bottom\' (default).
    );

    add_rewrite_rule(
        \'foodguide/([^/]+)(?:/([0-9]+))?/?$\',
        \'index.php?category_name=foodguide&name=$matches[1]&page=$matches[2]\',
        \'top\' // The rule position; either \'top\' or \'bottom\' (default).
    );
}

2 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

First off, the add_rewrite_rule() syntax is add_rewrite_rule( \'RegEx pattern\', \'WordPress query string/vars\', \'position/priority - top or bottom\' ).

Now I could see you\'re trying to have the following permalink structure:

Structure: example.com/foodguide/<post ID>/<post slug>
Example:   example.com/foodguide/1/sample-food-guide

But your rewrite rule (the second one in your code) does not have the proper RegEx subpattern for the post ID part in the permalink; hence, the permalink leads to a 404 error page.

And to fix the issue, you can add \\d+/ to the rewrite rule:

add_rewrite_rule(
    \'foodguide/\\d+/([^/]+)(?:/([0-9]+))?/?$\', // <- here, add the \\d+/
    \'index.php?category_name=foodguide&name=$matches[1]&page=$matches[2]\',
    \'top\'
);

Alternatively, in the RegEx pattern, you can "capture" the post ID (i.e. use (\\d+)) and not the post slug, and then in the WordPress query, use the p (i.e. ID) parameter instead of name (i.e. slug):

add_rewrite_rule(
    \'foodguide/(\\d+)/[^/]+(?:/([0-9]+))?/?$\', // capture the \\d+
    // $matches[1] is now the post ID, so use &p= instead of &name=
    \'index.php?category_name=foodguide&p=$matches[1]&page=$matches[2]\',
    \'top\'
);

PS: Don\'t forget to flush the rewrite rules — just visit the permalink settings page.

UPDATE

If you want the post permalink to end with a .html, then:

  1. In custom_permalink(), make the following change:

    // Change this:
    $permalink = trailingslashit( home_url(\'/\'. $cat_name . \'/\' . $post->ID . \'/\' . $post->post_name .\'/\' ) );
    
    // To this:
    $permalink = untrailingslashit( home_url( \'/\'. $cat_name . \'/\' . $post->ID . \'/\' . $post->post_name . \'.html\' ) );
    
  2. In custom_rewrite_rules(), change the second add_rewrite_rule() to this:

    add_rewrite_rule(
        \'^foodguide/(\\d+)/[^/]+\\.html$\',
        \'index.php?category_name=foodguide&p=$matches[1]\',
        \'top\'
    );
    
  3. After the function custom_rewrite_rules() { ... }, add this:

    function cancel_canonical_redirect( $wp ) {
        if ( \'^foodguide/(\\d+)/[^/]+\\.html$\' === $wp->matched_rule ) {
            remove_action( \'template_redirect\', \'redirect_canonical\' );
        }
    }
    add_action( \'parse_request\', \'cancel_canonical_redirect\' );
    

    That would prevent "adding" the ending slash (/) to the permalink URL.

And once again, don\'t forget to flush the permalinks.

Also, I\'ve not added support for paginated requests (i.e. posts having the <!--nextpage--> tag or the Page Break block in Gutenberg); and if you need that, let me know.

UPDATE 2

For paginated requests with this permalink/URL structure:

example.com/foodguide/<post ID>/<post slug>/page-<page number>.html
  1. In custom_rewrite_rules(), change the second add_rewrite_rule() to this:

    add_rewrite_rule(
        \'^foodguide/(\\d+)/[^/]+(?:/page-(\\d+)|)\\.html$\',
        \'index.php?category_name=foodguide&p=$matches[1]&page=$matches[2]\',
        \'top\'
    );
    
  2. Change the cancel_canonical_redirect to (* the RegEx pattern needs to match the one used in the above add_rewrite_rule()):

    function cancel_canonical_redirect( $wp ) {
        if ( \'^foodguide/(\\d+)/[^/]+(?:/page-(\\d+)|)\\.html$\' === $wp->matched_rule ) {
            remove_action( \'template_redirect\', \'redirect_canonical\' );
        }
    }
    add_action( \'parse_request\', \'cancel_canonical_redirect\' );
    
  3. After the above code, add this:

    function custom_wp_link_pages_link( $link, $i ) {
        if ( in_category( \'foodguide\' ) ) {
            $name = get_post_field( \'post_name\' );
            $link = str_replace(
                "{$name}.html/$i/",     // replace example.com/foodguide/<post ID>/<post slug>.html/<page number>/
                "$name/page-{$i}.html", // with example.com/foodguide/<post ID>/<post slug>/page-<page number>.html
                $link
            );
        }
        return $link;
    }
    add_filter( \'wp_link_pages_link\', \'custom_wp_link_pages_link\', 10, 2 );
    

And as always, be sure to flush the permalinks.

SO网友:Gomathi

请从函数中删除代码。php。相反,请执行以下操作:,

步骤1:转到WordPress仪表板中的设置->永久链接(请参阅此链接)。enter image description here

步骤2:选择Custom Structure 在永久链接设置页面中(请参阅此链接)。enter image description here

步骤3:将永久链接更改为我们的需求,然后单击保存更改按钮(请参阅此链接)。enter image description here

第4步:然后文章的永久链接被更改,正如我们在设置页面中给出的那样(请参阅此链接)。enter image description here

相关推荐

Wordpress Permalinks

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