获取第一个类别的名称 [英] Get the name of the first category
问题描述
我正在尝试创建一个页面,列出每个类别的内容.我设法创建了列表.我现在需要获取类别的名称.我有以下代码:
<li>此处为类别名称 </li><?php query_posts('cat=0');?><?php while ( have_posts() ) : the_post();?><li><a href="<?php echo get_permalink(); ?>"><?php the_title();?></a><?php endwhile;?>
如何调用第一类(0)的名称?
当前编辑:为什么多个作品行不通?
<ul><?php query_posts('cat=0');?><?php while ( have_posts() ) : the_post();?><li><?php $category = get_the_category();echo $category[0]->cat_name;?><li><a href="<?php echo get_permalink(); ?>"><?php the_title();?></a><?php endwhile;?><div class="first-col"><ul><li><?php $category = get_the_category();echo $category[0]->cat_name;?><?php query_posts('cat=3');?><?php while ( have_posts() ) : the_post();?><li><a href="<?php echo get_permalink(); ?>"><?php the_title();?></a><?php endwhile;?>
您必须获取类别数组并从数组中回显第一个.http://codex.wordpress.org/Function_Reference/get_the_category
cat_name;?>
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>
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;
?>
这篇关于获取第一个类别的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!