WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have my news in posts and I am trying to display the two latest news articles (posts) on my main page. I've got that solved by getting the title using the_title() and getting a little bit of the text using the_excerpt().

I want to show a more... link that takes you to the post. However, some of the news articles that I am posting have some text in the post body that's a link, how do I only get the link within the body of a post so that my more... link takes your there directly?

For example, sometimes we will link to news articles in PDFs that are on another server.

I'm using the following code.

<?php query_posts('category_name=news2011'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php /* the_ID(); */ ?> 
    <?php /* the_date('F Y'); */ ?>
    <ol class="news">
        <strong><p><?php the_title()?></p></strong>
        <li><?php the_content(); ?></li>
<?php endwhile; endif; ?>
share|improve this question
up vote 1 down vote accepted

You can scan the content to see whether it contains a link and then parse it to find the href attribute. There are many ways to do this, this example uses the built-in kses functionality, as demonstrated by Otto:

$post_link = get_the_permalink();
if ( preg_match('/<a (.+?)>/', get_the_content(), $match) ) {
    $link = array();
    foreach ( wp_kses_hair($match[1], array('http')) as $attr) {
        $link[$attr['name']] = $attr['value'];
    $post_link = $link['href'];
share|improve this answer
Thanks I'll give that a try. – Beto Apr 6 '11 at 23:26
Worked like a charm, sorry for the really late reply. – Beto Jun 28 '11 at 17:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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