• Resolved g9013507

    (@g9013507)


    I have developed a site where the client writes blog posts for two different categories called ‘News’ and ‘Company News’.
    Everything for ‘News’ is listed on the news page and everything for ‘Company News’ is listed on the ‘Company News’ page.

    I have been asked to add a back button to each post which takes the user back to the parent list page for that category, i.e. if the post is listed under ‘News’ the back button will take them to the ‘News’ page.

    My problem is that all blog posts use the ‘single.php’ template regardless of the category they’re associated with.

    I have written the following which works in a way, but it takes me to a specific category area rather than the ‘News’ or ‘Company News’ list pages.

    ///////////////////////
    
    <?php
    $cats=get_the_category();
        foreach($cats as $cat){
            if($cat->category_parent == 0 && $cat->term_id != 1){
                echo '<h2 class="link"><a>term_id ).'">Return</a></h2>';
            }
            break;
        }
    ?>
    
    ///////////////////////

    I’m struggling to adapt this to take the user to the right page.

    Thanks

    • This topic was modified 4 years, 9 months ago by Jan Dembowski.
    • This topic was modified 4 years, 9 months ago by g9013507.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Isn’t the “specific category area” for ether News or Company News the right page? Or is the right page some other page than the category archive list? Or is it that the link leads to other assigned category archives besides the two desired?

    I’d think the logic you want would be: if “News” is one of the assigned categories, output a link to the News page; else if “Company News” is one of the assigned categories, output a link to the Company News page.

    Also, your echo line isn’t outputting valid HTML. I’d initially simply hard code the correct links instead of building them dynamically. If you want to add dynamic flexibility later on that’s fine, but you should want basic functioning code first. You can add bells and whistles later.

    Considering people could land at a blog post directly from search results, “Back” or “Return” would not make sense unless the referrer page was first checked. You might consider labels like “More News” instead. Of course, convincing clients their idea is not the best can be challenging ??

    Thread Starter g9013507

    (@g9013507)

    Thanks for your reply.
    You’re right and have described it better than me. The posts are called to two custom pages, not the standard archive pages, one listing the posts assigned to the ‘news’ category and the other showing posts assigned to the ‘company news’ category.

    I agree that a hard coded solution is a neat way to do this, however all posts regardless of category use the ‘single.php’ template. This way the return button would always send the user to the same place which would be correct only approximately 50% of the time.
    I’m trying to add a solution that checks the category the post is assigned to and returns the user to the custom page showing that category. i.e. News or Company News.

    A button could be added to the wysiwyg editor each time a post is written, but other people write the posts and I’m trying to avoid any extra work for them.

    • This reply was modified 4 years, 9 months ago by g9013507.
    Moderator bcworkz

    (@bcworkz)

    You misunderstand my proposed solution. I don’t mean hardcode the entire back button in a single bit of static HTML. I propose modifying your code with an elseif () condition which determines the href attribute of your button code. There would be three possible outcomes, one of the two back buttons or no button when there is neither category. For example:

    $cats = get_the_category();
    foreach($cats as $cat){
      if ( $cat->term_id == 123) { // News cat ID
        // output News button here
      } elseif ( $cat->term_id == 456) { // Company News cat ID
        // output Company News button here
      }
      break;
    }

    It is the two different outputs I propose hardcoding. In part because there is no formal relation in WP between an arbitrary page and any specific category. Such a button is normally a common anchor link which has been styled to appear like a button. For example:
    echo '<a href="https://example.info/latest-news/">More News</a>';
    All of this goes on your single.php template where ever in the loop output you want the button to appear. When you find the need to alter theme templates, it’s best done with a child theme.

    Thread Starter g9013507

    (@g9013507)

    Hey bcworkz,

    I see what you mean.
    So in this situation becuase the two pages displaying the different categories are custom, there’s no way to easily and automatically pick the page URL to return to.

    Ok, let me see about your work around and I’ll check back in with how it’s going.
    Also yes, fully agreed on using the child theme and I’m running that for all custom additions.

    Many thanks for your support.

    Moderator bcworkz

    (@bcworkz)

    It’s possible to dynamically build the related page URLs based on the current category. It’s still a kludge. For two links it’s hardly worth the effort IMO. It’s easier to simply hardcode each URL. Now if you had a dozen different pages to go back to, it may be worth dynamically constructing the URL’s. Even then, such a scheme would be dependent upon there being a consistent relationship between category name and page name, which isn’t necessarily a given.

    I recommend implementing my solution first just to have something working. If you’d like to embark on an interesting coding exercise afterwards to dynamically build URLs, by all means give it a go. It’d be a learning experience whether you succeed or not ??

    Thread Starter g9013507

    (@g9013507)

    Sure thing.
    Thanks for the input. Really appreciated.
    I’ve worked in the hard coded solution and updated for my site.
    It works very well!

    I’ll develop a dynamic solution as things move on, but for the moment this one is solved.

    Again thank you very much for your assistance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add back button on single posts pages’ is closed to new replies.