0

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

4

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.

  • 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
2

A more updated and clean method:

function get_link_url() {
    $content = get_the_content();
    $has_url = get_url_in_content( $content );

    return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
}

http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it

http://bavotasan.com/tutorials/retrieve-the-first-image-from-a-wordpress-post/

http://wordpress.org/extend/plugins/simple-image-grabber/

  • Somehow, I read "link" as "image". So, an upvote for Kaiser. :) – Chip Bennett May 4 '11 at 22:18
  • I've added code example. – Maxwell s.c Jan 9 '18 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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