• Hi there, I am trying to get the category name for each post that displays, I am using this:

    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>

    There is a parent category (id=16) and the folowing code displays all the posts inside this category. Category 16 is divided in several child categories. The following code displays all posts, most of the time the code above displays the child category name, which is fine for me, but sometimes it seams not being able to find the child, it only displays the parent category name. Any idea of what’s going on here?

    This is the complete code:
    <?php
    query_posts(‘cat=16’);
    ?>

    <?php
    global $more;
    $more = 0;
    ?>
    
    <?php	if (have_posts()) : ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Pr&eacute;c&eacute;dents') ?></div>
    			<div class="alignright"><?php previous_posts_link('Suivants &raquo;') ?></div>
    		</div>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Lien vers <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
    <h3><SPAN class="date"><?php the_time('Y-m-d') ?> /
    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?></SPAN></h3>
    <?php the_content('<br /><br />Lire la suite &raquo;'); ?>
    
    <br />
    <hr size="1" color="black">
    <br />
    
    		<?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Pr&eacute;c&eacute;dents') ?></div>
    			<div class="alignright"><?php previous_posts_link('Suivants &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    
    		  <?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    		  <?php endif; ?>
  • The topic ‘Problem with get_the_category’ is closed to new replies.