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 was exploring and ran across this post: Grab First Link From A Post, Grab First Blockquote

I would basically like to do the same thing, but have it call not only the URL but also the link, since I am using the link as an excerpt title.

For example; if my link is <a href="http://www.facebook.com/>Facebook</a> I want it to call a link that says Facebook not one that says http://www.facebook.com like the current code does.

This is probably easy when given the code provided already, but I can't figure it out.

share|improve this question
possible duplicate of how to grab first link in post... and of course call it – Chip Bennett Jul 10 '11 at 17:57

maybe try with php DOMDocument rather than regex. I don't think all DOMDocument methods will work with just an html fragment and not a complete document, but for your needs it may be enough:

$dom= new DOMDocument();
$html = '<p>Some html with a <a href="http://foo.com/">link</a>. Some more text and <a href="http://bar.com/">another</a> link.</p>';
$dom->loadHTML( $html );
$dom->preserveWhiteSpace = false;
$elements = $dom->getElementsByTagName( 'a' );
foreach ( $elements as $element ){
    echo "link text: " . $element->nodeValue . "<br>";
    echo "href value: " . $element->getAttribute( 'href' ) . "<br><br>";

// outputs:
// link text: link
// href value: http://foo.com/
// link text: another
// href value: http://bar.com/
share|improve this answer
I don't get this, how would this pull from the post? I don't want to manually input links. – Jeremy Jul 11 '11 at 21:16
I just put some dummy content in a variable as an example, $html would be whatever content you're passing to it. – Milo Jul 11 '11 at 22:47

I'm actually trying to do this too.

The following code calls up the link, but not like you're asking (with the actual text for the link included). If you figure this out please post it and let us know, I could really use it!

function get_content_link( $content = false, $echo = false )
    // allows using this function also for excerpts
    if ( $content === false )
        $content = get_the_content(); // You could also use $GLOBALS['post']->post_content;

    $content = preg_match_all( '/href\s*=\s*[\"\']([^\"\']+)/', $content, $links );
    $content = $links[1][0];
    $content = make_clickable( $content );

    // if you set the 2nd arg to true, you'll echo the output, else just return for later usage
    if ( $echo === true )
        echo $content;

    return $content;
share|improve this answer
Ok, I figured this sucker out. Use the code I provided above, put php the_title inside a link then make the src= point to the link created by the code above. – Lucas Jul 12 '11 at 17:17

You could build a function that does something like this.

$partnersite = '<a href="http://www.facebook.com/">Facebook</a>';
$partnersite = explode('>', $partnersite);
$partnersite = explode('<', $partnersite[1]);
echo $partnersite[0];
share|improve this answer

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.