简体   繁体   English

更改帖子永久链接结构(WordPress)

[英]Change post permalink structure (WordPress)

I want to change the default post permalink structure from (example.com/post-name) to (example.com/author_firstname-author_lastname/post-name). 我想将默认的帖子永久链接结构从(example.com/post-name)更改为(example.com/author_firstname-author_lastname/post-name)。

I tried to add a new rewrite tag which I could use in the custom permalink structure to use the author first name and last name in the URL but it didn't work (404 error page). 我试图添加一个新的重写标记,可以在自定义永久链接结构中使用它来在URL中使用作者的名字和姓氏,但它不起作用(404错误页面)。

add_filter('post_link', 'pauthor_permalink', 10, 3);
add_filter('post_type_link', 'pauthor_permalink', 10, 3);

function pauthor_permalink($permalink, $post_id, $leavename) {
    add_rewrite_tag( '%pauthor%', '(.+)', 'pauthor=' );
    if (strpos($permalink, '%pauthor%') === FALSE) return $permalink;

    // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get Author Data
        $post_author = get_post_field( 'post_author', $post->ID );
        $user_info = get_userdata($post_author);
        $first_name = $user_info->first_name;
        $last_name = $user_info->last_name;
        $pauthor = $first_name.'-'.$last_name;


    return str_replace('%pauthor%', $pauthor, $permalink);
}   

any help!? 任何帮助!?

Changing the Permalink structure in wordpress can be done without any sort of php. 无需任何类型的php,即可更改wordpress中的Permalink结构。

See Settings > Permalinks 请参阅设置>永久链接

https://codex.wordpress.org/Settings_Permalinks_Screen https://codex.wordpress.org/Settings_Permalinks_Screen


The structure you are looking for appears to be /%author%/%postname%/ 您正在寻找的结构似乎是/%author%/%postname%/

More tags can be found here: https://codex.wordpress.org/Using_Permalinks#Structure_Tags 可以在这里找到更多标签: https : //codex.wordpress.org/Using_Permalinks#Structure_Tags


Creating Custom Tags 创建自定义标签

You can create custom tags by creating a new taxonomy with 'rewrite' => true , for example: 您可以通过使用'rewrite' => true创建新的分类法来创建自定义标签,例如:

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 
            ) 
        );
    }
}

Source: https://wordpress.stackexchange.com/questions/168946/add-more-structure-tag-to-permalink 资料来源: https : //wordpress.stackexchange.com/questions/168946/add-more-structure-tag-to-permalink

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM