WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Echoing Multiple Category Names from single Post (5 posts)

  1. Lee
    Member
    Posted 6 months ago #

    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 | Date

    Everything 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 | Date

    I'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?

  2. Jeffrey van Rossum
    Member
    Posted 6 months ago #

    Hello,

    You could create an array and implode it comma seperated.

    Something like this:

    foreach ( $media as $link ) {
    
    	// Setting categorie arrays
    	$categories = array();
    	$post_categories = get_the_category( $link->ID );
    
    	// Loop through post categories
    	foreach( $post_categories as $category ) {
    		if( $category->term_id == 39 ){
    			// Do nothing
    		} else {
    			$categories[] = $category->name; // Add category name to array
    		}
    	}
    
    	// Comma seperate the categories array
    	$categories_list = implode( ", " , $categories );
    
    	// Custom fields
    	$media_link = get_post_meta( $link->ID, 'media_link', true );
    
    	// Output
    	echo '<tr>';
    	echo '<td><a class="media-link" href="'. $media_link .'" target="_blank">'. $link->post_title .'</a></td>';
    	echo '<td>'. $categories_list .'</td>';
    	echo '<td>'. mysql2date('F j, Y', $link->post_date) .'</td>';
    	echo '</tr>';
    
    }

    This should work.

  3. speedito
    Member
    Posted 6 months ago #

    Why do you have a loop for categories within the first loop. I would approach it like this:

    $media = "";
    $args = array(
    	'orderby'		=> 'date',
    	'order'			=> 'DESC'
    );
    
    $media = get_posts( $args );
    
    foreach ( $media as $link ) {
    		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 (!(in_category( 39 ))) {
    
    			echo '<td>';
    			the_category(' | ');
    			echo '</td>';
    		}
    
    		echo '<td>'.mysql2date('F j, Y', $link->post_date).'</td>';
    		echo '</tr>';
    }
    
    $mediaLinks = ob_get_contents();
    ob_end_clean();
    
    echo $mediaLinks;
  4. Jeffrey van Rossum
    Member
    Posted 6 months ago #

    @speedito: You´re right. I was thinking to difficult and forgot about the the_category function. Thanks.

  5. Lee
    Member
    Posted 6 months ago #

    Jeffreyvr,

    Thanks for the reply! You're code worked. I didn't have any luck getting speedito's code to work though unfortunately.

    I made one small change to yours. Instead of your line:

    // Loop through post categories
    	foreach( $post_categories as $category ) {
    		if( $category->term_id == 39 ){
    			// Do nothing
    		} else {
    			$categories[] = $category->name; // Add category name to array
    		}
    	}

    I condensed it to:

    // Loop through post categories
    	foreach( $post_categories as $category ) {
    		if(!( $category->term_id == 39 )){
    			$categories[] = $category->name; // Add category name to array
    		}
    	}

    Here is the final working code if anyone else needs it:

    <?php $media = "";
    $args = array(
    	'orderby'		=> 'date',
    	'order'			=> 'DESC'
    );
    
    $media = get_posts( $args );
    
    foreach ( $media as $link ) {
    
    	// Setting category arrays
    	$categories = array();
    	$post_categories = get_the_category( $link->ID );
    
    	// Loop through post categories
    	foreach( $post_categories as $category ) {
    		if(!( $category->term_id == 39 )){
    			$categories[] = $category->name; // Add category name to array
    		}
    	}
    
    	// Comma seperate the categories array
    	$categories_list = implode( ", " , $categories );
    
    	// Custom fields
    	$media_link = get_post_meta( $link->ID, 'media_link', true );
    
    	// Output
    	echo '<tr>';
    	echo '<td><a class="media-link" href="'. $media_link .'" target="_blank">'. $link->post_title .'</a></td>';
    	echo '<td>'. $categories_list .'</td>';
    	echo '<td>'. mysql2date('F j, Y', $link->post_date) .'</td>';
    	echo '</tr>';
    
    }
    
    $mediaLinks = ob_get_contents();
    ob_end_clean();
    
    ?>
    
    <?php echo $mediaLinks; ?>

Reply

You must log in to post.

About this Topic