While creating a new clone theme, I need to get the URL of the first link found in the post content. Fortunately, there is a read-to-use snippet in the default Twenty Eleven theme. If you are using Twenty Eleven, then you are all set up, otherwise add following to your functions.php
.
/** * Return the URL for the first link found in the post content. * * http://clonetemplates.com/?p=536 * * Return string|bool URL or false when no link is present. */ function ct_url_grabber() { if ( ! preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/is', get_the_content(), $matches ) ) return false; return esc_url_raw( $matches[1] ); }
Usage:
<?php echo ct_url_grabber(); ?>
Be noted that this function has to be used in the loop.
[…] 原文来自:Clone Templates […]