Echoing Multiple Category Names from single Post
-
Hi,
I am having a problem that I need some help with if possible. I am trying to output a set of posts in a table using the following structure:
Column 1 | Column 2 | Column 3
Post Title | Category1, Category 2 | DateEverything is working fine except some posts belong to multiple categories and some belong to only one category. So in the case of a post that belongs to more than one category, it is being output into its own row for each category. So it is looking like this:
Column 1 | Column 2 | Column 3
Post Title1 | Category 1 | Date
Post Title1 | Category 2 | DateI’d like it to have its own row with both listed in one table td cell, separated by a comma.
Here is my current code:
$media = ""; $args = array( 'orderby' => 'date', 'order' => 'DESC' ); $media = get_posts( $args ); foreach ( $media as $link ) { foreach(get_the_category($link->ID) as $category) { echo '<tr>'; echo '<td><a class="media-link" href="'.get_post_meta($link->ID,'media_link',true).'" target="_blank">'.$link->post_title.'</a></td>'; if (!($category->cat_ID == '39') && !empty($category)) { echo '<td>'.$category->name.'</td>'; } echo '<td>'.mysql2date('F j, Y', $link->post_date).'</td>'; echo '</tr>'; } } $mediaLinks = ob_get_contents(); ob_end_clean(); echo $mediaLinks;
The if statement is in there because I am wanting to exclude the single category that is being used to group these posts from what it displays and that works sort of.
Is there anyone that knows how to do this and would be willing to help me figure it out?
- The topic ‘Echoing Multiple Category Names from single Post’ is closed to new replies.