WordPress文章格式中有一种是链接,用来快速分享一个链接,如果我们可以将文章的这个链接自动链接到这篇文章的标题中,远比在文章内容中直接显示这个链接来的完美。将下面的代码添加到当前主题的 functions.php 中:
function get_content_link( $content = false, $echo = false )
{
if ( $content === false )
$content = get_the_content();
$content = preg_match_all( '/hrefs*=s*["']([^"']+)/', $content, $links );
$content = $links[1][0];
if ( empty($content) ) {
$content = false;
}
return $content;
} |
function get_content_link( $content = false, $echo = false )
{
if ( $content === false )
$content = get_the_content();
$content = preg_match_all( '/hrefs*=s*["']([^"']+)/', $content, $links );
$content = $links[1][0];
if ( empty($content) ) {
$content = false;
}
return $content;
}
然后你可以类似下面的代码来为标题添加这个链接:
<h2><a href="<?php echo get_content_link( get_the_content() ); ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">Comment »</a> |
<h2><a href="<?php echo get_content_link( get_the_content() ); ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">Comment »</a>
参考资料:http://wp-snippets.com/get-the-first-link-in-post/
来源:
https://www.wpdaxue.com/get-the-first-link-in-post.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32786.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57