WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Modifying "next_post_link" (6 posts)

  1. nickaster
    Member
    Posted 3 years ago #

    Hi there. I want to add some tracking to my "next_post_link" and my "next_previous_link" so that I can see whether people are actually using the links.

    I'm thinking the easiest way to do it would be to add a "?next" or "?previous" to the URLs so that they'd show up in Google Analytics.

    But.. looks like the code to make the URLs is waaaaaay buried in "link-template.php" and i don't really want to mess with it. Can anyone help with a simple function that could make this happen?

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    you could possibly add filters; in functions.php of the theme:

    example:

    add_filter('next_post_link', 'mod_next_post_link');
    function mod_next_post_link($link) {
    $link = str_replace('" rel="next">', '?next" rel="next">', $link);
    return $link;
    }
    add_filter('previous_post_link', 'mod_previous_post_link');
    function mod_previous_post_link($link) {
    $link = str_replace('" rel="prev">', '?previous" rel="prev">', $link);
    return $link;
    }
  3. nickaster
    Member
    Posted 3 years ago #

    Solid, thanks!

    Where in that code would I add my text? ie, if I wanted it to say "?APPLES" after the "next" URL, where does it go in there?

  4. alchymyth
    Forum Moderator
    Posted 3 years ago #

    in this line:

    $link = str_replace('" rel="next">', '?next" rel="next">', $link);

    you could replace ?next with ?APPLES

  5. nickaster
    Member
    Posted 3 years ago #

    Ah! got it, sorry, should have been obvious, thanks.

  6. Daniel Haim
    Member
    Posted 2 years ago #

    Thanks Nickaster.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.