WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Twenty Eleven Adding category title in single posts (24 posts)

  1. insurgenesis
    Member
    Posted 1 year ago #

    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 :
    http://pastebin.com/4uYYc7wA

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

  2. tiaanswart
    Member
    Posted 1 year ago #

    When trying to show only the first categories name use the below:

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

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

    <?php
    $category = get_the_category($post->ID);
    if($category[0]){
    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...

  3. insurgenesis
    Member
    Posted 1 year ago #

    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.

  4. tiaanswart
    Member
    Posted 1 year ago #

    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(); ?>
        </div>
    
        <?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>
        </div>
    
        <?php endif; ?>
    
        <?php endif; ?>
  5. insurgenesis
    Member
    Posted 1 year ago #

    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:
    http://wordpress.org/support/topic/twenty-eleven-set-all-posts-to-use-one-navigation-template?replies=19#post-3126786

  6. tiaanswart
    Member
    Posted 1 year ago #

    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...

  7. tiaanswart
    Member
    Posted 1 year ago #

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

  8. insurgenesis
    Member
    Posted 1 year ago #

    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>' );
    ?></h1>
    
    <?php
    $category_description = category_description();
    if ( ! empty( $category_description ) )
    echo apply_filters( 'category_archive_meta', '<div class="category-archive-meta">' . $category_description . '</div>' );
    ?>
    </header>

    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...

  9. tiaanswart
    Member
    Posted 1 year ago #

    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">
    	            <tbody>
                        <tr>
    	                    <td valign="top" width="200">
    	                        Skills:
    	                    </td>
                            <td valign="top" width="400">
                                <?php the_terms( $post->ID, 'Skills', '', ', ', ' ' ); ?>
                            </td>
                        </tr>
                    </tbody>
    	            </table>
                </div>
            <?php endif; ?>
  10. insurgenesis
    Member
    Posted 1 year ago #

    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.

  11. tiaanswart
    Member
    Posted 1 year ago #

    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 :http://wordpress.org/support/topic/twenty-eleven-set-all-posts-to-use-one-navigation-template?replies=19#post-3126786

    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>' );
    ?></h1>
    
    <?php
    $category_description = category_description();
    if ( ! empty( $category_description ) )
    echo apply_filters( 'category_archive_meta', '<div class="category-archive-meta">' . $category_description . '</div>' );
    ?>
    </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
  12. insurgenesis
    Member
    Posted 1 year ago #

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

  13. tiaanswart
    Member
    Posted 1 year ago #

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

  14. insurgenesis
    Member
    Posted 1 year ago #

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

  15. insurgenesis
    Member
    Posted 1 year ago #

    If you haven't become impatient, it's here:
    http://pastebin.com/jXrF471J

  16. tiaanswart
    Member
    Posted 1 year ago #

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

  17. tiaanswart
    Member
    Posted 1 year ago #

    i'm busy trying to solve the issue from my last post...

    you've got me intrigued here so if i get impatient it would only be a good thing as it would confirm your suspicion that single.php is really not playing nice...

    or i'm just not clever enough to figure it out

  18. insurgenesis
    Member
    Posted 1 year ago #

    no hahaha i'm truly lost

    You think you're lost? No, we've almost reached the 20-post threshold - stoksielalleen op 'n Saterdag aand.

    Yes that result is to be expected but when you tested it, as you said you did, did the category title/heading show?

    As I said, I can see mine in firebug only partly there...

    BTW do you earn a living with WP?

  19. tiaanswart
    Member
    Posted 1 year ago #

    I finaly got the category to show in single.php but the below code needs to be pasted below the loop start:

    <header class="page-header">
            <h1 class="page-title"><?php the_terms( $post->ID, 'Skills', 'Category Archives: <span>', '</span>, <span>', '</span> ' ); ?></h1>
        </header>

    remember to change skills to your own custom taxonomy

  20. insurgenesis
    Member
    Posted 1 year ago #

    Where precisely should I paste it in the code I put in the pastebin link? I'm it goes in single.php?
    I don't know what to replace "Skills" with because I don't use custom taxonomies. Can I just leave it blank like '' ?

  21. tiaanswart
    Member
    Posted 1 year ago #

    Nope see below code tested works fine now with normal categories:

    <header class="page-header">
            <h1 class="page-title">Category Archive: <?php
                $seperator  = ' &bull; ';
                $parents    = '';
                $post_id    = $post->ID;
    
                the_category( $seperator , $parents, $post_id );
                ?>
            </h1>
        </header>

    Paste this code just below the following line in single.php:

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

  22. insurgenesis
    Member
    Posted 1 year ago #

    Hi tiaanswart, thanks for looking into it.
    I can confirm that categories now show up above single posts.
    However, they looked completely wrong and my custom css had to be mangled to work it so that I could style with these selectors:
    .page-title {} .page-title a {}.

    It's good to know forums can intrigue us isn't it.
    Thanks again.

  23. insurgenesis
    Member
    Posted 1 year ago #

    Do you have the means to receive a PM?

  24. esmi
    Forum Moderator
    Posted 1 year ago #

    These forums does not provide support via personal messaging.

Topic Closed

This topic has been closed to new replies.

About this Topic