admin管理员组

文章数量:1130349

I have a Wordpress site talking about laptop review. In every post, I always add a category name on the title.

For example "ASUS ZenBook UX480FD Review" is categorized as "ASUS".

However, I need to add the category manually. For efficiency purpose, I want my site to automatically apply category based on the post title (especially the first word of the title).

Is anybody knows the plugin or code to do so?

Thanks!

note: the category slug already created

I have a Wordpress site talking about laptop review. In every post, I always add a category name on the title.

For example "ASUS ZenBook UX480FD Review" is categorized as "ASUS".

However, I need to add the category manually. For efficiency purpose, I want my site to automatically apply category based on the post title (especially the first word of the title).

Is anybody knows the plugin or code to do so?

Thanks!

note: the category slug already created

Share Improve this question asked Dec 29, 2018 at 9:32 Christian Dwi WijayaChristian Dwi Wijaya 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You could try this. I havn't really tested it, so I'm not sure it works - might require some small tweaks aswell.

function add_category_based_on_title( $post_id ) {

    // title
    $title = get_the_title( $post_id );

    // get first part of title
    $substr = explode( " ", $title )[0];

    // get category id
    $category_id = get_cat_ID( $substr ); 

    // check category
    if ( ! empty( $category_id ) ) ) {

        // set category
        wp_set_post_categories( $post_id, [ $category_id ], true )

    }

}
add_action( 'save_post', 'add_category_based_on_title' );

References:

https://codex.wordpress/Function_Reference/wp_set_post_categories https://codex.wordpress/Function_Reference/get_cat_ID https://developer.wordpress/reference/functions/get_the_title/

I have a Wordpress site talking about laptop review. In every post, I always add a category name on the title.

For example "ASUS ZenBook UX480FD Review" is categorized as "ASUS".

However, I need to add the category manually. For efficiency purpose, I want my site to automatically apply category based on the post title (especially the first word of the title).

Is anybody knows the plugin or code to do so?

Thanks!

note: the category slug already created

I have a Wordpress site talking about laptop review. In every post, I always add a category name on the title.

For example "ASUS ZenBook UX480FD Review" is categorized as "ASUS".

However, I need to add the category manually. For efficiency purpose, I want my site to automatically apply category based on the post title (especially the first word of the title).

Is anybody knows the plugin or code to do so?

Thanks!

note: the category slug already created

Share Improve this question asked Dec 29, 2018 at 9:32 Christian Dwi WijayaChristian Dwi Wijaya 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You could try this. I havn't really tested it, so I'm not sure it works - might require some small tweaks aswell.

function add_category_based_on_title( $post_id ) {

    // title
    $title = get_the_title( $post_id );

    // get first part of title
    $substr = explode( " ", $title )[0];

    // get category id
    $category_id = get_cat_ID( $substr ); 

    // check category
    if ( ! empty( $category_id ) ) ) {

        // set category
        wp_set_post_categories( $post_id, [ $category_id ], true )

    }

}
add_action( 'save_post', 'add_category_based_on_title' );

References:

https://codex.wordpress/Function_Reference/wp_set_post_categories https://codex.wordpress/Function_Reference/get_cat_ID https://developer.wordpress/reference/functions/get_the_title/

本文标签: categoriesHow to automatically apply a category based on the post title