获取WordPress文章的第一个链接
WordPress文章格式中有一种是链接,用来快速分享一个链接,如果我们可以将文章的这个链接自动链接到这篇文章的标题中,远比在文章内容中直接显示这个链接来的完美。将下面的代码添加到当前主题的 functions.php 中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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; } |
然后你可以类似下面的代码来为标题添加这个链接:
1 2 3 | <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/
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
1 条评论
发表评论
要发表评论,您必须先登录。
文章中的functions.php代码是错误的,无法加入到functions.php中!希望能够检查下,谢谢!