0

When everything else fails, I turn to stackoverflow.

I have two websites that I am in the process of creating a theme for. In the footer section of each website I include two links: one to the privacy page and the other to the terms page.

On the first website, the permalink to those pages is as follows: www.website.com/privacy and www.website.com/terms.

On the second website the permalink to those pages is as follows: www.website.com/privacy-policy and www.website.com/terms-conditions.

Here's what I was hoping to do. Instead of hard coding the urls to these two pages from the footer section, I wanted to generate them dynamically.

Heres an example of what the if statement might look like:

<a href="
   <?php
       if (the url exists) {
         echo the_permalink('privacy');
       } else { 
         echo the_permalink('privacy-policy');
       }
   ?>
">Privacy</a>

Your help is appreciated.

testing
  • 19,681
  • 50
  • 236
  • 417
Fabian Amran
  • 833
  • 6
  • 11
  • 2
    Why not use [`wp_nav_menu()`](https://developer.wordpress.org/reference/functions/wp_nav_menu/)? – rnevius Mar 25 '16 at 13:59

1 Answers1

0

Use get_permalink instead. Note that at this point in your code you will need to know the unique post ID for each post.

<a href="<?php
if ( the url exists ) {
    echo get_permalink( $first_post_id );
} else {
    echo get_permalink( $second_post_id );
} ?>">Privacy</a>

If those URLs might change though you should ideally use a menu.

Technoh
  • 1,606
  • 15
  • 34
  • That's not entirely true. The OP could easily get the post ID programmatically from the post slug. – rnevius Mar 25 '16 at 14:11
  • Of course. I meant to say "at this point you will need to know". I've updated my answer to reflect this. – Technoh Mar 25 '16 at 14:28