admin管理员组

文章数量:1310221

See: /

By default, when someone sets their permalink structure to:

/%category%/%postname%/

Post URLs appear like so:

/**abc**/some-cool-post/

ABC merely represents the first category of potentially several selected for the post, but unlikely the most appropriate.

I've made it so that my writers can select which category should be most prominent, which is saved as post meta for each post, and can be output with the variable $prominent_category.

I would like to automatically replace %category% with $prominent_category in the slug, without the need of registering an additional taxonomy or doing anything else that might be overkill, so that whatever the author sets as their prominent category, say Pizza, that the new structure for the post appears as so:

/**pizza**/some-cool-post/

Are either category_rewrite_rules or post_link_category the right choice, or should I be going down a different path?


if ( $prominent_category = get_post_meta( get_the_ID(), 'prominent_cat', true ) ) {
echo esc_html( str_replace( ' ', '-', strtolower( $prominent_category ) ) );
}

Full attempt:

add_filter( 'post_link_category', 'prominent_link_category', 10, 3 );
function prominent_link_category( $cat, $cats, $post ) {
if ( $prominent_primary_cat = get_post_meta( get_the_ID(), 'prominent_primary', true ) ) {
$cat = get_term_by( 'name', esc_html( str_replace( ' ', '-', strtolower( $prominent_primary_cat ) ) ), 'category' );
} else {
$category = get_the_category();
$cat = esc_attr( $category[0]->cat_slug );
}
return $cat;
}

See: https://wordpress/support/article/using-permalinks/

By default, when someone sets their permalink structure to:

/%category%/%postname%/

Post URLs appear like so:

https://example/**abc**/some-cool-post/

ABC merely represents the first category of potentially several selected for the post, but unlikely the most appropriate.

I've made it so that my writers can select which category should be most prominent, which is saved as post meta for each post, and can be output with the variable $prominent_category.

I would like to automatically replace %category% with $prominent_category in the slug, without the need of registering an additional taxonomy or doing anything else that might be overkill, so that whatever the author sets as their prominent category, say Pizza, that the new structure for the post appears as so:

https://example/**pizza**/some-cool-post/

Are either category_rewrite_rules or post_link_category the right choice, or should I be going down a different path?


if ( $prominent_category = get_post_meta( get_the_ID(), 'prominent_cat', true ) ) {
echo esc_html( str_replace( ' ', '-', strtolower( $prominent_category ) ) );
}

Full attempt:

add_filter( 'post_link_category', 'prominent_link_category', 10, 3 );
function prominent_link_category( $cat, $cats, $post ) {
if ( $prominent_primary_cat = get_post_meta( get_the_ID(), 'prominent_primary', true ) ) {
$cat = get_term_by( 'name', esc_html( str_replace( ' ', '-', strtolower( $prominent_primary_cat ) ) ), 'category' );
} else {
$category = get_the_category();
$cat = esc_attr( $category[0]->cat_slug );
}
return $cat;
}
Share Improve this question edited Jan 11, 2021 at 15:40 asked Dec 31, 2020 at 9:47 user199677user199677 2
  • 1 Is $prominent_category a global variable; if so, where/when/how is it defined? Or is prominent_category the meta key? But yes, you'd want to use the post_link_category hook to filter the %category% replacement value. – Sally CJ Commented Dec 31, 2020 at 10:11
  • So does the metadata prominent_cat contain the category name (e.g. foo cat), slug (e.g. foo-cat) or ID (e.g. 123)? – Sally CJ Commented Jan 1, 2021 at 1:13
Add a comment  | 

1 Answer 1

Reset to default 3

Yes, the post_link_category hook is indeed what you would use to filter the %category% replacement value in the permalink.

And here's an example with prominent_cat being the meta key and the value being the category name (e.g. Pizza Hut), where I'm using get_term_by() to get the category object/data:

add_filter( 'post_link_category', 'my_post_link_category', 10, 3 );
function my_post_link_category( $cat, $cats, $post ) {
    $prominent_category = get_post_meta( $post->ID, 'prominent_cat', true );
    if ( $term = get_term_by( 'name', $prominent_category, 'category' ) ) {
        $cat = $term;
    }

    return $cat;
}

If the meta value is a term/category ID, then you can simply use get_term() to get the category data:

add_filter( 'post_link_category', 'my_post_link_category', 10, 3 );
function my_post_link_category( $cat, $cats, $post ) {
    $prominent_category = get_post_meta( $post->ID, 'prominent_cat', true );
    if ( $prominent_category && term_exists( (int) $prominent_category ) ) {
        $cat = get_term( $prominent_category );
    }

    return $cat;
}

本文标签: categoriesWhat39s the Simplest Way to OverrideRewrite the category Permalink Structure Tag