I have product pages which need a button to link to the related appointment booking page. So for example I'll create a page called 'glass-product', and I want to code its template so that there's a link to a page called 'glass-product-book'.

So I need to get the permalink of the post, but then add '-book' to the URI within the link, presumably I need to add something to the current: <a href="<?php the_permalink(); ?>">Book Now</a>

Anyone know what needs adding?

1 Answer 1


Well, succinctly you could just add '-book' to your link, if your permalink settings result in post-name slugs:

<a href="<?php echo untrailingslashit( get_the_permalink() ); ?>-book/">Book Now</a>

However if you're not very careful, or are planning on running a site with a large number of content or with several people having access to the dashboard - you'll probably end up with broken links at some point.

The more robust solution would be to store the ID of the related appointments page in a Custom Field on the products page (let's call the field product_appointment_page_id), then retrieve the corresponding appointment page permalink in your template:

  $appointment_page_id = get_post_meta( get_the_id(), 'product_appointment_page_id', true );

  if( ! empty( $appointment_page_id ) )
    echo( '<a href="' . get_permalink( $appointment_page_id ) . '">Book Now</a>' );

Even better still would be to implement both "products" as well as "appointments" as their own Custom Post Types, then associate them with the Metadata API, as in the previous example.

  • Thanks Bosco - the first solution actually creates a link to 'glass-product/-book', there's only a couple dozen products and only me on admin.
    – iain-g
    Dec 2, 2015 at 22:51
  • Ah, right! Luckily there's a function to get rid of trailing slashes. Try it now.
    – bosco
    Dec 2, 2015 at 22:56
  • Not sure I get how the second option works but will give it a try. Products and appointments are already custom post types, so I will look into option 3 too. Many thanks.
    – iain-g
    Dec 2, 2015 at 22:56
  • Perfect - will try the other options too for thoroughness, but this is a solution. Thank you!
    – iain-g
    Dec 2, 2015 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.