WordPress.org

Ready to get started?Download WordPress

Forums

Display category slug, description and parent (2 posts)

  1. elphi
    Member
    Posted 6 years ago #

    The original code that will display 3 categories which shows 1 recent post for each category:

    <?php
    $display_categories = array(5,6,7);
    foreach ($display_categories as $category) { ?>
    
    <div class="clearfloat">
    
    <?php query_posts("showposts=1&cat=$category");
    $wp_query->is_category = false;
    $wp_query->is_archive = false;
    $wp_query->is_home = true;
    ?>
    
    <h3><a href="<?php echo get_category_link($category);?>"><?php
    single_cat_title(); ?></a></h3>
    
    <?php while (have_posts()) : the_post(); ?>
    
    <?php the_excerpt(); ?>
    
    <?php endwhile; ?>
    
    </div>
    
    <?php } ?>

    From that code, I dont know what code to replace on the capitalize word below to display parent, slug and description of the listed category:

    <?php
    $display_categories = array(5,6,7);
    foreach ($display_categories as $category) { ?>
    
    <?php query_posts("showposts=1&cat=$category");
    $wp_query->is_category = false;
    $wp_query->is_archive = false;
    $wp_query->is_home = true;
    ?>
    
    <h3><a href="<?php echo get_category_link($category);?>"><?php
    single_cat_title(); ?></a></h3>
    
    <?php echo CATEGORY PARENT;?>
    
    <?php echo CATEGORY SLUG;?>
    
    <?php echo CATEGORY DESCRIPTION;?>
    
    <?php while (have_posts()) : the_post(); ?>
    
    <?php the_excerpt(); ?>
    
    <?php endwhile; ?>
    
    </div>
    
    <?php } ?>

    -------------------------

    what i did so far:

    1. I have no problem in pulling the category description:

    i use this code: <?php echo category_description('$category'); ?>

    2. but for the category parent and slug(nicename) i cant figure out how.

    from wordpress codex (http://codex.wordpress.org/Template_Tags/get_category_parents), if I use this code:
    <?php echo(get_category_parents($cat, TRUE, ' » ')); ?>

    this will result to:
    Internet » Blogging » WordPress »

    How can I make it display Category Parent Only (eg. 'Internet')?

    3. on pulling the category slug, i'm clueless.

    thanks for the upcoming responses! any help would be greatly appreciated! =)

  2. estsn
    Member
    Posted 6 years ago #

    Substitute this for the appropriate capitalized terms:

    $category->slug;
    $category->description;
    $category->parent;

    Look at the database schema for Terms (or categories in older WordPress) - basically any field can be called by the field name you see in the table.

    $category->parent returns the ID of the parent category, so if you need something else you'll need to use the method calls that take cat_ID as an argument.

    (http://www.estsn.com/ - it's WordPress)

Topic Closed

This topic has been closed to new replies.

About this Topic