Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

On the main page in WordPress all I want is the category to come before the title of the article posted. So for example if it's a Review, I'd like the article name to be Review: Super 8.

Okay so if you go to my site www.r3entertainment.org it displays the names of each of the articles on the main page. What I want, is for the name of the category the article is in to be placed before the title just on the main page there so people know what type of article it is.

And by the names of the articles on the main page I don't mean the 'Latest Content' widget. I mean the boxes that appear below the huge slider box. Each box has a different article in it. I mean if you can get it to effect the latest content widget too that'd be cool. Didn't think about that. xD

Thanks.

The new code looks like this:

<!--THE POST-->
$getCAT = get_the_category();
$firstCAT = $currentCat[0]->cat_name;<?php if(have_posts()): ?><?php while(have_posts()): ?><?php the_post(); ?>
            <div <?php post_class(); ?> id="post-<?php the_ID(); ?>"> 
             <div class="date"><div class="date_content"><?php the_time('dS'); ?> <?php the_time('M'); ?><span><?php the_time('Y'); ?></span></div></div>
               <div class="post_content">
                <h2 class="postitle"><a href="<?php the_permalink();?>"><?php the_title( $firstCAT,"",true ); ?> </a></h2>
                <div class="single_metainfo">by <?php the_author(); ?></div>
                <?php the_content(); ?>
                <?php wp_link_pages('<p class="pages"><strong>'.__('Pages:').'</strong> ', '</p>', 'number'); ?>
            </div> 

Did I do something wrong?

share|improve this question
    
This question is suffering a severe lack of detail. Do you want to edit the theme? Are you looking for a plugin? How about the code that you're looking to modify/hook to? –  m0r7if3r Feb 4 '12 at 23:39
    
Note that in general, any post can belong to many categories. –  Raphael Feb 5 '12 at 10:29
    
Your new code isn't working and you want to know what is wrong? –  Mamaduka Feb 6 '12 at 11:30

3 Answers 3

Main problem with the other snippets is, that they place get_the_cagetory() function outside to loop, yes you can use it like this, but with passing $post->ID parameter. It would be wise to put that code snippet inside loop and avoid unnecessary global call.

Trevor's loop after this would be something like:

<!-- The Loop -->
<?php if( have_posts() ): while(have_posts()): the_post(); ?>
    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>"> 
        <div class="date"><div class="date_content"><?php the_time('dS'); ?> <?php the_time('M'); ?><span><?php the_time('Y'); ?></span></div></div>
            <div class="post_content">
            <?php $category = get_the_category();
            $first_cat = ( !empty( $category[0] ) ) ? $category[0]->cat_name : ''; ?>
            <h2 class="postitle"><a href="<?php the_permalink();?>"><?php the_title( "$first_cat: " ); ?></a></h2>
                <div class="single_metainfo">by <?php the_author(); ?></div>
                <?php the_content(); ?>
                <?php wp_link_pages('<p class="pages"><strong>'.__('Pages:').'</strong> ', '</p>', 'number'); ?>
            </div> 

note lines after <div class="post_content">.

My second solutions is using the_title filter.

function mamaduka_category_perefix_title( $title, $id ) {
    $category = get_the_category( $id );

    if ( ! empty( $category[0] ) )
        $title = sprintf( '%1$s: %2$s', $category[0]->cat_name, $title );

    return $title;
}
add_filter( 'the_title', 'mamaduka_category_perefix_title', 10, 2 );

But this code has side effect. It will prefix every title inside the loop, including widgets like "Recent Posts".

share|improve this answer
    
Niether of these worked. –  Trevor Feb 6 '12 at 15:15
    
I tested both examples and they work. You need to place last snippet in your theme's functions.php –  Mamaduka Feb 6 '12 at 15:22
    
AWESOME. The 2nd part worked. Thanks man, huuuuge appreciation. –  Trevor Feb 6 '12 at 17:32
    
You're very welcome. –  Mamaduka Feb 6 '12 at 17:34
    
It didn't effect the recent posts widgets on any other page, any idea how I'd get that effect? –  Trevor Feb 6 '12 at 17:36

The simplest way to achieve the desired result is,

$getCAT = get_the_category();
$firstCAT = $currentCat[0]->cat_name;
<?php the_title( $firstCAT,"",true ); ?> 

$getCAT : gets the list of categories where the current post is posted $firstCAT : store the first category of the current post (in case the post is posted in more than one category)

the third line of code is the default wordpress title function that allows you to add something before/after the wordpress title , so i simply pass $firstCAT is the paramter inside the_title

The code is tested and working fine , it outputs the following,

[Category where the post is posted][Post Title]

Usage:

place,

 $getCAT = get_the_category();
    $firstCAT = $currentCat[0]->cat_name;

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

then find this line of code in your index.php or single.php,

<?php the_title(); ?>

replace it with,

<?php the_title( $firstCAT,"",true ); ?> 
share|improve this answer
    
Where do I place that code? I'm assuming in the CSS but, where in the CSS? –  Trevor Feb 5 '12 at 18:02
    
this shouldn't be in CSS, details has been added in the answer –  Sufiyan Ghori Feb 5 '12 at 18:11
    
I did what you said but nothing has changed. Not sure what I'm doing wrong. –  Trevor Feb 5 '12 at 18:40
    
This is what it looks like in the index.php and single.php: –  Trevor Feb 5 '12 at 18:42
    
Er, check my original post since it's too long. –  Trevor Feb 5 '12 at 18:42

you've already had a comment about the lack of detail, so I'll try and guess what you want to do.

Assuming you're inside 'the loop' and your title is being echo'd out with:

<?php the_title(); ?>

..then the easiest way for you to show the title first is to do this:

<?php 
$catobjarray = get_the_category(); 
if($catobjarray[0]){ echo $category[0]->cat_name;}
?>

Caveat of this method: As a post can belong to more than one category, this code will only display the first cat the post is in.

Have a look at the codex for get_the_category() HERE.

share|improve this answer
    
Hmm ok I'll try and be more specific. –  Trevor Feb 5 '12 at 5:07
    
Okay so if you go to my site www.r3entertainment.org it displays the names of each of the articles on the main page. What I want, is for the name of the category the article is in to be placed before the title just on the main page there so people know what type of article it is. –  Trevor Feb 5 '12 at 5:08
    
And by the names of the articles on the main page I don't mean the 'Latest Content' widget. I mean the boxes that appear below the huge slider box. Each box has a different article in it. I mean if you can get it to effect the latest content widget too that'd be cool. Didn't think about that. xD –  Trevor Feb 5 '12 at 5:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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