Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have parent category, Game (cat ID 42), and some child categories in it: Acrade, Causaul. After I using the loop to get the title of the post, I want to have a child category name. But my code is not working:

<?php 
$args = array(
    'cat'            => 42,
    'posts_per_page' => 12,
    'post__in'       => get_option( 'sticky_posts' ),
);

// The Query
$the_query = new WP_Query( $args );
$categories = get_the_category($post->ID);

// The Loop
while ( $the_query->have_posts() ) : 
    $the_query->the_post();
?>

<?php
/* if post has a thumbnail */
if (  ( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() )  ) { ?>

<li>
  <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_post_thumbnail('app_icon', array('title' => get_the_title())); ?></a>
  <p class="ptit">
    <a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a>
  </p>
  <span class="cate"><?php echo $categories ?></span>
</li>

<?php
} /* end if post has a thumbnail */

endwhile;

// Reset Post Data
wp_reset_postdata();
?>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

When you call

$categories = get_the_category($post->ID);

the $post variable is not defined yet (or is not the one that you expect), because you have to use it inside the loop.

Where you have

<span class="cate"><?php echo $categories ?></span>

replace with (this will output something like "Game, Arcade")

<span class="cate"><?php the_category(', '); ?></span>

or if you want remove parent cat

<span class="cate">
<?php
$categories = get_the_category();
if( $categories ){
  $o = '';
  $sep = ', ';
  foreach($categories as $c) {
    if ( $c->parent == 0 ) continue;
    $o .= $c->cat_name . $sep;
  }
  echo rtrim($o, $sep);
}
?>
</span>
share|improve this answer

In the mainpage has working. But in navsidebar, the code is not work. My code in header.php. And i want to ask, how to only get thumbnail the first item, same the 1mobile pageenter image description here

<div class="mb10">
         <div class="sidetoplist">
             <div class="title"><h3 class="tit">Latest Games</h3></div>
             <div class="first"><a href="/subway-surfers-574540.html" class="pic"><img src="http://img.1mobile.com/market/iconv2/36/36542a7ff008ac4ab8440c34b8f02f40-s144.png" alt="Subway Surfers"></a></div>
             <ul class="list clearfix" id="topgames">
                                    <?php  

$args = array( 'cat' => 42, 'posts_per_page' => 10, 'orderby' => 'date' ); // The Query $count = 1; $the_query = new WP_Query( $args );

// The Loop while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                        <li><em class="num"><?php echo $count?>.</em><a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><b><?php the_title(); ?></b><i><?php

$categories = get_the_category(); if( $categories ){ $o = ''; $sep = ', '; foreach($categories as $c) { if ( $c->parent == 0 ) continue; $o .= $c->cat_name . $sep; } echo rtrim($o, $sep); } ?>

                        <?php }
                        $count++;
                    endwhile;

// Reset Post Data wp_reset_postdata(); ?>

             </ul>
         </div>
share|improve this answer

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.