/* 336x280*/
有时候,你可能想要发布一篇标题外链的文章。WordPress 没有这样的内建功能,那我们就自己动手丰衣足食吧。
第一件要做的事情是打开当前主题的 functions.php 文件,并且粘贴以下代码:
function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);
if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';
}
完成之后,打开 index.php 文件,找到类似下面的输出标题的函数语句:
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="点击查看 <?php the_title(); ?>"><?php the_title(); ?></a></h2>
将 the_title() 函数替换为新的标题输出函数:
<?php print_post_title() ?>
现在,如果你想要使 WordPress 文章标题链向外部资源,只要为该文章设定名称为 url1、title_url 或 url_title 的自定义字段,并将字段值写成外部链接地址。
/* 336x280*/
版权所有,转载请注明出处。
转载自 <a href="http://www.mangguo.org/link-to-external-in-post-title/" title="使 WordPress 文章标题链向外部资源" rel="bookmark">使 WordPress 文章标题链向外部资源 | 芒果小站</a>
我简单说几句