Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:27:24+00:00 2026-06-17T22:27:24+00:00

I’ve been stuck on this issue all day, and I just keep getting nowhere

  • 0

I’ve been stuck on this issue all day, and I just keep getting nowhere with it, hoping someone can help!

I’m building a site for a restaurant that has multiple locations and need to list out each drink that exists at each specific location. These drinks need to then be sorted by category (brown, white, tomato, beer, wine). I feel like I’m extremely close to a solution, but I’ve been banging my head for the last while.

Here’s my code:

$drinks = get_posts('post_type=drinks&numberposts=-1');
        $show_brown_title   = false;
        $show_white_title   = false;
        $show_tomato_title  = false;
        $show_wine_title    = false;
        $show_beer_title    = false;

        if ( $drinks ) {
            foreach( $drinks as $drink ) {

                $id             = $drink->ID;
                $drink_location = $drink->drink_location;

                if($drink->drink_category == 'Brown' && $drink_location && in_array($site_slug, $drink_location)) {
                    if($show_brown_title == false) {
                        echo '<h4><span>Brown</span> Cocktails</h4>';
                        echo '<ul>';

                        $show_brown_title = true;
                    }

                    echo '<li>';
                    echo '<span class="drink_title">'.$drink->post_title.'</span>';
                    echo '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
                    echo '<span class="drink_price">'.$drink->price_oz.'</span>';
                    echo '</li>';
                }

                if($drink->drink_category == 'White' && $drink_location && in_array($site_slug, $drink_location)) {
                    if($show_white_title == false) {
                        echo '<h4><span>White</span> Cocktails</h4>';
                        echo '<ul>';

                        $show_white_title = true;
                    }

                    echo '<li>';
                    echo '<span class="drink_title">'.$drink->post_title.'</span>';
                    echo '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
                    echo '<span class="drink_price">'.$drink->price_oz.'</span>';
                    echo '</li>';
                }
           }
}

For the most part, this works. However, I’m running into 2 issues.

  1. I can’t figure out how to close the unordered list after I’m done with each category.
  2. This groups by category for the most part, however, if I have a drink that comes later, it will not actually put it into the right category, it’ll just go into whatever category is at the bottom. I’m not sure if this is because I’m not closing the unordered list, or if because of the order that the posts are being pulled from WP.

Let me know if I’m explaining this alright, and I really appreciate any help you guys can offer!

Z

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:27:26+00:00Added an answer on June 17, 2026 at 10:27 pm

    Bellow is my solution.

    Note: get_posts uses the orderby parameter to order the posts by drink_category.

    The code is simplified to only start <ul> when drink_category changes and to end </ul> on category change too. Code also checks that html to output has been assigned a value and appends to it a final </ul> in case one is not appended already.

    $drinks = get_posts('post_type=drinks&numberposts=-1&orderby=drink_category');
    
    $current_category = '';
    $html = '';
    if ( $drinks ) {
        foreach( $drinks as $drink ) {
            $id             = $drink->ID;
            $drink_location = $drink->drink_location;
    
            if ($current_category != $drink->drink_category) {
                if ($current_category != '') {
                    $html .= '</ul>';           
                }
                $current_category = $drink->drink_category;
                $html .= '<h4><span>' . $current_category . '</span> Cocktails</h4>';
                $html .= '<ul>';
            }
    
            if($drink_location && in_array($site_slug, $drink_location)) {
                $html .= '<li>';
                $html .= '<span class="drink_title">'.$drink->post_title.'</span>';
                $html .= '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
                $html .= '<span class="drink_price">'.$drink->price_oz.'</span>';
                $html .= '</li>';
            }
        }
    }
    
    if (strlen($html) > 0 && !endsWith($html, '</ul>')) {
        $html .= '</ul>';
    }
    
    function endsWith($haystack, $needle)
    {
        $length = strlen($needle);
        if ($length == 0) {
            return true;
        }
        return (substr($haystack, -$length) === $needle);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.