WordPress: Add the_permalink to a link inside of a function

I am very new to PHP and really don’t know where to start when it comes to writing it myself.

I have found this function for WordPress which I am using:

Read More
function excerpt($limit) {
  $excerpt = explode(' ', get_the_excerpt(), $limit);
  if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt).'<a href="<?php the_permalink(); ?>">Read In Full</a>';
  } else {
    $excerpt = implode(" ",$excerpt);
  } 
  $excerpt = preg_replace('`[[^]]*]`','',$excerpt);
  return $excerpt;
}

function content($limit) {
  $content = explode(' ', get_the_content(), $limit);
  if (count($content)>=$limit) {
    array_pop($content);
    $content = implode(" ",$content).'[...]';
  } else {
    $content = implode(" ",$content);
  } 
  $content = preg_replace('/[.+]/','', $content);
  $content = apply_filters('the_content', $content); 
  $content = str_replace(']]>', ']]&gt;', $content);
  return $content;

}

In the above code, I have placed

"<?php the_permalink(); ?>"

into the href. It is just showing up as a string of words instead of creating a call to the posts link.

Anyone help me out??
Thanks!

Related posts

Leave a Reply

2 comments

  1. I was having this exact problem, and found that sbrajesh was right — we need to use get_permalink(). But it only works if you append the php properly — when I used <?php ?> it didn’t process the php at all (just spit out the html).

    Here is the working version I ended up with: echo implode(' ', $words)."<span class='more'><a href='" . get_permalink() . "'>read more</a></span>"; }

  2. Try it like this:

    $excerpt = implode(" ",$excerpt).'<a href="'<?php the_permalink(); ?>'">Read In Full</a>';
    

    You forgot to put ‘ before and after php. Hope this helps you.

    Later edit:

    I guess your whole code is write in php. Then I think you should try:

    $excerpt = implode(" ",$excerpt).'<a href="'.the_permalink().'">Read In Full</a>';
    

    Hope this time will work.