use the_permalink
or get_the_permalink
to display/retrieve the URL of a post ( a post of any type, they’re all the same internally )
e.g.
the_permalink();
orx
<a href="https://wordpress.stackexchange.com/questions/283706/<?php echo esc_url( get_the_permalink( $post_id ) ); ?>">
Notice I used esc_url
to escape the value for security reasons.
Here are some similar functions you might find useful:
get_post_type_archive_link('product')
should return/product/
or the equivalent archive for that post type that WP generatesget_term_link( 'purple', 'colours')
fetches the archive link for posts that have the termpurple
in the custom taxonomycolours
. Note that this can also be used for tags and categories. The functions such as tag_link etc all use this function inside them so cut out the middle man!get_author_posts_url(1)
gets the URL of the author posts archive for the user with ID 1get_day_link( $year, $month, $day )
gets the link to a date archive by dayget_month_link
,get_year_link
, same as aboveget_comment_link( $comment )
, get the permalink to a comment
Note that all of these should be escaped with esc_url
at the moment of output, with the exception of the_permalink
which escapes internally