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 6095367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:48:15+00:00 2026-05-23T12:48:15+00:00

Am a bit stuck with a wordpress loop, am wondering if anyone can help.

  • 0

Am a bit stuck with a wordpress loop, am wondering if anyone can help.

I need to run a WordPress loop but only get the category names/id (Either is fine) from each post and have all of those variables as one php item I can echo later in the page.

Its for a category list filter system, but I only want to show categories which have posted displayed on that page.

The loop will be dynamic as well, so I cant just hard code exclude/include, I need to echo the value of all the numbers in together.

I hope that makes sense! Anyone who has any ideas would be really cool. Thanks!

  • 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-05-23T12:48:16+00:00Added an answer on May 23, 2026 at 12:48 pm

    I would use the get_the_category function like so…

    <?php
    // before you begin the wordpress loop
    $category_array = array();
    ?>
    
    <?php
    // from *within* the wordpress loop
    foreach((get_the_category()) as $category) { 
        if (!in_array($category->cat_name, $category_array)) {
            $category_array[] = $category->cat_name;
        }
    }
    ?>
    
    <?php
    // after the wordpress loop is finished
    echo implode(",", $category_array);
    ?>
    

    This code basically creates a new (empty) array so that for each category in the current page, check if you’ve already added that category name to the array, then if not, go ahead and add it. Then when the loop is finished, echo out a comma separated string of category names. (You can of course, change the separator if you want a comma and space “, ” or any other delimiter).

    The Codex article has a lot more information on other things you can do with that function. Hope that helps.

    Edit: Fixed the implementation because I forgot this was going to be used on a page where you are listing many posts using the loop. (You need to initialize your array from outside the wordpress loop, then echo your results after the loop has been finished).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone can help me, I am a bit stuck. I have this query which
I'm a bit stuck on what is probably a common situation, but can't find
Im a bit stuck with my lighting. My light can light up meshes but
bit stuck try to achieve something in Jquery and wondered if anyone can assist..
I'm working on learning Objective-C/Coaoa, but I've seem to have gotten a bit stuck
I am a bit stuck here and can't think further. public struct CandidateDetail {
I seem to be a bit stuck on this, but my experience in Linq
I'm experimenting with WPF animations, and I'm a bit stuck. Here's what I need
I'm a bit stuck trying to loop through a table row, calculate the values
I'm a bit stuck trying to get box2D to compile and I think it's

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.