WordPress 使用自定义fields 实例三: 添加原创和转载信息

首先参见基础文章: http://www.iamhippo.com/2015-12/260.html

此类信息其实也是可以通过插件来实现的,但是用自定义fields 能实现更好的自由度和运行速度.

实现过程:

1 添加’copyright’ 自定义meta field

2  找到模板的single-content 的需要插入的地方,插入如下代码:

<?php $custom_fields = get_post_custom_keys($post_id);
if (!in_array ('copyright', $custom_fields)) : ?>
<div class="postcopyright">
<p><strong> 声明: </strong> 本文由(<a href="<?php bloginfo('home'); ?>"> <?php the_author(); ?> </a>)原创,转载请保留本文链接: <a href="<?php the_permalink()?>" title=<?php the_title(); ?>><?php the_title(); ?></a></p>
</div>
<?php else: ?>
<?php $custom = get_post_custom($post_id);
$custom_value = $custom['copyright']; ?>
<div class="postcopyright">
<p><strong> 声明: </strong> 本文来源于 <a rel="nofollow" target="_blank" href="/go.php?url=<?php echo $custom_value[0] ?>"><?php echo $custom_value[0] ?></a> ,由(<a href="<?php bloginfo('home'); ?>"> <?php the_author(); ?> </a>) 整编。</p>
<p><strong> 本文链接: </strong><a href="<?php the_permalink()?>" title=<?php the_title(); ?>><?php the_title(); ?></a> .</p>
</div>
<?php endif; ?>

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.