为wordpress增加网站公告功能
来源:本站原创|时间:2022-11-25|栏目:WordPress|
首先,在主题functions.php中增加下面的代码:
复制代码
代码如下:function wp_dashboard_GongGao() {
if($_POST && $_POST['update-GongGao'] == 1){
check_admin_referer('GongGao');
$GongGao = serialize($_POST['GongGao']);
update_option('GongGao',$GongGao) or add_option('GongGao',$GongGao);
}
?>
<form method="post" action="<?php echo add_query_arg ('time',time()); ?>">
<?php if($_POST && $_POST['update-GongGao'] == 1)echo '<p><span style="color:red;font-weight:bold;">更新公告成功</span></p>'; ?>
<?php $GongGao = unserialize(get_option('GongGao')); ?>
公告链接(留空则无链接):<input type="text" name="GongGao[link]" value="<?php if(isset($GongGao['link']))echo stripslashes($GongGao['link']); ?>" />
<p>公告信息
<textarea name="GongGao[content]" style="word-break:break-all;width:80%;" rows="4"><?php echo stripslashes($GongGao['content']); ?></textarea></p>
<input type="submit" class="button-primary" value="提交" />
<?php wp_nonce_field('GongGao'); ?>
<input type="hidden" name="update-GongGao" value="1" />
</form>
<?php
}
function my_wp_dashboard_setup() {
if(current_user_can('edit_themes'))wp_add_dashboard_widget('wp_dashboard_GongGao','网站公告','wp_dashboard_GongGao');
}
add_action('wp_dashboard_setup','my_wp_dashboard_setup');
添加这段代码之后,进入后台首页就能找到一个关键,让我们填写公告内容。
之后在前台如何调用呢?在需要之处使用下面的代码即可:
复制代码
代码如下:<?php $GongGao = unserialize(get_option('GongGao'));if(!empty($GongGao['content'])): ?>
<div id="site-gonggao"><?php echo ($GongGao['link'] ? '<a href="'.$GongGao['link'].'">' : '').$GongGao['content'].($GongGao['link'] ? '</a>' : ''); ?></div>
<?php endif; ?>
至于样式之类,可根据自身需求进行调整。