wordpress永久链接设置中的帖子作者ID

时间:2017-11-15 10:14:57

标签: wordpress

我正在制作我的自定义固定链接并希望获得帖子作者ID我已经尝试了很多东西但都是徒劳的,请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

STEP 1 add this into your functions.php

Guidance here https://wordpress.stackexchange.com/questions/112719/how-to-use-author-id-in-post-permalink     

function wpse_112719_pre_post_link( $permalink, $post, $leavename ) {
    if ( strpos( $permalink, '%author%' ) !== false ) {
        $authordata = get_userdata( $post->post_author );
        $author_id = $authordata->ID;
        $permalink = str_replace( '%author%', $author_id, $permalink );
    }

    return $permalink;
}

add_filter( 'pre_post_link', 'wpse_112719_pre_post_link', 10, 3 );

function wpse_112730_add_rewrite_rules() {
    add_rewrite_rule(
        "([0-9]+)/(.*)",
        'index.php?name=$matches[2]',
        'top'
    );
}

add_action( 'init', 'wpse_112730_add_rewrite_rules' );

第2步登录管理员端并设置永久链接自定义结构,如下所示:/%postname%/%author%