Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this piece of ode in a buy template. I like to know what to add to get the child name of the category

the mais display category is sponsor. but in the sponsor category, there is : gold, silver, platium... i want to get that "color" category an output it as <div class="cat-name">platium</div>

here is the code :

<div id="carouselContainer">
<h2 id="sponsorsTitle"><?php echo get_cat_name( $carouselCategory ) ?></h2>
<ul id="carousel">
    <?php $showPostsInCategory = new WP_Query(); $showPostsInCategory->query('cat='. $carouselCategory .'&showposts='. $carouselNumber .'');
    if ($showPostsInCategory->have_posts()) : while ($showPostsInCategory->have_posts()) : $showPostsInCategory->the_post();?>
        <li>
            <?php $data = get_post_meta( $post->ID, 'key', true ); ?>
            <?php echo get_cat_name( $carouselCategory ) ?>
            <a href="<?php  if ($data[ 'custom_link' ]) { echo $data[ 'custom_link' ];} else { the_permalink(); } ?>">
                <?php the_post_thumbnail('sponsor', array('title' => "")); ?>
            </a>
        </li>
    <?php endwhile; endif; ?>
</ul><!--end carousel-->
</div><!--end carouselContainer-->
<?php } ?>

<div id="footerContainer">
<div id="footer"> 
share|improve this question
1  
Are you looking for something like wp_get_post_categories()? I'm not sure what you're trying to get the child category of, as you are querying posts. – m0r7if3r Mar 11 '12 at 15:50

3 Answers

up vote 0 down vote accepted

menardmam, this is my solution:

    <?php $showPostsInCategory = new WP_Query(); $showPostsInCategory->query('cat='. $carouselCategory .'&showposts='. $carouselNumber .'');
     if ($showPostsInCategory->have_posts()) :  while ($showPostsInCategory->have_posts()) : $showPostsInCategory->the_post(); ?>
      <li>
           <?php $data = get_post_meta( $post->ID, 'key', true ); ?>
           <?php foreach((get_the_category()) as $category) {echo ( $category->cat_name != $carouselCategory ) ? $category->cat_name . ' ' : '';}?>
           <a href="<?php  if ($data[ 'custom_link' ]) { echo $data[ 'custom_link' ];} else { the_permalink(); } ?>">
           <?php the_post_thumbnail('sponsor', array('title' => "")); ?>
           </a>
      </li>
    <?php endwhile; endif; ?>

The get_the_category retrieves every category (parent and child) of your post, but by using the $category->cat_name != $carouselCategory part the parent category won't be displayed, just the child.

share|improve this answer
this is just perfect... but i have found a solution myself... i will get you my solution, but mark you as the approved answer ! – menardmam Mar 13 '12 at 0:39
<?php 
function get_me_the_child_cat($arr, $val){
    foreach ($arr as $key => $value){
    if ($arr[$key] == $val){
    unset($arr[$key]);
    }
    }
    $arr_clean = array_values($arr);
    return (get_cat_name( $arr_clean[0]));
}
?>

<div class="clear"></div>
</div><!--end content-->
</div><!--end contentContainer-->


<div id="carouselContainer">
<h2 id="sponsorsTitle"><?php echo get_cat_name( $carouselCategory ) ?></h2>
<div id="carousel">
<ul>
    <?php $showPostsInCategory = new WP_Query(); $showPostsInCategory->query('cat='. $carouselCategory .'&showposts='. $carouselNumber .'');
    if ($showPostsInCategory->have_posts()) : while ($showPostsInCategory->have_posts()) : $showPostsInCategory->the_post();?>
        <li class="spacerli">
            <?php $data = get_post_meta( $post->ID, 'key', true ); ?>
            <div class="headerlogo"><?php $all_cat = wp_get_post_categories($post->ID); echo (get_me_the_child_cat($all_cat,$carouselCategory));?></div>
            <a href="<?php  if ($data[ 'custom_link' ]) { echo $data[ 'custom_link' ];} else { the_permalink(); } ?>">
                <?php the_post_thumbnail('sponsor', array('title' => "")); ?>
            </a>
        </li>
    <?php endwhile; endif; ?>
</ul>
share|improve this answer
    <ul>
<?php
    $blogCategoryID = "5";
    $childCatID = $wpdb->get_col("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=$blogCategoryID");
    if ($childCatID){
        foreach ($childCatID as $kid)
        {
            $childCatName = $wpdb->get_row("SELECT name, term_id FROM $wpdb->terms WHERE term_id=$kid");
            echo '<li>'.$childCatName->name.'</li>';
        }
    }

?>

    </ul>

This is get child categories of the parent category id =5.

share|improve this answer
Sorry to say, but this is wrong in lots of ways. Please use the internal API functions for that or prepare your queries. – kaiser Sep 21 '12 at 16:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.