WordPress.org

Ready to get started?Download WordPress

Forums

Trying to add a link back to a single page template (12 posts)

  1. databell96
    Member
    Posted 3 years ago #

    I'm creating a series of templates for a music client. Idea is at the end of the post, they should see a link like this:

    RETURN TO OFFICIAL NEWS-->

    Not Return to Previous Page but the actual name of the category. So I duplicated single.php into single-official-news (also tried the number of the category as well) and used the following HTML code:

    <a href="http://www.bigpearlandthefugitivesoffunk.com/category/official-news/" title="Return to Offical News">RETURN TO OFFICIAL NEWS--></a>

    But it's not showing up in the post. How can I make it appear? Am I using the right template file?

  2. I don't think that filename will do the trick. Single.php only seems to have variations for custom post types:

    http://codex.wordpress.org/Template_Hierarchy#Single_Post_display

    I would try adding a conditional statement to your original single.php. Something like:

    <?php if (in_category('official-news')): ?>
    
    <a href="http://www.bigpearlandthefugitivesoffunk.com/category/official-news/" title="Return to Offical News">RETURN TO OFFICIAL NEWS--></a>
    
    <?php endif; ?>

    This says "If the post is in the official-news category, display the return link."

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

    Let me know if it works.

  3. databell96
    Member
    Posted 3 years ago #

    It does to a point, Zoonini. However, I decided to add an elseif statement and that doesn't seem to work. Here's what I did to the original code:

    <?php if (in_category('official-news')): ?>
    
    <a href="http://www.bigpearlandthefugitivesoffunk.com/category/official-news/" title="Return to Offical News">RETURN TO OFFICIAL NEWS--></a>
    
    } elseif (in_category('latest')): {
    
    <a href="http://www.bigpearlandthefugitivesoffunk.com/category/latest/" title="Return to Offical News">RETURN TO LATEST NEWS--></a>
    
    <?php endif; ?>
  4. Bunch of syntax problems there. You can't mix colon syntax and bracket syntax - it has to be one or the other. You have some missing php opening/closing code. Also, I'm not positive but I don't think you can use "elseif" without a final "else." Try this instead:

    <?php if (in_category('official-news')): ?>
    
    <a href="http://www.bigpearlandthefugitivesoffunk.com/category/official-news/" title="Return to Offical News">RETURN TO OFFICIAL NEWS--></a>
    
    <?php endif;
          if (in_category('latest')): ?>
    
    <a href="http://www.bigpearlandthefugitivesoffunk.com/category/latest/" title="Return to Offical News">RETURN TO LATEST NEWS--></a>
    
    <?php endif; ?>
  5. databell96
    Member
    Posted 3 years ago #

  6. Are some posts are assigned to both the Official News and Latest categories?

  7. databell96
    Member
    Posted 3 years ago #

    Yes

  8. Then you need to decide how to handle it - i.e. which link do you want displayed for those posts in both categories - and code your single.php accordingly.

  9. databell96
    Member
    Posted 3 years ago #

    Well, am I stuck using just one link reference for both or all categories? Client doesn't want that. If you're on Latest the link should reference that and if on Official News it should reference that. That's why I was attempting to use variations of the single.php template.

    Frustrating, I know. And I thank you for all the assistance so far.

  10. The problem is once you click on an individual post that is assigned to both Official News and Latest, all the system knows is that the post you're on has those two categories attached to it. I don't think the single post knows that you happened to have clicked on the post under the "Latest" heading or under the "Official News" heading. Do you see the issue?

    The reason the Official News link is the one that shows up in the posts that are in both categories is because the "Official News" PHP statement is first, before the "Latest" one. If you were to flip the order of the statements so Latest is first, it would be that link that would display for posts in both categories.

    Not sure I'm explaining this well. Perhaps someone else has another idea, but for the moment I can't think how to achieve what you're trying to do.

    One thought is that for posts in both categories you could display both return links - you would add a third statement for that scenario.

  11. databell96
    Member
    Posted 3 years ago #

    Yea, I'm trying to avoid going the plugin route. Wonder If I just need to change my permalink structure from /%postname%/ to /%category%//%postname%/ ?

  12. You could always try it and see - not sure if that'll matter but it's worth a shot.

Topic Closed

This topic has been closed to new replies.

About this Topic