最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom permalink structure for posts in specific category

programmeradmin0浏览0评论

Hi I'm trying to rewrite permalink structure for posts in one specific category, the structure should be category name - author name - post title When I use the code below, there is no author name in the URL Please, advise me where am I wrong.

add_filter( 'post_link', 'custom_permalink', 'author_link', 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 == "Tips" ) {
    $cat_name = strtolower($category[0]->cat_name);
    $author_nickname = get_user_meta( $author_id, 'nickname', true );
    $permalink = trailingslashit( home_url('/'. $cat_name . '/' . 
$author_nickname . '/' . $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 ) && 'tips' === $slug ) {
    $link = home_url( user_trailingslashit( '/tips/', 'category' ) );
}
return $link;
}

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

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

Hi I'm trying to rewrite permalink structure for posts in one specific category, the structure should be category name - author name - post title When I use the code below, there is no author name in the URL Please, advise me where am I wrong.

add_filter( 'post_link', 'custom_permalink', 'author_link', 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 == "Tips" ) {
    $cat_name = strtolower($category[0]->cat_name);
    $author_nickname = get_user_meta( $author_id, 'nickname', true );
    $permalink = trailingslashit( home_url('/'. $cat_name . '/' . 
$author_nickname . '/' . $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 ) && 'tips' === $slug ) {
    $link = home_url( user_trailingslashit( '/tips/', 'category' ) );
}
return $link;
}

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

add_rewrite_rule(
'tips/\d+/([^/]+)(?:/([0-9]+))?/?$', // <- here, add the \d+/
'index.php?category_name=tips&name=$matches[1]&page=$matches[2]',
'top'
);
}
Share Improve this question asked Feb 16, 2020 at 1:40 MaksimLMaksimL 1211 bronze badge 4
  • I see you use $category[0], what if the post is in the category but it's $category[1] or $category[4]? Note that nicknames can be changed in the admin interface so they aren't good for use in URLs. Also, when you say there is no author in the URL can you be more specific? Do you mean links to that post have the wrong URL? You get the wrong canonical URL? Redirects? It's unclear. Can you fix the indenting on your code? – Tom J Nowell Commented Feb 16, 2020 at 1:48
  • Yes, the author nickname doesn't appear in the URL. Thank you for noticing the category id. – MaksimL Commented Feb 16, 2020 at 2:28
  • Is it possible to use author slug instead of nickname? The slug which remains unchanged if the nickname is changed? Slug of Usernames which cannot be changed – MaksimL Commented Feb 16, 2020 at 2:35
  • I changed author to $author = get_the_author_meta( 'user_url', $user_id, true ); But still no author slug in the permalink structure – MaksimL Commented Feb 16, 2020 at 2:48
Add a comment  | 

1 Answer 1

Reset to default 0

This code works good for me:

 add_filter( 'post_link', 'custom_permalink', 'author_link', 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 == "Tips" ) {
    $cat_name = strtolower($category[0]->cat_name);
    $authordata = get_userdata( $post->post_author );
        $author     = $authordata->user_nicename;
    $permalink = trailingslashit( home_url('/'. $cat_name . '/' . $author . '/' .          
$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 ) && 'tips' === $slug ) {
    $link = home_url( user_trailingslashit( '/tips/', 'category' ) );
}
return $link;
}

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

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

评论列表(0)

  1. 暂无评论