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 badges
Add a comment  | 

4 Answers 4

Reset to default 1
Use 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