WordPress.org

Ready to get started?Download WordPress

Forums

Conditional statements and the Loop - help :) (4 posts)

  1. opensource
    Member
    Posted 3 years ago #

    Hi there,

    I wondered if somebody out there can help me! :)

    I'm trying to output a link at the end of my loop to take the user back to the correct category depending on some conditions.

    I have read many help forums and tried a combination of things, but couldn't see the answer.

    At the end of my loop I want it to output the correct link depending on if the post was categorised as "blog", "in the news", "event updates" etc.

    So for example, say I click on Blog then one of the posts listed on that page, at the foot of post (when viewed as a single post) I want to output "back to blog." The same for Event Updates, when a user is viewing a single post view, I want my loop to output a link that takes the user back to 'Event Updates'.

    Here's my code, at the end of my loop (which works perfectly fine otherwise!:

    <?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>
    
           <?php if (get_the_category('blog')) { 
    
         echo '<a href="/blog">Back to blog</a>'; } 
    
    		elseif (get_the_category('eventsupdates')) { 
    
            echo '<a href="/event-updates">Back to event updates</a>';}
    
     		else { 
    
    	 echo '<a href="/in-the-news/">Back to in the news</a>'; }

    Any help gratefully received!

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    have you read the codex documentation on get_the_category() ?

    http://codex.wordpress.org/Function_Reference/get_the_category

    simply make it a habit to look-up all functions that you want to use.

    i would put this before the <?php else : ?> at the start of your snippet:

    <?php foreach(get_the_category() as $cat) { echo '<a href="' . get_category_link($cat->term_id) . '">Back to ' . $cat->name . '</a><br/>'; } ?>

    you might want to change the text, add html tags for formatting reasons, and remove the <br/> if you are positive that the post will only have one category.

  3. opensource
    Member
    Posted 3 years ago #

    Alchymyth,

    Thanks so much for helping me out and taking the time to help.

    I had a question - is there a way to get my single post view to go back to it's parent page?

    For example in my page 'In the news' I built a simple loop which displays all categories tagged 'in the news'. Is there a way to change my link so that it says

      back to in the news

    , but instead of jumping to the archive page it jumps back to the page that I created called "in the news"?

    Your assistance, thoughts greatly appreciated.

    Thank you!

  4. alchymyth
    Forum Moderator
    Posted 3 years ago #

    the 'back button' of the browser would do that - i think there is a way with javascript to import this into a button on the site.

    with wordpress functions, this is rather more difficult to achieve.

Topic Closed

This topic has been closed to new replies.

About this Topic