WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Listing sub-categories and descriptions automatically (4 posts)

  1. daddysunny
    Member
    Posted 6 years ago #

    Hello folks, I hope you can help me with a small issue.

    I want to be able to access a customised category page (which I've set up, e.g.: 'Campaigns') and then list:

    A sub-category header (eg: 'Neighbourhood watch')
    Its description (eg: 'This is a campaign to get people...')
    Posts filed in that sub-header.

    And loop this for all the sub-categories.

    Right now I'm using this piece of code:

    <li><a href="<?php echo get_category_link(75);?>"><b><?php echo category_description(75); ?></b></a>
    Posts on this campaign:<br/>
    <? global $post;
    $myposts = get_posts('category=75');
    foreach($myposts as $post) : setup_postdata($post); ?>
    - <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/>
    <?php endforeach; ?><br/></li>

    But then I have to manually do this for each sub-category without it doing so in a loop.

    Can anyone suggest an alternative?

    thanks in advance!

  2. Kafkaesqui

    Posted 6 years ago #

    Not sure how you want category name to appear, so:

    <?php
    global $wpdb;
    $categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE parent = '$cat' ORDER BY name ASC");
    foreach($categories as $category) :
    ?>
    
    <h2><?php echo $category->name; ?></h2>
    	<ul>
    	<li><a href="<?php echo get_category_link($category->id);?>"><b><?php echo $category->description; ?></b></a>
    Posts on this campaign:<br/>
    <?php global $post;
    $myposts = get_posts("category=$category->id");
    foreach($myposts as $post) : setup_postdata($post);
    ?>
    - <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/>
    <?php endforeach; ?>
    	</li>
    	</ul> 
    
    <?php endforeach; ?>

    Note this is intended for a category template.

    If you meant some of other type of template (say a custom Page template), edit the $categories = $wpdb->get_results() line to provide the parent category ID. So if the category ID is 10, change:

    parent = '$cat'

    to:

    parent = '10'

  3. mores
    Member
    Posted 6 years ago #

    *bump*, sorry
    For some reason, when I use this code (which helps me a lot!!!), I only get the latest 5 posts. THis code is in a category-1.php file, and I suspect it has something to do with the number of loops I do in the index.php that always show the latest 5 posts.
    How do I make this code show ALL posts, not just 5 ?

    EDIT: fixed this line:

    $myposts = get_posts('numberposts=999&'."category=$category->id");
  4. arminbw
    Member
    Posted 6 years ago #

    Check "Settings/Reading/Blog pages show at most" in your admin backend. Ouch...

Topic Closed

This topic has been closed to new replies.

About this Topic