如何回显wordpress中的每个帖子类别

时间:2014-07-25 17:21:51

标签: wordpress-theming wordpress

我希望将wordpress打印/回显到帖子的类别名称。我需要这个在主循环内的索引页面上工作。这就是我的意思:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <article class="<?php ** I NEED THIS CODE ** ?>">
            <div>
                <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
            </div>
            <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } else {} ?>
        </article>

    <?php endwhile; ?>
    <?php else : ?>
        <h2>Not Found</h2>
    <?php endif; ?>

希望你理解我对我的问题的不良描述。感谢

1 个答案:

答案 0 :(得分:1)

来自http://lorelle.wordpress.com/2007/09/06/using-wordpress-categories-to-style-posts/

将以下内容添加到主题的functions.php文件中:

function the_category_unlinked($separator = ' ') {
    $categories = (array) get_the_category();

    $thelist = '';
    foreach($categories as $category) {    // concate
        $thelist .= $separator . $category->category_nicename;
    }

    echo $thelist;
}

相应的标记是:

<article class="<?php the_category_unlinked(' '); ?>">