Add an anchor to a generated link
-
I’ve used this code to craft my navigation between posts in a category:
<div id="nav-above" class="navigation"> <div class="nav-previous"><?php previous_post_link('« %link', '%title', TRUE); ?></div> <div class="nav-next"><?php next_post_link('%link »', '%title', TRUE); ?></div> </div><!-- #nav-above -->
I need to append the
#content
anchor to the links it fetches. How can I do this? Thanks.– E.D.
-
OK – I will do a little testing and get back to you. Please show me a ‘before’ and ‘after’ example or two so I can be sure we are on the same page about what you want.
I am just not sure how an anchor goes into a pretty permalink.
<?php previous_post_link('« %link', '%title', TRUE); ?>
outputs (in this case): http://www.looseleafstories.com/2010/09/chapter-26-page-1/I need it to output: http://www.looseleafstories.com/2010/09/chapter-26-page-1/#LLScontent
I’m not sure what before/after you want, to I apologize if this isn’t even close to what you’re asking. This example is pulled from this page.
Sorry, that’s only the link output. The full output is « Chapter 26: Prayers Unanswered, page 1. All I need it to do is add
#LLScontent
to the link.OK. That’s what I was looking for. Here is some code to try. Note that it is not exhaustively tested. I count on you to test it. Place this in functions.php:
<?php function mam_add_anchor ($anchor,$link) { // Adds the anchor $anchor to $link. // First, make sure $anchor has a leading # if (!preg_match('/^#/',$anchor)) $anchor = '#' . $anchor; if (strpos($link,'href')) { $hrefpat = '/(href *= *([\"\']?)([^\"\' ]+)\2)/'; } else { $hrefpat = '/(([\"\']?)(http([^\"\' ]+))\2)/'; } if (preg_match($hrefpat,$link,$matches)) { $url = $matches[3]; // If there are other parameters, just add to end. // Otherwise, make sure there is a trailing slash on the url. if (strpos($url,'?')) { $newurl = $url; } else { $newurl = trailingslashit($url); } $newurl = $newurl . $anchor; // echo '<p>OLDURL:' . htmlspecialchars($url) . '</p>'; // echo '<p>NEWURL:' . htmlspecialchars($newurl) . '</p>'; $link = str_replace($url,$newurl,$link); } return $link; } function mam_post_link_add_anchor ($link) { global $mam_add_anchor; if ($mam_add_anchor) $link = mam_add_anchor($mam_add_anchor,$link); return $link; } add_filter('previous_post_link','mam_post_link_add_anchor'); add_filter('next_post_link','mam_post_link_add_anchor');
And, add this line before your nav code:
$mam_add_anchor = 'LLScontent';
Do I need to add it to a specific section of function.php or otherwise divide from the existing content? I’ve added it to the end of the document and it crashes the theme every time.
I guess sleepiness is all I needed. I found a simple, one-line fix for my entire issue. I just put an autojump to the content in single.php and it does exactly what I need. No need for filters… Sorry, looks like I wasted your time. >.<
No problem. Glad you got it working.
- The topic ‘Add an anchor to a generated link’ is closed to new replies.