9

我正在尝试创建一个列出每个类别内容的页面。我设法创建了列表。我现在需要获取类别的名称。我有以下代码:

<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>

如何调用第一类(0)的名称?

当前编辑:为什么不会有多个作品?

<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>
4

3 回答 3

20

您必须获取类别数组并从数组中回显第一个类别。 http://codex.wordpress.org/Function_Reference/get_the_category

<?php
$category = get_the_category(); 
echo $category[0]->cat_name;
?>
于 2013-08-30T15:06:42.753 回答
4

根据 wordpress 开发人员的 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>';
}

这将为您提供第一个类别并将其链接到该类别的页面。

于 2018-08-24T22:07:26.333 回答
-1

还有一个短代码插件,可以帮助创建基于类别、术语等的列表。http://wordpress.org/plugins/display-posts-shortcode/

于 2013-08-30T15:15:49.670 回答