ThemeShaper Forums » Thematic

[closed]

How to add a link to the Single Post to redirect to certain pages

(14 posts)
  • Started 7 years ago by sixfootjames
  • Latest reply from sixfootjames
  • This topic is not resolved
  1. Hi,

    I have about three categories of posts using the same single.php file. What I need is a way to add a button to the bottom of the page to return to the "list" page of THAT specific category once the person gets to the end of the post.

    I know I should probably be using an override but I am not sure where to even start with this?

    Thanks

    Posted 7 years ago #
  2. Or it should be added to the hook thematic_belowpost()? Still... I would need a way to run an if statement with that hook to check which category I am in and then redirect the link to the right list page.

    Posted 7 years ago #
  3. Right, I am answering my own question here and hoping others will fill in the gaps :)

    function add_below_the_post ()
    {
       if (this_category)
       {
    
       } else if (another_category)
       {
    
       } else if (another_category)
       {
    
       } else
       {
    
       }
    }
    
    add_action('thematic_belowpost', 'add_below_the_post');
    Posted 7 years ago #
  4. what you are missing is the conditions. in this case the logic test should be whether a post is in a specific category... which is achieved with in_category()

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

    originally, i borked and wrote you needed conditional tags
    http://codex.wordpress.org/Conditional_Tags

    you don't need them this time, but you almost assuredly will, so i will leave them as a reminder.

    edited code:

    function add_below_the_post () {
       if (in_category('apple')) {
    		// Get the category object
    		$category = get_category_by_slug( 'apple' );
       } elseif (in_category('bacon')) {
    		$category = get_category_by_slug( 'bacon' );
       } elseif (in_category('guacamole')) {
    		$category = get_category_by_slug( 'guacamole' );
       } 
    
       	if (!is_wp_error($category)) {
    
    		// Get the URL of this category
    		$category_link = get_category_link( $category->term_id );	?>
    
    		<!-- Print a link to this category -->
    		<a href="<?php echo esc_url( $category_link ); ?>" title="<?php _e('Permalink to ' . $category->name); ?>"><?php _e('Permalink to ' . $category->name); ?></a>
    	<?php }
    
    }
    
    add_action('thematic_belowpost', 'add_below_the_post');

    Edited 3/6/2012 b/c the first code was wrong. this code is tested, but will not work well if post is assigned to multiple categories.... it will be print the link for whichever category comes first in the function's if/else construct

    Posted 7 years ago #
  5. Right, let me test this ;)

    Posted 7 years ago #
  6. Hi Helga,

    So I am testing this and I do not seem to get even into my first if statement.

    function add_below_the_post () {
    echo ('test');
    if (is_category( 'student-stories' ) ) {
    // Get the category object
    echo ('stories');.....

    The syntax looks correct and the first "test" outputs at the end of the single post. Is there something else I might be overlooking?

    I'm reading over the conditional tags link you sent me and trying numerous options but I must be doing something wrong?

    Thanks

    Posted 7 years ago #
  7. Ok, I see that if I add the echo in the...

    if (!is_wp_error($category)) {
    <?php echo ('test') ?>.....

    statement, I can output something if we are on a specific category. A little closer to where I want to be.

    If however I <?php echo ($category_link) ?>, it does not return anything. I am trying to echo the $category too, to see what it returns without much joy to trace what is coming through in that var.

    Posted 7 years ago #
  8. so i probably shouldn't post untested code... especially not when i'm tired.

    i got confused on when you wanted to know if 'we are on a specific category'. but we're adding stuff to thematic_belowpost which occurs only on single posts. is_category() only returns true on category archives.... and ne'er the two shall meet.

    if you want to check if a post is in a category you use different logic: http://codex.wordpress.org/Function_Reference/in_category

    i'm going to edit my code above instead of leaving such borked code out there for people to use. also you won't be able to echo the $category variable since it will be an object. to see what is in it you'll need to print_r($category) or var_dump($category)

    Posted 7 years ago #
  9. Hi Helga,

    I've read through a fair amount of documentation on WordPress and what you sent me and slowly building it up.

    Will let you know how I get on ;)

    Cheers!

    Posted 7 years ago #
  10. Ok, back to this. Let me see if I can get this sorted out. If you're about today Helga...that would be much appreciated! :)

    In the meantime I will see if I can crack this sucker.

    Posted 7 years ago #
  11. Ok, I think I have it...

    function add_below_the_post () {
    
       //echo ('working');
    
    	$category = get_the_category();
    	$category = $category[0]->category_nicename;
    
    	if (!is_wp_error($category))
    	{
    		if ($category == 'student-stories')
    		{
    			echo('<a href="' .site_url() .'/people/student-stories/">Return to ' .$category .'</a>');
    		} else if ($category == 'news')
    		{
    			echo('<a href="' .site_url() .'/research/news/">Return to ' .$category .'</a>');
    		} else if ($category == 'research')
    		{
    			echo('<a href="' .site_url() .'/research/research-themes/">Return to ' .$category .'</a>');
    		}
    
    	}
    
    }

    Only problem is that it repeats the link twice and I cannot seem to see why this should happen?

    Also, as I am still learning to write PHP, please correct me if there is a better way to write this out ;)

    Posted 7 years ago #
  12. I seem to have found the culprit but this doesn't make sense. I have commented out //thematic_belowpost(); so now my button only appears once surely this cannot be right?

    Posted 7 years ago #
  13. did you try the code i edited above? using in_category()? you shouldn't have to hard-code the links.... what if they changed?

    not sure about the twice-appearing content...

    Posted 7 years ago #
  14. Oops, didn't see you edited the code. Let me have a look at this again. Yes, hardcoding the urls was my last resort but I will look into your example now...thanks Helga!

    Posted 7 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.