在主页上显示类别元循环

时间:2014-11-06 作者:Rob

我在一个网站上工作,该网站使用类别将内容分组为“问题”。主页通过拉取ID最高的类别(因此是最新的)来显示最新的问题。

我想做的是在下面类别循环中的帖子之前显示类别标题和描述。有人能给我指出正确的方向吗?

<?php $issue = get_terms(\'category\', \'orderby=ID&order=DESC&number=1&child_of=3\'); $latest_issue = $issue[0]->term_taxonomy_id; ?>
                <?php query_posts(array( \'category__in\' => $latest_issue )); ?>
                <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                <article id="post-<?php the_ID(); ?>" <?php post_class(\'clearfix\'); ?> role="article">

                    <header class="home-header">

                        <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( \'wpbs-featured\' ); ?></a>

                    </header> <!-- end article header -->

                    <section class="home-content clearfix">

                        <div class="home-content-header"><h1 class="h2"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> <span><?php the_author_posts_link(); ?></span></h1></div>

                        <section class="post_content home-excerpt clearfix">
                        <?php the_excerpt(); ?>
                    </section>

                </article> <!-- end article -->

                <?php endwhile; ?>  

1 个回复
SO网友:Marcin Bazanowski

$category = get_category($latest_issue); // it is category object
echo $category->title;
echo $category->description;
在法典中:http://codex.wordpress.org/Function_Reference/get_category

但我认为您不应该通过上次创建的ID来获取类别,而应该通过slug来获取类别:

http://codex.wordpress.org/Function_Reference/get_category_by_slug

因此,您应该更改:

  <?php $issue = get_terms(\'category\', \'orderby=ID&order=DESC&number=1&child_of=3\'); $latest_issue = $issue[0]->term_taxonomy_id; ?>
  <?php query_posts(array( \'category__in\' => $latest_issue )); ?>

<?php $issue_category = get_category_by_slug(\'issue\'); ?>
<?php query_posts(array( \'category__in\' => $issue_category->term_id )); ?>
要打印类别标题和说明:

echo $issue_category->title;
echo $issue_category->description;

结束

相关推荐

Show Pages in Categories

通过将此代码添加到函数中,我创建了category函数。php:function page_category() { register_taxonomy_for_object_type(\'category\', \'page\'); } // Add to the admin_init hook of your theme functions.php file add_action( \'init\', \'page_category\' ); 但问