How to get the categories from Related Posts
-
Sorry if this has been answered elsewhere — if it has, I cannot find the answer — or if this seems like a basic question. But I am trying to use this in my theme:
$related = p2p_type( 'posts_to_pages' )->get_related( get_queried_object() );
and then I start a loop:
if( $related->have_posts() ): while( $related->have_posts() ) : $related->the_post(); // the rest of the loop....
But what I want to do is include the category name in the loop so that I can output something like this:
<ul> <li>Category Name 1 <ul> <li>Post 1</li> <li>Post 2</li> <li>Post 3</li> </ul> </li> <li>Category Name 2 <ul> <li>Post 4</li> <li>Post 5</li> <li>Post 6</li> </ul> </li> </ul>
So I am thinking I need an array of all the categories for posts that are just within the “related” posts query so that I can do a “foreach” loop to achieve this.
I know that get_categories() will give me ALL of the categories in the database. But that is too broad. How to do I get all the categories just for the query I am trying to make above?
- The topic ‘How to get the categories from Related Posts’ is closed to new replies.