我相信你所指的“查看”链接并不是url应该更改的唯一地方,所以我冒昧地在其他地方也更改了它们。
这只影响已发布的帖子和页面。
这是相当多的代码,但这应该可以做到。
// Change the "View" link on the all posts page.
function my_custom_view_link( $actions, $page_object ) {
$title = _draft_or_post_title();
$actions[\'view\'] = sprintf(
\'<a href="%s" rel="permalink" aria-label="%s">%s</a>\',
my_custom_permalink( $post->ID ),
esc_attr( sprintf( __( \'View “%s”\', \'my-text-domain\' ), $title ) ),
__( \'View\', \'my-text-domain\' )
);
return $actions;
}
add_filter( \'post_row_actions\', \'my_custom_view_link\', 10, 2 );
// Change the "Post updated" link on the edit post page after saved.
function my_custom_post_updated_messages( $messages ) {
global $post_ID;
$view_page_link_html = sprintf( \' <a href="%1$s">%2$s</a>\',
esc_url( my_custom_permalink( $post_ID ) ),
__( \'View post\', \'my-text-domain\' )
);
$messages[\'post\'][1] = __( \'Post updated\', \'my-text-domain\' ) . $view_page_link_html;
return $messages;
}
add_filter( \'post_updated_messages\', \'my_custom_post_updated_messages\', 10, 1 );
// Change the "Post updated" link on the edit post/page after saved.
function my_custom_post_preview_link( $return, $post_id, $new_title, $new_slug, $post ) {
if ( \'publish\' !== $post->post_status ) {
return $return;
}
if ( false === strpos( $permalink, \'%postname%\' ) ) {
$view_link = my_custom_permalink( $post_id );
$display_link = urldecode( $view_link );
$preview_target = " target=\'wp-preview-{$post->ID}\'";
$return = \'<strong>\' . __( \'Permalink:\', \'my-text-domain\' ) . "</strong>\\n";
$return .= \'<a id="sample-permalink" href="\' . esc_url( $view_link ) . \'"\' . $preview_target . \'>\' . esc_html( $display_link ) . "</a>\\n";
}
return $return;
}
add_filter( \'get_sample_permalink_html\', \'my_custom_post_preview_link\', 10, 5 );
// Replace the base url with a custom one.
function my_custom_permalink( $post_id ) {
return str_replace( home_url(), \'http://site2.com\', get_permalink( $post_id ) );
}