Support » Themes and Templates » How to Add Title Tag to Twenty Eleven Single Post "Previous/Next" Navigation?

  • Strangely enough, not even after the latest update does twenty eleven theme have the title tag in PREVIOUS and NEXT navigation buttons on single pages. I thought this was a fundamental requirement that every theme developer would strive to meet but somehow it keeps getting overlooked in twenty eleven. How can I manually add it to the theme?

Viewing 13 replies - 1 through 13 (of 13 total)
  • esmi

    (@esmi)

    Forum Moderator

    Do not edit the Twenty Eleven theme. It is the default WordPress theme and having access to an unedited version of the theme is vital when dealing with a range of site issues. Create a child theme for your changes.

    Oops, I already did some edits to it. Can I still revert it and do the child theme approach?

    And when we’re at it – do you know how to add the title tag to the navigation? In single.php the code looks like this:

    <span class="nav-previous"><?php previous_post_link( '%link', __( '<span class="meta-nav">←</span> Previous', 'twentyeleven' ) ); ?></span>
    <span class="nav-next"><?php next_post_link( '%link', __( 'Next <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?></span>

    I really don’t know how to add the title tag there without breaking the whole thing.

    esmi

    (@esmi)

    Forum Moderator

    Can I still revert it and do the child theme approach?

    Absolutely. If you’re not sure if you’ve reverted all of the edits, just re-upload a fresh copy of the Twenty eleven the to wp-content/themes using FTP.

    do you know how to add the title tag to the navigation?

    It can’t be added via the previous_post_link() or next_post_link() functions but you could just use something like:

    <span class="nav-previous" title="Previous post">[...]
    <span class="nav-next" title="Next post">[...]

    I forgot to mention – I’d like these tags to be dynamic, so they display the title of the post they link to. Would you know what to replace the text between the double quotes with to have the text added there dynamically based on what the post they link to is about?

    Thank you so much for your help, either way.

    esmi

    (@esmi)

    Forum Moderator

    Have you reviewed:
    http://codex.wordpress.org/Template_Tags/previous_post_link
    http://codex.wordpress.org/Template_Tags/next_post_link

    Perhaps there something in there that would suit your needs. Determining the next & previous post titles by code would probably mean at least 2 secondary loops.

    After checking both those pages, it would seem as though the twenty eleven developers thought of many things, except from title tags. I don’t think the way to achieve it lead through there. What you suggested before seems more viable, if there only was some way to echo the title dynamically. I wish I had more PHP experience to figure it out myself 🙁

    esmi

    (@esmi)

    Forum Moderator

    This sounds like it could be a solution, unfortunately this command doesn’t work. Hopefully the developers can fix it so it works in the next update:

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

    esmi

    (@esmi)

    Forum Moderator

    What problem did you have with that function?

    First I wanted to try whether calling a dynamic value between title double quotes would work, and it did:

    <span class="nav-previous" title="<?php the_title(); ?>">

    But this unsurprisingly calls the title of current post. SO I tried the get_adjacent_post tag, but it does absolutely nothing:

    <span class="nav-previous" title="<?php get_adjacent_post($previous); ?>">

    esmi

    (@esmi)

    Forum Moderator

    No – you need more code yet. something like:

    <?php // Get next post title
    $adjacent_post = get_adjacent_post(false,'',false);
    $next_post = get_post( $adjacent_post->ID );
    $next_post_title = $next_post->post_title;
    // Get previous post title
    $in_same_cat = false;
    $excluded_categories = '';
    $previous = true;
    $previous_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
    $previous_post_title = $previous_post->post_title;
    ?>

    Then you can use <?php echo $next_post_title;?> and <?php echo $previous_post_title;?> in the title attribute of your span tags.

    But do bear in mind that I’m writing this off the top of my head, so it’s not tested.

    Where exactly do I add this code?

    esmi

    (@esmi)

    Forum Moderator

    You could add it to your theme’s single.php file – just before these previous & next post code.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘How to Add Title Tag to Twenty Eleven Single Post "Previous/Next" Navigation?’ is closed to new replies.