0

Good Day

I am using excerpts in my wordpress theme. Now in wordpress's documentation, they say wordpress does not automatically add links for the excerpts, you have to manually add them like this:

Original excerpts file

 <div class="content">
 <?php the_excerpt(); ?>
 </div>

Adding links

<div class="content">
<?php the_excerpt(); ?>
<a href="<?php echo get_permalink(); ?>"> Read More...</a>
</div>

Now what I would like to do is to link each excerpt to a different wordpress page, and not to its own excerpt. So for that, I would have to create a custom php function to create a unique link for each excerpt, pointing to a different page each. How do I do that?

See my website where the excerpts are located - It is right below the main slider named: Pastor's Corner, Mother's Heart, Sermons etc...

Thank you

1
  • You know how to create URLs so just point the link to wherever you want. There isn't enough detail in your question for anyone to write the code for you. "a different page" is pretty generic.
    – s_ha_dum
    Commented Feb 23, 2013 at 23:47

2 Answers 2

1

For something like that, you need to use custom post metadata (aka "Custom Fields").

There are two steps:

  1. Define a new custom field for the post (this will be where you enter your URL)
  2. Output that custom field in your template.

STEP 1 First, you will need to define a new custom field to store your links. Start by editing any post you want to do this with. On the edit screen, find the "Custom Fields" box. If it's not visible, open the "Screen Options" tab (at the top-right of the window), and make sure the "Custom Fields" option is checked.

Now, define a new field. Click on "Enter New" and type a name in the left box... for example, you might call this "Excerpt URL". In the next field, type the URL you want to link to.

Sidenote: From now on, you will be able to select "Excerpt URL" from the Custom Fields drop-down any time you want to use it with a post. No need to type it every time.

STEP 2 Now, you just have to output that post meta from your template file. You want to use WordPress's built-in get_post_meta() function. Use it within the loop like so...

<?php 
   $url = get_post_meta(get_the_ID(),'Excerpt URL',true); 
   if ( ! empty($url) ) {
      echo "<a href='{$url}'>Read more...</a>";
   }
?>
2
  • Thank you Matt. This sounds like it can work, but there is one problem. The excerpt posts do not have any custom field values - So I cannot link the excerpt to a page...Or is it the other way around? Do I link the page, with a custom field, to an excerpt? But how would you be redirected to the page when clicking on the excerpts 'Read More' link? What I want to do: Excerpt>Read more link>Link to a page. Now this is easy to use normal html and just write out each excerpt linking to each page, but how do I do this dymanically? With one html definer/parent? Commented Feb 24, 2013 at 10:53
  • Featured Image, Excerpt and slug are the only screen options available for the exerpts... Commented Feb 24, 2013 at 10:54
0

The excerpt automatically redirects to the single post page when clicked. I think the trouble you are having is is that [...] is not clickable, and you want to make it clickable. To do this you need to replace [...] with a custom Read More... link.

You need to do something like this:

function new_excerpt_more( $more ) {
    return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">' . __('Read More', 'your-text-domain') . '</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );

If you need a custom excerpt that does not strip html tags, go and check my answer on a previous question here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.