另一种选择是使用add_query_arg()
具有get_permalink()
.
我想get_the_title()
可能不是这里的最佳选项,因为标题可能包含“Protected”、“Private”或其他内容,具体取决于您的设置以及自定义函数是否连接到the_title
滤器使用起来可能更好/更安全post_title
直接从post对象。
<?php global $post;
$url = add_query_arg( \'job_position\', $post->post_title, get_permalink() ); ?>
<a href="<?php echo esc_url_raw( $url ); ?>"><?php _e( "Apply to job", "text-domain" ); ?></a>
<?php // Results in http://www.domain.tld?job_position=title ?>
EDIT - shortcode example
<?php
// Copy to (child) theme\'s functions.php
function button_shortcode( $atts ) {
// Support for shortcode "url" parameter for overriding default hard-coded url
// usage: [bt-apply-job url="http://www.someurl.com"]
// Check if parameter is set and is valid url
// defaults to hard-coded url
// use $url = \'http://example.com/send-cv/\' if shortcode parameter support is not needed
$url = ( ! empty( $atts[\'url\'] ) && filter_var( $url, FILTER_VALIDATE_URL ) ) ? $atts[\'url\']: \'http://example.com/send-cv/\';
// Add post title as job_position parameter to url
// post object is used to get unfiltered post title
global $post;
$url = add_query_arg( \'job_position\', $post->post_title, $url );
// Return shortcode output, shortcode shouldn\'t echo its output
// E.g. http://example.com/send-cv/?job_position=title
return sprintf(
\'<a href="%s">%s</a>\',
esc_url_raw( $url ), // escaped url for safe output
esc_html__( "Apply to job", "text-domain" ) // escaped translatable anchor text
);
}
add_shortcode(\'bt-apply-job\', \'button_shortcode\');