The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

How to make use of the post format "Link"? (8 posts)

  1. calvinhsu
    Posted 5 years ago #

    WP3.1 has introduced post format.

    Below is described on the codex page of "Post Format"

    link - A link to another site. Themes may wish to use the first tag in the post content as the external link for that post. An alternative approach could be if the post consists only of a URL, then that will be the URL and the title (post_title) will be the name attached to the anchor for it.

    This sounds great, because I can share stuff I'm reading on mobile devices onto my blog together with the post-by-email function, just sending title as email subject and a link as email content.

    But it seems there is no easy way to achieve this at the moment.

    So does anyone has some idea how to assign the external link in post content to the post title, thus making a Link format post.

  2. esmi
    Forum Moderator
    Posted 5 years ago #


    // Extract first occurance of text from a string
    if( !function_exists ('extract_from_string') ) :
    function extract_from_string($start, $end, $tring) {
    	$tring = stristr($tring, $start);
    	$trimmed = stristr($tring, $end);
    	return substr($tring, strlen($start), -strlen($trimmed));


    // Get the text & url from the first link in the content
    $content = get_the_content();
    $link_string = 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>
  3. calvinhsu
    Posted 5 years ago #

    @esmi Thanks for the code!

    But I'm not quite clear how to use the format-link.php

    I created a file named format-link.php in my theme directory, but nothing happens.

    I tried paste the code into loop, error occurs.

    What should I do?

  4. esmi
    Forum Moderator
    Posted 5 years ago #

  5. Chip Bennett
    Theme Review Admin
    Posted 5 years ago #

    What Theme are you using?

    Your Theme needs to enable support for Post Formats, including explicitly declaring the Post Format types it will support.

    Then, the Theme needs to include custom layout/style for each given Post Format type.

  6. calvinhsu
    Posted 5 years ago #

    @Chip Bennett
    It's a child theme of twentyten.

    I have add link to add_theme_support()

    Then I tried adding
    `if ( has_post_format( 'link' )) {
    echo 'this is the link format';
    into loop and it worked.

  7. calvinhsu
    Posted 5 years ago #

    Since I'm not able to figure out how to use the code @esmi has provided, I tried to study the php function in his code,stristr(),substr(),strlen()(I know really very little about coding), and finally I came up with some code, they work fine with the purpose what I have originally described--extract the first link from content, or we just put a plain url in content, and the output become the post title linking to the address.

    Just put the code in loop, and nothing has to be add into functions.php

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Since I'm not a php guy, the code is much likely to fail in some case. But as I haven't yet found an easy way to achieve this on the internet, I just paste them here and hope some veteran would give further advice.

  8. calvinhsu
    Posted 5 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic