WordPress.org

Ready to get started?Download WordPress

Forums

Display Sub Categories of Current Parent Category if there are posts (7 posts)

  1. CodeyMonkey
    Member
    Posted 2 years ago #

    Hi,

    When viewing a "Parent Category" i'd like to list the title of the "Sub-Categories" that only contain posts, i've been through documentation and can't find anythign on this, hope someone can help.

    For example:
    I'f i'm viewing the category "Portfolio". mysite.com/portfolio/

    It would Display categories like "Photography", "Design" etc etc. Seems simple in theory.

    But only show these sub-categories if they have posts within them.

    Hope that makes sense.

    Note: I don't want to have to hardcore the category name e.g . (IF category ="Portoflio") - it must be dynamic.

    Also I don't need it to be linked, just to pull the ptitles in plain text.

    Thanks.

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    When viewing a "Parent Category" i'd like to list the title of the "Sub-Categories" that only contain posts

    assuming you are in a category archive page:

    get_categories('child_of='.get_query_var('cat'));

    or use the 'parent' parameter to get only direct child cats.

    by default, it is set to not show categories without posts:
    'hide_empty' => 1

    http://codex.wordpress.org/Function_Reference/get_categories

  3. CodeyMonkey
    Member
    Posted 2 years ago #

    This doesn't work unfortauntely :(

    I'd like to have more styling over this for example, ideally i'd like:

      (Query loop here)

      <li >(Sub Category ID)(Sub Category Title)</li >
      (End Queryy)

    Hope that makes sense.

  4. CodeyMonkey
    Member
    Posted 2 years ago #

    OK, to be more specfic, I chnaged the first line of the example, to what you said e.g.

    <?php
      $categories=  get_categories('child_of='.get_query_var('cat'));
      foreach ($categories as $category) {

    But it's still pulling the title of the "Parent" e.g. "Portfolio", within the results any ideas? - Whcih it shouldn't if I want it to show only ones with posts within it. (excluding the parent)

  5. CodeyMonkey
    Member
    Posted 2 years ago #

    Can someone help please, the above code pulls the parent as well as the child categories, when I only want just the "children" only.

  6. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    But it's still pulling the title of the "Parent" e.g. "Portfolio", within the results any ideas?

    the code is supposed to work in a category arcghive page only. and it should not show the category of the archive.

    can you paste the full code of your template into a http://pastebin.com/ and post the link to it here?

    ideally, can you post a link to your site?

  7. CodeyMonkey
    Member
    Posted 2 years ago #

    I have a "Parent Category" which works as /category/portfolio/

    And the category is named category-portfolio.php.

    All I have inside is:

    <?php query_posts('showposts=25'); while (have_posts()) : the_post(); ?>
    <li data-type="<?php $category = get_the_category(); echo $category[0]->cat_ID; ?>"></li>
    <?php endwhile; ?>
    
    <?php
    $args=array(
      'orderby' => 'name',
      'order' => 'ASC'
      );
     $categories=  get_categories('child_of='.get_query_var('cat'));
      foreach($categories as $category) {
        echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        echo '<p> Description:'. $category->description . '</p>';
        echo '<p> Post Count: '. $category->count . '</p>';  }
    ?>

    And this produces:

    Category: Photography
    Description:Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Post Count: 1

    Category: Portfolio
    Description:Portfolio description would go here
    Post Count: 0

    Category: Illustration
    Description:Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Post Count: 1

    Why is portfolio even showing!!?

Topic Closed

This topic has been closed to new replies.

About this Topic