Add more structure tag to permalink?

admin2025-01-07  4

I'm new in wordpress .I wondering is there anyway to get the category id in permalink ? My current permalink is :

http:///example/%category%/%post_id%-%postname%.html
http:///example/music/1-hello.html

Now my music category_id is 2 , how to add this category_id to permalink ? I want to this:

http:///example/2-music/1-hello.html

I'm new in wordpress .I wondering is there anyway to get the category id in permalink ? My current permalink is :

http:///example.com/%category%/%post_id%-%postname%.html
http:///example.com/music/1-hello.html

Now my music category_id is 2 , how to add this category_id to permalink ? I want to this:

http:///example.com/2-music/1-hello.html
Share Improve this question asked Nov 20, 2014 at 2:50 AsamoaAsamoa 112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

=> Create a Custom Taxonomy

First, we create a custom taxonomy object called rating with the register_taxonomy WordPress function.

add_action( 'init', 'my_rating_init' );

function my_rating_init() {
    if ( ! is_taxonomy( 'rating' ) ) {
        register_taxonomy( 
            'rating', 
            'post', 
            array( 
                'hierarchical' => FALSE, 
                'label' => __( 'Rating' ),  
                'public' => TRUE, 
                'show_ui' => TRUE,
                'query_var' => 'rating',
                'rewrite' => true 
            ) 
        );
    }
}

Setting 'rewrite' => true will automatically add the tag %rating% to our WordPress system.

Yes, it's possible to add the category ID to your permalink in WordPress. You can use the following code in your functions.php file to add the category ID to your permalink structure:

function add_category_id_to_permalink( $post_link, $post ) {
    if ( is_object( $post ) && $post->post_type == 'post' ) {
        $terms = wp_get_object_terms( $post->ID, 'category' );
        if( $terms ) {
            return str_replace( '%category%', $terms[0]->term_id . '-' . $terms[0]->slug, $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_link', 'add_category_id_to_permalink', 10, 2 );

This code will add the first category's ID and slug to the permalink structure. Just make sure to update your permalink structure in WordPress settings to include %category% before adding this code.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265521a1052.html

最新回复(0)