Support » Fixing WordPress » Twenty Eleven Adding category title in single posts

  • Resolved insurgenesis


    How do I add category headings / titles to single posts?

    The problem is really that single posts are removed from the category sequence and so you can’t cycle through to adjacent posts within the same category unless the navigation is customised like this:
    <?php previous_post_link('%link', 'Previous in category', TRUE); ?>
    BUT it’s very confusing if someone stumbles upon a single post where the category to which it belongs is omitted.

    All I know is that I can’t simply get category headings in single posts by using the code found in category.php :

    I don’t know why this is the case.
    Perhaps there is a more economical way.
    Anyone felt the same about this?

Viewing 15 replies - 1 through 15 (of 23 total)
  • When trying to show only the first categories name use the below:

    $category = get_the_category($post->ID);
    echo $category[0]->cat_name;

    if you want to make it a link use the below:

    $category = get_the_category($post->ID);
    echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';

    note that this code will only work WITHIN the loop…

    Hi tiaanswart, thanks for stopping by (BTW you from South Africa?)

    What do you mean when you say:

    only the first categories name

    When would that be useful?

    No, I need the category name in which a single post appears to be visible above the post itself so that, no matter where you are or how you got there, you would still know how to sensibly navigate through to the next post (in the same category).I hope you understand what I mean.

    My problem is that I can’t find a way to copy this over into single.php and/or content-single.php. It doesn’t work and I think there must be a better way.

    Yes I’m from South Africa, Krugersdorp.

    Ok so if i understand correctly you want to display the category(or categories) which the post is linked to be displayed above the post in a single view?

    If so the code above should point you in the correct direction as it can be inserted within the loop where you want to display the category/categories.

    If not then please collaborate as i am not to sure what you are trying to accomplish here…

    If this issue is about pagination, I checked the code on my own site for pagination on my custom taxonomy skills and my code seems to be working 100%:

    <?php if(($wp_query->found_posts) > (get_option('posts_per_page'))) :?>
        <!-- Page Navigation -->
        <?php if (function_exists('wp_pagenavi')) : ?>
        <div class="pagenavi">
            <?php wp_pagenavi(); ?>
        <?php else : // Use WordPress default page navigation. ?>
        <div class="pages">
            <span class="older"><?php next_posts_link('&laquo; Older Entries'); ?></span>
            <span class="newer"><?php previous_posts_link('Newer Entries &raquo;'); ?></span>
        <?php endif; ?>
        <?php endif; ?>

    Hello brother,

    If so the code above should point you in the correct direction as it can be inserted within the loop where you want to display the category/categories.

    Actually, as I explained, it cannot be done. There has to be another way to get single.php to use the same navigation style as category.php.

    This is what’s happening:
    I’m viewing post 1 in category A (using category.php), then click a text link and get taken to a different post, let’s say post 4 in category C (it switches to using single.php) and the pagination works differently and I can’t cycle through all the posts in category C! Suddenly the “next” and “previous” navigation no longer confines the posts to Category C and I can jump to random places!! This happens because navigation for category.php is different from the navigation used for single.php.

    This is what should be happening:
    From post 1 in Category A (uses category.php), click a link and go to post 4 in Category C (ALSO uses category.php NOT single.php), and I can navigate only in Category C – why? Because I’m in Category C now and want to get the full picture of what category C has to offer without getting lost in the matrix.

    Do you know what I mean?
    Anyway here is how my quest has panned out up to now:

    Ok here’s what i did…

    I created a custom post type called ‘portfolio’ then i created a taxonomy called ‘skills’.

    I then created a page called ‘portfolio’ and linked it to a custom template (portfolio.php).

    in the template (before the post loop) i list all custom taxonomy links and then i list the posts in the custom post type.

    when i navigate to one of the skills links like ‘CSS’ my posts for that archive is displayed along with my pagination links which works fine for navigating through that specific skill?

    Now what you want to do from my understanding is slightly different as you want to list the category within the post loop within the content area of the post? Also you want to list this within a single template file…

    i’m going to quickly test this and get back to you on that…

    Well mine works so i don’t know i’m probably just not understanding you correctly?

    OK thanks for confirming that.
    Answer me this:
    Where in the single.php should I drop in this or similar:

    <header class="page-header">
    <h1 class="page-title"><?php
    printf( __( '%s', 'twentyeleven' ), '<span>' . single_cat_title( '', false ) . '</span>' );
    $category_description = category_description();
    if ( ! empty( $category_description ) )
    echo apply_filters( 'category_archive_meta', '<div class="category-archive-meta">' . $category_description . '</div>' );

    to produce category headings on single posts?

    When I do this I see traces of it with firebug but it’s not fully visible, maybe because I pasted it in the wrong place?
    One would omit the <header> bit precisely because single.php only closes its <header> in content-single.php…

    Well my code is a bit different i did the below to get the skills with their links:

    <?php if ( get_the_terms( $post->ID, 'Skills' ) ) : ?>
                <div class="meta-values">
                   <p>More Info on this Portfolio Item:</p>
                    <table border="0" cellspacing="0" cellpadding="0">
    	                    <td valign="top" width="200">
                            <td valign="top" width="400">
                                <?php the_terms( $post->ID, 'Skills', '', ', ', ' ' ); ?>
            <?php endif; ?>

    I’m sorry but I haven’t the skills to translate that to to mine.
    I should mention that my (child theme) template files are standard Twenty Eleven ones with minor tweaks. So please understand that what you’ve posted is foreign to me and probably very different from my case.

    Do you perhaps have a fresh copy of Twenty Eleven around?
    If you compare the standard category.php template and the singles you’ll know what I’m on about.

    Just to recap: I want single.php to look and work like category.php. I have no use for the singles.

    ok i give up hahaha….

    are you also from SA and do you understand Afrikaans?

    Maybe if you explain it that way i can get a fresh look at it?

    the first though that comes to mind would be to copy ALL the code from category.php to single.php since the loop in single.php will only find one post but keep the look and feel of category.php it should be what you were looking for in :

    in this specific string of posts here i would suggest that you amend your above code to something like this:

    <div class="page-header">
    <h1 class="page-title"><?php
    printf( __( '%s', 'twentyeleven' ), '<span>' . single_cat_title( '', false ) . '</span>' );
    $category_description = category_description();
    if ( ! empty( $category_description ) )
    echo apply_filters( 'category_archive_meta', '<div class="category-archive-meta">' . $category_description . '</div>' );

    and then paste it above the below line of code:

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

    however i am not to sure how the pagination would be done then as pagination on the twenty eleven theme goes through the below code:

    <?php twentyeleven_content_nav( 'nav-above' ); ?>

    from function.php:

    if ( ! function_exists( 'twentyeleven_content_nav' ) ) :
     * Display navigation to next/previous pages when applicable
    function twentyeleven_content_nav( $nav_id ) {
    	global $wp_query;
    	if ( $wp_query->max_num_pages > 1 ) : ?>
    		<nav id="<?php echo $nav_id; ?>">
    			<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentyeleven' ); ?></h3>
    			<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyeleven' ) ); ?></div>
    			<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?></div>
    		</nav><!-- #nav-above -->
    	<?php endif;
    endif; // twentyeleven_content_nav

    Thanks, it’s identical to what I’ve posted – except the closing </div> at the end.
    Where’s the opening for it?

    opening and closing are different as i changed it from a header to a div tag

    Still, no luck.
    Would it help, I mean for clarity’s sake, if I place a pastebin link to my single.php?

    If you haven’t become impatient, it’s here:

    no hahaha i’m truly lost here i think i know what you want now but i tested it in my code and a weird result with pagination…

    when paginating singe.php it worked but the post that i wanted to diplay did not display instead i got another post from that very same category??

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘Twenty Eleven Adding category title in single posts’ is closed to new replies.