基于前面问题中提供的可怕的G.M.代码(Use Custom Fields to Create Guest Author Link), 我现在有一个自定义函数,它使用自定义字段来支持来宾作者。代码将更改帖子作者以显示来宾作者姓名,并将作者链接更改为我选择的来宾作者URL。这是我添加到主题函数中的代码。支持来宾作者的php文件:
//Guest Author Handling - Uses custom fields for guest author name and URL. Changes the guest author name display and changes the author link to guest author URL of your choosing
add_filter( \'get_the_author_user_url\', \'guest_author_url\' );
add_filter( \'the_author\', \'guest_author_link\' );
add_filter( \'get_the_author_display_name\', \'guest_author_name\' );
function guest_author_url($url) {
global $post;
$guest_url = get_post_meta( $post->ID, \'guest-url\', true );
if ( filter_var($guest_url, FILTER_VALIDATE_URL) ) {
return $guest_url;
} elseif ( get_post_meta( $post->ID, \'guest-author\', true ) ) {
return \'#\';
}
return $url;
}
function guest_author_link($name) {
global $post;
$guest_url = get_post_meta( $post->ID, \'guest-url\', true );
$guest_name = get_post_meta( $post->ID, \'guest-author\', true );
if ( $guest_name && filter_var($guest_url, FILTER_VALIDATE_URL) ) {
return \'<a href="\' . esc_url( $guest_url ) . \'" title="\' . esc_attr( sprintf(__("Visit %s’s website"), $guest_name) ) . \'" rel="author external">\' . $guest_name . \'</a>\';
} elseif( $guest_name ) {
return $guest_name;
}
return $name;
}
function guest_author_name( $name ) {
global $post;
$guest_name = get_post_meta( $post->ID, \'guest-author\', true );
if ( $guest_name ) return $guest_name;
return $name;
}
玩了这个之后,我想到可以简化来宾作者URL条目。我的来宾作者的URL将始终按以下格式设置:
http://yoursite.com/author/[AuthorName]
由于URL将始终遵循相同的模式,我需要知道的是如何修改G.M.的代码,以便只需在来宾作者URL的自定义字段中输入[AuthorName]?
我希望这有意义。。。
SO网友:Matt Costa
为了防止有人在这里寻找如何添加来宾描述,下面是我的完整实现。
如果遵循与其他答案相同的逻辑,我只添加了一个get_the_author_description 过滤器和覆盖作者个人信息的功能(说明)。与上面的其他解决方案一样,您可以将其作为自定义字段传递(请参见所附图像)。
这是完整的实现,添加了上述解决方案。
// Change Guest Author Name, Link and Description
add_filter( \'get_the_author_user_url\', \'guest_author_url\' );
add_filter( \'the_author\', \'guest_author_link\' );
add_filter( \'get_the_author_display_name\', \'guest_author_name\' );
add_filter( \'get_the_author_description\', \'guest_author_description\' );
function guest_author_url($url) {
global $post;
$guest_url = get_post_meta( $post->ID, \'guest-url\', true );
if ( filter_var($guest_url, FILTER_VALIDATE_URL) ) {
return $guest_url;
} elseif ( get_post_meta( $post->ID, \'guest-author\', true ) ) {
return \'\';
}
return $url;
}
function guest_author_link($name) {
global $post;
$guest_url = get_post_meta( $post->ID, \'guest-url\', true );
$guest_name = get_post_meta( $post->ID, \'guest-author\', true );
if ( $guest_name && filter_var($guest_url, FILTER_VALIDATE_URL) ) {
return \'\' . $guest_name . \'\';
} elseif( $guest_name ) {
return \'\' . $guest_name . \'\';
}
return $name;
}
function guest_author_description($description) {
global $post;
$guest_description = get_post_meta( $post->ID, \'guest-description\', true );
if ( $guest_description ) return $guest_description;
elseif ( get_post_meta( $post->ID, \'guest-author\', true ) ) {
return \'\';
}
return $description;
}
function guest_author_name( $name ) {
global $post;
$guest_name = get_post_meta( $post->ID, \'guest-author\', true );
if ( $guest_name ) return $guest_name;
return $name;
}