WordPress.org

Ready to get started?Download WordPress

Forums

Get post from category and groupby subcategory (1 post)

  1. paszczak000
    Member
    Posted 3 years ago #

    I have some category with few subcat:

    topcat:
    - subcat1
    - subcat2
    - subcat3

    I want to show post list from topcat grouped by subcats. I create template:

    <?php
    $args = array(
        'type'		=> 'post',
        'child_of'		=> 6,
        'orderby'		=> 'name',
        'order'		=> 'ASC',
        'hide_empty'	=> 1,
        'hierarchical'	=> 1,
        'taxonomy'		=> 'category',
        'pad_counts'	=> false );
    $categories = get_categories( $args );
    foreach($categories as $category) : ?>
    	<h2><?php print($category->name); ?></h2>
    	<p><?php print($category->description); ?></p>
    	<ul>
    	<?php
    		global $post;
    		$args = array(
    			'numberposts' => '',
    			'category' => $category->cat_ID );
    		$myposts = get_posts( $args );
    		foreach( $myposts as $post ) :	setup_postdata($post); ?>	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php endforeach; ?>
    	</ul>
    <?php endforeach; ?>

    But this solution is not good. How can I create the same using some SQL query?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.