WordPress.org

Forums

next_posts_link greyed out if null (6 posts)

  1. nicnatros
    Member
    Posted 8 years ago #

    I want to have my next_posts_link and previous_posts_link show greyed-out text instead of a link when someone views the first or last page and there are no more (when the value is assumably null).

    I know nothing about PHP or non-html wordpress code beyond extreme basics(if is_page - else - endif).

  2. nicnatros
    Member
    Posted 8 years ago #

    This is the section in link-template.php but I just don't know how to accomplish this. I appreciate any help anyone has to offer.

    function next_posts_link($label='Next Page »', $max_page=0) {
    	global $paged, $wpdb, $wp_query;
    	if ( !$max_page ) {
    		$max_page = $wp_query->max_num_pages;
    	}
    	if ( !$paged )
    		$paged = 1;
    	$nextpage = intval($paged) + 1;
    	if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
    		echo '<a href="';
    		next_posts($max_page);
    		echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
    	}
    }
  3. nicnatros
    Member
    Posted 8 years ago #

    Does anyone know how to do this? I need this to complete my theme. I'm using a bar that has the previous and next links seperated on the left and right. It looks lopsided when people enter and there is only text for previous link.

  4. nicnatros
    Member
    Posted 8 years ago #

    Can anyone help me do this?

  5. nicnatros
    Member
    Posted 8 years ago #

    Is this not possible? I'm open to CSS formating tricks if any exist that would allow for blank text but overwritten with the link if one exists. I have no clue how to go about this but it may be possible.

  6. sammyb
    Member
    Posted 7 years ago #

    you can do this using the global parameter $paged and the query $wp_query->max_num_pages;
    it's a bit of a long winded hack and i'm sure there's a simpler way, but this is the way i found when faced with a similar problem:

    //set the divider that separates the links
    $divider = ' | ';
    
    //get the current page number
    $page_num = $paged;
    if($page_num == ''){$page_num = '1';}
    
    //get the number of the last page
    $max_num_pages = $wp_query->max_num_pages;
    
    //if its the first page
    if($page_num>1){
    posts_nav_link('','', __('previous posts'));
    echo $divider;
    echo'<span class="greyed_out">next posts</span>';
    }
    
    //if its in the middle
    if($page_num>1 && $page_num<$max_num_pages){
    posts_nav_link('','', __('previous posts'));
    echo $divider;
    posts_nav_link('', __('next posts'),'');
    }
    
    //if its the last page
    if($page_num == $max_num_pages){
    echo'<span class="greyed_out">previous posts</span>';
    echo $divider;
    posts_nav_link('', __('next posts'),'');
    }

Topic Closed

This topic has been closed to new replies.

About this Topic