为wordpress增加网站公告功能
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>首先,在主题functions.php中增加下面的代码:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
<p>
代码如下:</p>
</div>
<div id="phpcode10" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
function wp_dashboard_GongGao() {<br>
if($_POST && $_POST['update-GongGao'] == 1){<br>
check_admin_referer('GongGao');<br>
$GongGao = serialize($_POST['GongGao']);<br>
update_option('GongGao',$GongGao) or add_option('GongGao',$GongGao);<br>
}<br>
?><br>
<form method="post" action="<?php echo add_query_arg ('time',time()); ?>"><br>
<?php if($_POST && $_POST['update-GongGao'] == 1)echo '<p><span style="color:red;font-weight:bold;">更新公告成功</span></p>'; ?><br>
<?php $GongGao = unserialize(get_option('GongGao')); ?><br>
公告链接(留空则无链接):<input type="text" name="GongGao" value="<?php if(isset($GongGao['link']))echo stripslashes($GongGao['link']); ?>" /><br>
<p>公告信息 <br>
<textarea name="GongGao" style="word-break:break-all;width:80%;" rows="4"><?php echo stripslashes($GongGao['content']); ?></textarea></p><br>
<input type="submit" value="提交" /><br>
<?php wp_nonce_field('GongGao'); ?><br>
<input type="hidden" name="update-GongGao" value="1" /><br>
</form><br>
<?php<br>
}<br>
function my_wp_dashboard_setup() {<br>
if(current_user_can('edit_themes'))wp_add_dashboard_widget('wp_dashboard_GongGao','网站公告','wp_dashboard_GongGao');<br>
}<br>
add_action('wp_dashboard_setup','my_wp_dashboard_setup');</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
添加这段代码之后,进入后台首页就能找到一个关键,让我们填写公告内容。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
之后在前台如何调用呢?在需要之处使用下面的代码即可:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
<p>
代码如下:</p>
</div>
<div id="phpcode11" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
<?php $GongGao = unserialize(get_option('GongGao'));if(!empty($GongGao['content'])): ?><br>
<div id="site-gonggao"><?php echo ($GongGao['link'] ? '<a href="'.$GongGao['link'].'">' : '').$GongGao['content'].($GongGao['link'] ? '</a>' : ''); ?></div><br>
<?php endif; ?></div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
至于样式之类,可根据自身需求进行调整。</p>
頁:
[1]