Reputation:
I am trying to create a single page that lists the content of each category. I have managed to create the list. I now need to get the name of the category. I have the following code:
<ul>
<li> CATEGORY NAME HERE </li>
<?php query_posts('cat=0'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo get_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
How to call the name of the first category (0)?
Current edit: Why won't multiple works?
<div class="first-col">
<ul>
<?php query_posts('cat=0'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<li> <?php $category = get_the_category();
echo $category[0]->cat_name;
?> </li>
<li>
<a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="first-col">
<ul>
<li> <?php $category = get_the_category();
echo $category[0]->cat_name;?> </li>
<?php query_posts('cat=3'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
</div>
Upvotes: 11
Views: 26424
Reputation: 330
According to wordpress developers codex:
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
}
This will give you the first category and also link it to that category's page.
Upvotes: 4
Reputation: 297
there is also a shortcodes plug in that will help create lists based on categories, terms, etc. http://wordpress.org/plugins/display-posts-shortcode/
Upvotes: -1
Reputation: 281
You have to get the array of categories and echo the first one from the array. http://codex.wordpress.org/Function_Reference/get_the_category
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
Upvotes: 25