Support » Themes and Templates » Simple if statement with conditional tags

  • Hey guys,

    In theory this should achieve the desired affect but I have been messing around with this statement for a while now with no joy:

    <?php if (is_single() || is_page('blog')) {
         echo "<a href='blog'>&laquo Back to the blog</a>";
    } elseif (is_archive() || is_single()) {
         echo the_category();
    } ?>

    Essentially I want a link to be displayed at the bottom of single posts – If the user reads a post from the blog it will display a link saying “back to the blog”. If the user has gone through a category and then selected a single post it will provide a link back to the category.

    Thanks guys

Viewing 4 replies - 1 through 4 (of 4 total)
  • DigitalSquid

    (@twelvefootsnowman)

    Assuming you’re using the standard permalink system with domain.com/category/ as the url, you could try something like this:

    $url = explode('/', $_SERVER['HTTP_REFERER']);
    if (in_array('category', $url)) {
    	echo the_category();
    } else {
    	echo "<a href='blog'>&laquo Back to the blog</a>";
    }

    Thanks Charity!

    It’s almost there, however the links are being displayed at the bottom of the pages (i.e back to the blog is being displayed at the bottom of the blog page & the category link is at the bottom of the category page).

    I want the links to only be displayed when a single post is being displayed – For example if the user has gone into “Uncategorised” then selected a single post the link at the bottom would say “Back to Uncategorised”.

    Think that’s possible?

    Thanks!

    DigitalSquid

    (@twelvefootsnowman)

    If you’ve got a single.php page template then paste the code in there.

    If you haven’t got a single.php change the code to:

    if(is_single()) {
       $url = explode('/', $_SERVER['HTTP_REFERER']);
       if (in_array('category', $url)) {
    	echo the_category();
       } else {
    	echo "<a href='blog'>&laquo Back to the blog</a>";
       }
    }

    That worked a charm, I understand the if statement, I will have to read up on the explode to see how that works!!!

    Thanks so much buddy, tried to get that working for hours!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Simple if statement with conditional tags’ is closed to new replies.