• My previous post link, the same as for next, is:
    <?php previous_post_link('%link') ?>

    How would I change this to an a href? so that I may wrap the link around a div?

    I’m not 100% sure how to work around php just yet.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Hello!

    As you can check it in the WordPress Codex page, it generates an <a href> when you open the page. If you would like to put this inside a div, just create a div around the php code like this:

    <div id="someid"><?php previous_post_link('%link') ?></div>

    I hope I have answered your question. If I didn’t, or I can help you in any more questions, just post it here and I am happy to assist.

    Cheers,
    Toth Balint BT

    Thread Starter thelackof

    (@thelackof)

    Hi, I understand that which is something that I am doing now.

    I need to convert the link to an A href though so that I can put the link around my div, not my div around the link.

    Any way to do this?

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    So if I got this right you want to wrap the href around the div? like:

    <a href=""><div>Something</div></a>

    If that is the case, one way I can think of would be to use a filter or alternatively try:

    previous_post_link( '%link', '<div>%title</div>' );

    Keep in mind that is untested. ??

    Thread Starter thelackof

    (@thelackof)

    Thank you Jose, I think your reply is on the right track, but I seem to be getting a white screen when I try to implement it within my containing php (probably should have included)

    This is what I have:

    <?php $prev_post = get_previous_post(); if($prev_post) { ?>
    <div class='prev'>
    <p><?php previous_post_link('%link') ?></p>
    </div>
    <?php }?>
    
    <?php $next_post = get_next_post(); if($next_post) { ?>
    <div class='next'>
    <p><?php  next_post_link('%link') ?></p>
    </div>
    <?php }?>

    How would I work your example within
    <?php $prev_post = get_previous_post(); if($prev_post) { ?><?php }?>

    I tried:

    <?php $prev_post = get_previous_post(); if($prev_post) { previous_post_link( '%link', '<div>%title</div>' ); }?>

    But I get a complete white screen. I tried another way and still white screen.

    Any clue?

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    try:

    if( ! empty( $prev_post) ) { //code to run };

    Complete blank? Have you looked in your logs to see what could be causing it?

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Were you able to solve your issue?

    Thread Starter thelackof

    (@thelackof)

    Sorry for the late reply, I could not.

    Does the following look correct?

    <?php $prev_post = get_previous_post(); if($prev_post) { previous_post_link( '%link', '<div>%title</div>' ); }?>

    Should that work or am I writing it wrong?

    That looks right. This is the HTML output from that PHP code.

    <a href="blah.html" rel="prev">
         <div>Blah</div>
    </a>

    Is that what you wanted?

    Thread Starter thelackof

    (@thelackof)

    Yeah I had wanted the link to contain the div but when I use the code included above, my entire website just goes white. No error, nothing.

    Not exactly sure what logs to check as again I am not 100% knowledgeable when it comes to PHP.

    I also tried
    if( ! empty( $prev_post) ) { //code to run };

    but that doesn’t seem to help either.
    Really lost here.

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Try:

    // Get the links
    $next = get_next_post_link( '%link', '<div class="link">%title</div>' );
    $prev = get_previous_post_link( '%link', '<div class="link">%title</div>' );
    // Create the HTML block
    $html = '<nav class="post-nav">';
    // If there is a previous post add it
    if ( ! empty( $prev ) ){
    	$html .= $prev;
    }
    // If there is a next post add it
    if ( ! empty( $next ) ){
    	$html .= $next;
    }
    // Close the nav element
    $html .= '</nav>';
    // Echo/printf() either one will work
    echo $html;

    Thread Starter thelackof

    (@thelackof)

    Really no idea what is causing it but even the above is causing my site to go white.

    The website that I am working on is https://www.thelackof.com.
    The next and prev are only on my single pages (posts) and I currently have no CDN installed so all cache should be cleared.

    The page opens my header and then following is my next and previous links.

    <div class='prev-next'>
    <div class='contain'>
    
    <?php $prev_post = get_previous_post(); if($prev_post) { ?>
    <div class='prev'>
    <p><?php previous_post_link('%link') ?></p>
    </div>
    <?php }?>
    
    <?php $next_post = get_next_post(); if($next_post) { ?>
    <div class='next'>
    <p><?php  next_post_link('%link') ?></p>
    </div>
    <?php }?>
    
    </div><!-- contain -->
    </div><!-- prev-next -->
    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    The site works fine for me. I see the navigation on the top just fine. I do, however, seem to get a 404 on the pagenavi.css file if that helps you out any.

    Thread Starter thelackof

    (@thelackof)

    Hey yeah your code is not on the site right now since the site goes white when I include it. The navigation works but I cannot get it to wrap around the entire box, so for now it’s only a text link.

    <?php previous_post_link('%link') ?>

    That’s why I need to convert the php link into a workable a href link to wrap around my div classes so that the entire box can be a clickable link, therefore I could style the div when hover and also so that I could have the arrows that I’ve created clickable.

    When I try the provided code in this thread, my entire page just goes white. No errors, nothing, the altered php just wipes my site.

    I’ve been meaning to remove the pagenavi.css though, it currently has nothing to do with my site and is just sitting in my functions. Thanks for reminding me!

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Why not make the a element display as a block and use padding?

    Something like:

    .prev a {
      display: block;
      padding: 1.5em 0;
      color: #6d6d6d;
    }

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Change PHP link to A href Link?’ is closed to new replies.