0

Here's what I have:

Custom Post type: Vendors

Vendor categories:

  • Wedding
  • Party
  • Catering

I want to query all Vendors and then assign them a class based on the category they are in. The class would be .wedding if they are categorized as wedding and .party for party and so on.

$args = array(
   'post_type' => 'vendors',
   'order' => 'asc',
   'orderby' => 'title',
);
$query = new WP_Query( $args );

So that grabs the posts in order by title. I want to add the class based off of their category and echo them as a list item as so:

<ul>
   <li class=".category">TITLE</li>
   <li class=".category">TITLE</li>
<ul>

I also want to query from the posts and categories that are made in Wordpress, not from some pre-defined list since this will be changing every now and then. I know I'll want to do a foreach loop but I'm not sure how to find their taxonomy and echo it as a class.

2
  • In your list, is TITLE the category title or post title? Do you even need posts or just categories? Is there a specific class you want or do you want each category to have a unique class ( think slug ) ?
    – Howdy_McGee
    Commented Nov 20, 2014 at 22:33
  • Title is the post title.
    – FranticJ3
    Commented Nov 21, 2014 at 22:41

1 Answer 1

0

Alright I figured it out!

$args = array(
'post_type' => 'vendors',
'order' => 'asc',
'orderby' => 'title',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
    $query->the_post();
    $category = get_the_category();
    echo '<li class="';
    echo $category[0]->category_nicename;
    echo '">' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
1
  • Please accept your own answer so we can get this one solved and closed. :-) Commented May 7, 2015 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.