WordPress.org

Ready to get started?Download WordPress

Forums

How to Add Title Tag to Twenty Eleven Single Post "Previous/Next" Navigation? (14 posts)

  1. Tranny
    Member
    Posted 2 years ago #

    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?

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  3. Tranny
    Member
    Posted 2 years ago #

    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.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    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">[...]
  5. Tranny
    Member
    Posted 2 years ago #

    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.

  6. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  7. Tranny
    Member
    Posted 2 years ago #

    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 :(

  8. esmi
    Forum Moderator
    Posted 2 years ago #

  9. Tranny
    Member
    Posted 2 years ago #

    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

  10. esmi
    Forum Moderator
    Posted 2 years ago #

    What problem did you have with that function?

  11. Tranny
    Member
    Posted 2 years ago #

    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); ?>">

  12. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  13. Tranny
    Member
    Posted 2 years ago #

    Where exactly do I add this code?

  14. esmi
    Forum Moderator
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic