Category title inside the loop but not repeat

748 views
Skip to first unread message

barbara schendel

unread,
Jan 29, 2012, 9:39:50 PM1/29/12
to Minneapolis St. Paul WordPress User Group
Hey all,
Is there a way to put the title of the cateogry inside of the loop,
but done in such a way that it only displays once?

The objective is for it to output the Category name, then post title
and post excerpt & thumbnail for each post in that category... BUT if
there are no posts in that category, it doesn't print the cat name.
AND I don't want it to repeat the category name before each post. Just
print it once.

I'm sure this is probably easy but for some reason I can't wrap my
head around how to do it. Any suggestions?

Thanks!
-barbara-

DaveE

unread,
Jan 30, 2012, 10:39:08 AM1/30/12
to mpls-stpau...@googlegroups.com
Hi Barbara,

If I understand your question correctly, you'd like to display the category title above the posts loop for the category currently being viewed.  To do that, you can use the single_cat_title() function.  Inside your category template, in place of where you want the title to show (outside the loop), insert this code: <?php single_cat_title(); ?>.  There are some additional options for the single_cat_title function, feel free to check them out in the WordPress Codex.

Cheers!

Sherman Bausch

unread,
Jan 30, 2012, 10:59:41 AM1/30/12
to mpls-stpau...@googlegroups.com
The "Loop" Code on your template can be boiled down to something like this…

  <?php if (have_posts()) : ?>
               <?php while (have_posts()) : the_post(); ?>    
               <!-- do stuff ... -->
               <?php endwhile; ?>
     <?php endif; ?>
All you need to do to accomplish your goal is to make sure the call for category title is included inside the "if" statement; like this.

<?php if (have_posts()) : ?>
<?php single_cat_title( $prefix, $display ); ?> 

That way if you don't have posts the category title won't be rendered.

Cheers
 - Sherman



--
You received this message because you are subscribed to the Google Groups "Minneapolis St. Paul WordPress User Group" group.
To post to this group, send email to mpls-stpau...@googlegroups.com.
To unsubscribe from this group, send email to mpls-stpaul-word...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mpls-stpaul-wordpress?hl=en.


barbara schendel

unread,
Jan 31, 2012, 11:54:02 AM1/31/12
to Minneapolis St. Paul WordPress User Group
single_cat_title would be a great idea if this was a normal category
archive, but it does not work for this. (nothing is output when I use
it)

A little background, This is an online magazine -- each month there
are articles in several different categories. When we view the
archives, we are viewing the archives for one particular month, with
excerpts of all of the articles from each category grouped together.

This is probably easier than I am making it, but I kind of suck at
loops and php (what can I say I am still more of a designer than
anything) so I think the way I'm doing this is probably really
backwards. I have a couple of places where I need to be able to spit
out excerpts and category titles... [please don't laugh at my
code :) ]

1) front-page.php, which is the current issue. It has one loop for
each category and it looks like this:

$today = getdate(); // I only want the current month to show
$query = new WP_Query( 'year=' . $today["year"] . '&monthnum=' .
$today["mon"] . '&cat=5&orderby=date&order=ASC' );
<h1>Category title hard coded</h1>
if ( have_posts() ) while ( have_posts() ) : the_post();
title, excerpt, thumbnail, etc
end loop
-- repeat the above for each category


2) archive.php ... I could not write a query specifying category on
this page or it grabbed all the months. And I could not figure out how
to make it just grab the month the user chose on the archives widget.
So I'm letting it do what it does naturally and just modified it to
look like this:

<h1>Cat title hard coded</h1>
if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php if (in_category( 5 )) { ?>
excerpt, title, thumbnail, etc
end if
end loop
-- repeat the above for each category


3) and finally sidebar.php shows up when they are viewing a single
article from any given issue. I want it to show the article titles
(and cat titles) of the issue they are currently viewing. I am not
sure how to do this. So right now it just shows the most recent
issue. It does this pretty much in the same way I did it in #2 above,
except that I start it out with this
$query = new WP_Query( 'year=' . $today["year"] . '&monthnum=' .
$today["mon"] . '&orderby=date&order=ASC' );?>
but again this is not what I actually want. I'd rather have whatever
month the single article is from.

If anyone could help out, I would love a hand with any of this, since
like I said, I kind of suck at this part of the job.

Humbly,
-barbara-

Toby C

unread,
Jan 31, 2012, 2:41:55 PM1/31/12
to mpls-stpau...@googlegroups.com

barbara schendel

unread,
Feb 1, 2012, 10:16:20 AM2/1/12
to Minneapolis St. Paul WordPress User Group
the_category() does seems to work except again because it's inside
the loop, it repeats for each article.

I am thinking of setting a little variable switch to on or off, so
that it remembers if it showed or not. A little duct-tapey, but it
should work.

Anybody know how to make the sidebar show the archive for the month
that the currently-viewed single article is in? I need a way to grab
what month they clicked on when they click to display an archive by
date.

Ideally it seems there should be a way to do this all with one
template instead of using three. If anyone can think of a more
elegant solution, I'd really love to hear about it. This kind of
functionality seems ideal for wordpress, which is why it seems to me
like I must be overthinking it. Am I ?





On Jan 31, 1:41 pm, Toby C <tcr...@gmail.com> wrote:
> Barbara,
> Have you tried *the_category()*?
>
> Toby
>
> ----
> t...@themightymo.comhttp://themightymo.com
> (612) 293-8629
>
> www.twitter.com/themightymowww.linkedin.com/in/tobycryns

Sherman Bausch

unread,
Feb 1, 2012, 10:20:44 AM2/1/12
to mpls-stpau...@googlegroups.com
If you drop it after have_posts() but before the "while" loop it should only appear once per loop.
Reply all
Reply to author
Forward
0 new messages