毛新 發表於 2023-10-11 00:00:00

为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 &amp;&amp; $_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>
?&gt;<br>
&lt;form method="post" action="&lt;?php echo add_query_arg ('time',time()); ?&gt;"&gt;<br>
&lt;?php if($_POST &amp;&amp; $_POST['update-GongGao'] == 1)echo '&lt;p&gt;&lt;span style="color:red;font-weight:bold;"&gt;更新公告成功&lt;/span&gt;&lt;/p&gt;'; ?&gt;<br>
&lt;?php $GongGao = unserialize(get_option('GongGao')); ?&gt;<br>
公告链接(留空则无链接):&lt;input type="text" name="GongGao" value="&lt;?php if(isset($GongGao['link']))echo stripslashes($GongGao['link']); ?&gt;" /&gt;<br>
&lt;p&gt;公告信息 <br>
&lt;textarea name="GongGao" style="word-break:break-all;width:80%;" rows="4"&gt;&lt;?php echo stripslashes($GongGao['content']); ?&gt;&lt;/textarea&gt;&lt;/p&gt;<br>
&lt;input type="submit" value="提交" /&gt;<br>
&lt;?php wp_nonce_field('GongGao'); ?&gt;<br>
&lt;input type="hidden" name="update-GongGao" value="1" /&gt;<br>
&lt;/form&gt;<br>
&lt;?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>
&lt;?php $GongGao = unserialize(get_option('GongGao'));if(!empty($GongGao['content'])): ?&gt;<br>
&lt;div id="site-gonggao"&gt;&lt;?php echo ($GongGao['link'] ? '&lt;a href="'.$GongGao['link'].'"&gt;' : '').$GongGao['content'].($GongGao['link'] ? '&lt;/a&gt;' : ''); ?&gt;&lt;/div&gt;<br>
&lt;?php endif; ?&gt;</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]
查看完整版本: 为wordpress增加网站公告功能