Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have been trying to get the answer to this for ages now and wondered if anyone here could help. I would like to 'grab' the first link in a post and attach it to a button/image. I have tried the numerous options available here: http://wpquestions.com/question/show/id/873

I have had greater success with the last suggestion in the post of the link above, HOWEVER, it only works in Safari... I have tried with Chrome, Opera, IE and Firefox and none of them can even see the image let alone the link.

My website is here: http://www.officialiphoneapp.com

When you click on an app to see more, you should see an 'Available on iTunes' button above the post picture and a 'Download on iTunes' button below the post but unless you're using Safari you won't see either.

Any and all suggestions welcome. Marcus

share|improve this question

2 Answers 2

Two possibilities: Use the the_content-filter, or just call it in your template.

You can just throw the plugin in your folder, or add one (or both) function(s) in your themes functions.php file. The filter triggers only for the post format link, while the template tag can be used more universal - in the loop.

As a plugin

<?php
! defined( 'ABSPATH' ) AND exit;
/* Plugin Name: (#16365) »kaiser« Get 1st link inside the content */

// As an public API function
function wpse16365_get_content_link( $content = false, $echo = false )
{
if ( ! in_the_loop() )
    return;

    // allows using this function also for excerpts
    ! $content AND $content = get_the_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
    $echo AND print $content;

    return $content;
}

// As a the_content-filter Callback
function wpse16365_get_content_link_cb( $content )
{
    if ( 'link' !== get_post_format() )
        return $content;

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

    return make_clickable( $content );
}
add_filter( 'the_content', 'wpse16365_get_content_link_cb' );

This function should answer your Q. It's tested and works fine.

share|improve this answer
1  
This is amazing... i was going to let go of the keyboard for today but you got me intrested :) - thanks a lot @kaiser –  Sagive SEO Nov 5 '11 at 11:16

Your Answer

 
discard

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.