WordPress.org

Ready to get started?Download WordPress

Forums

Simple if statement with conditional tags (5 posts)

  1. lewismalpas
    Member
    Posted 3 years ago #

    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

  2. DigitalSquid
    Member
    Posted 3 years ago #

    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>";
    }
  3. lewismalpas
    Member
    Posted 3 years ago #

    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!

  4. DigitalSquid
    Member
    Posted 3 years ago #

    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>";
       }
    }
  5. lewismalpas
    Member
    Posted 3 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic