0

I'm working on a site that groups content into 'issues' using categories. The home page shows the latest issue by pulling the category with the highest ID (thus the most recent).

What I want to do is to also show the category title and description before the posts in that category loop below. Can anyone point me in the right direction?

<?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
$category = get_category($latest_issue); // it is category object
echo $category->title;
echo $category->description;

in codex: http://codex.wordpress.org/Function_Reference/get_category

But i think you should get category not by last created ID, but by slug:

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

So you should change:

  <?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 )); ?>

to

<?php $issue_category = get_category_by_slug('issue'); ?>
<?php query_posts(array( 'category__in' => $issue_category->term_id )); ?>

and to print your category title and description:

echo $issue_category->title;
echo $issue_category->description;
  • Thanks for your response. The reason for getting the category by last created ID is that I need to show the most recent category of a particular child (an issue) and by sorting them numerically I can do this as the ID will always increase. My assumption is that I cannot do this by slug, but I may be wrong. I also tried echoing the category information but it's showing nothing unfortunately. – Rob Nov 7 '14 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.