WordPress.org

Ready to get started?Download WordPress

Forums

Next/Previous Post URL only (7 posts)

  1. logan_x
    Member
    Posted 4 years ago #

    Hi guys,

    I'm looking on the documentation and googling it, but I just can't find anything, which is weird, 'cause it seems to be a simple thing.

    You know, the functions: next_post_link and previous_post_link.

    But I want them to show ONLY the URL, without any text. Like, I wish to make a tag like this:

    <a href="<?php next_post_link(URL) ?>?variable=value"> Here some text </a>

    Could you help me, please?

  2. logan_x
    Member
    Posted 4 years ago #

    Anyone, please?

  3. Shane G.
    Member
    Posted 4 years ago #

  4. logan_x
    Member
    Posted 4 years ago #

    Those are for pages.

    I tried to make the function code for posts, but it still doesn't work.

    Here goes the code I made:

    $postlist = get_posts();
    $posts = array();
    foreach ($postlist as $post) {
    	$posts[] += $post->ID;
    }
    
    $current = array_search($post->ID, $posts);
    $prevID = $posts[$current-1];
    $nextID = $posts[$current+1];
    
    // Link for previous post
    if (!empty($prevID)) {
    	echo '<div><a href="'. get_permalink($prevID) .'?variable=true">Msg. Anterior</a></div>';
    }
    
    // Link for next post
    if (!empty($nextID)) {
    	echo '<div><a href="'. get_permalink($nextID) .'?variable=true">Pr&oacute;xima Msg.</a></div>';
    }
  5. pmoranc
    Member
    Posted 4 years ago #

    Im looking for the same thing!! Its rare.. I hope someone can solved this.

  6. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    there is an undocumented function 'get_next_post()':

    <?php $nepo=get_next_post(); $nepoid=$nepo->ID;
    $next_post_url = get_permalink($nepoid); ?>
    <?php $prpo=get_previous_post(); $prpoid=$prpo->ID;
    $prev_post_url = get_permalink($prpoid); ?>

    if there is no next/prev - the above will contain the link url of the last post at the respective end.

    in these cases, it could be useful to define a fixed url:

    <?php $nepo=get_next_post(); if($nepo) { $nepoid=$nepo->ID;
    $next_post_url = get_permalink($nepoid); } else { $next_post_url = '#'; ?>
  7. Pagelift
    Member
    Posted 3 years ago #

    Here you go:

    Previous Post:
    <?php echo get_permalink(get_adjacent_post(false,'',false)); ?>

    Next Post:
    <?php echo get_permalink(get_adjacent_post(false,'',true)); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic