• Can I use the “Link” post format to create an external link from my home page — without using custom fields or plugins?

    I’d like such external links to show up as regular posts (though differently styled of course) with title, thumb, excerpt and post meta, but I haven’t been able to find a tutorial or post anywhere that explains how to do this. Is it even possible?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Assuming you mean the Link Post Format, then yes – you can output the post in any way that you like. Have a look at Twenty Eleven theme for a way of handling this – specifically the content-link.php and index.php files.

    Thread Starter Nick Ottens

    (@ottens)

    Thanks esmi.

    I see the relevant code in functions.php is this:

    function twentyeleven_url_grabber() {
    	if ( ! preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/is', get_the_content(), $matches ) )
    		return false;
    
    	return esc_url_raw( $matches[1] );
    }

    Is it possible to pull this off without creating a separate content-link.php though?

    In theory, you could use a conditional based on get_post_format() to code up different loop displays, yes.

    Thread Starter Nick Ottens

    (@ottens)

    Yes, but what do I want to display? Both the content.php and content-link.php link to the permalink in the title.

    what do I want to display?

    Err… that’s up to you. If you do not want the post’s title to be a permalink to the single post view, don’t make it a link.

    Thread Starter Nick Ottens

    (@ottens)

    The post title should point to the external link, so I suppose my question is: how do I reference that external link which I’ve just “grabbed”?

    There might be prettier ways of doing this but I’ve used the following in one of my themes:

    <?php
    // Get the text & url from the first link in the content
    $content = get_the_content();
    $link_string = my_extract_from_string('<a href=', '/a>', $content);
    $link_bits = explode('"', $link_string);
    foreach( $link_bits as $bit ) {
    	if( substr($bit, 0, 1) == '>') $link_text = substr($bit, 1, strlen($bit)-2);
    	if( substr($bit, 0, 4) == 'http') $link_url = $bit;
    }?>
    <h2 class="post-title"><a href="<?php echo $link_url;?>" title="<?php _e('External link';?>"><?php echo $link_text;?></a></h2>

    And the function:

    // Extract first occurance of text from a string
    function my_extract_from_string($start, $end, $tring) {
    	$tring = stristr($tring, $start);
    	$trimmed = stristr($tring, $end);
    	return substr($tring, strlen($start), -strlen($trimmed));
    }
    Thread Starter Nick Ottens

    (@ottens)

    Ok, added that to function and tried this is in content.php:

    <?php if ( has_post_format( 'link' )) {
    $content = get_the_content();
    $link_string = my_extract_from_string('<a href=', '/a>', $content);
    $link_bits = explode('"', $link_string);
    foreach( $link_bits as $bit ) {
    	if( substr($bit, 0, 1) == '>') $link_text = substr($bit, 1, strlen($bit)-2);
    	if( substr($bit, 0, 4) == 'http') $link_url = $bit;
    }?>
    <h1 class="entry-title"><a href="<?php echo $link_url;?>" title="<?php _e('External link';?>"><?php echo $link_text;?></a></h1><?php }
    else {
    ?>
    
    		<h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'toolbox' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    <?php } ?>

    But it doesn’t work. I must have done something wrong there with the code because the page won’t even load….

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Grab first link from post for external link’ is closed to new replies.