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