admin管理员组文章数量:1124409
I want to pass the 'post title' of current post as parameter into a link
For example in this way:
<a href='/?job_position=seller'>apply to job</a>
Where the post title would be 'seller'
How can I achieve this?
I want to pass the 'post title' of current post as parameter into a link
For example in this way:
<a href='http://example.com/send-cv/?job_position=seller'>apply to job</a>
Where the post title would be 'seller'
How can I achieve this?
Share Improve this question edited May 25, 2019 at 16:32 LoicTheAztec 3,38117 silver badges24 bronze badges asked May 25, 2019 at 15:08 Estación VisualEstación Visual 115 bronze badges4 Answers
Reset to default 1Use this function of wordpress
get_permalink( int|WP_Post $post,bool $leavename = false )
Like this...
<a href="<?=get_permalink()?>">Apply to job </a>
Documentation :
https://developer.wordpress.org/reference/functions/get_permalink/
Anyways if you want get the title of current post use that:
https://developer.wordpress.org/reference/functions/the_title/
Finally I achieved what I was looking for.
This is the code:
function button_shortcode() {
echo '<a href="http://example.com/send-cv/?job_position='.get_the_title().'">Apply to job</a>';
}
add_shortcode('bt-apply-job', 'button_shortcode');
Another option is to use add_query_arg()
with get_permalink()
.
I think get_the_title()
might not be the best option here, because the title might contain "Protected", "Private" or something else depending on your setup and if custom functions are hooked to the_title
filter. Might be better/safer to use post_title
directly from the post object.
<?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');
You can dynamically set the link href attribute by using the the_title() function to retrieve the current post title and concatenating it into the desired URL:
<a href="http://example.com/send-cv/?job_position=<?php echo urlencode(get_the_title()); ?>">apply to job</a>
In this example, the get_the_title() function retrieves the current post title and the urlencode() function is used to encode the post title for use in a URL. The resulting link will have an href attribute of http://example.com/send-cv/?job_position=seller, with seller being the value of the current post title.
本文标签: phpAdd post title as a link parameter
版权声明:本文标题:php - Add post title as a link parameter 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736626293a1945681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论