首頁  >  文章  >  CMS教程  >  Wordpress滾動公告怎麼做

Wordpress滾動公告怎麼做

尚
原創
2019-07-12 11:41:412965瀏覽

Wordpress滾動公告怎麼做

1、新增公告文章類型

首先,註冊一個公告的文章類型,包括公告的新建,添加,編輯與刪除。在functions.php的同級目錄下新建一個gonggao.php ,程式碼如下:

<?php
function post_type_bulletin() {
 register_post_type(
 &#39;bulletin&#39;,
 array( &#39;public&#39; => true,
        &#39;publicly_queryable&#39; => true,
        &#39;hierarchical&#39; => false,
        &#39;labels&#39;=>array(
        &#39;name&#39; => _x(&#39;公告&#39;, &#39;post type general name&#39;),
        &#39;singular_name&#39; => _x(&#39;公告&#39;, &#39;post type singular name&#39;),
        &#39;add_new&#39; => _x(&#39;添加新公告&#39;, &#39;公告&#39;),
        &#39;add_new_item&#39; => __(&#39;添加新公告&#39;),
        &#39;edit_item&#39; => __(&#39;编辑公告&#39;),
        &#39;new_item&#39; => __(&#39;新的公告&#39;),
        &#39;view_item&#39; => __(&#39;预览公告&#39;),
        &#39;search_items&#39; => __(&#39;搜索公告&#39;),
        &#39;not_found&#39; =>  __(&#39;您还没有发布公告&#39;),
        &#39;not_found_in_trash&#39; => __(&#39;回收站中没有公告&#39;),
        &#39;parent_item_colon&#39; => &#39;&#39;
        ),
        &#39;show_ui&#39; => true,
        &#39;menu_position&#39;=>5,
        &#39;supports&#39; => array(
        &#39;title&#39;,
        &#39;author&#39;,
        &#39;excerpt&#39;,
        &#39;thumbnail&#39;,
        &#39;trackbacks&#39;,
        &#39;editor&#39;,
        &#39;comments&#39;,
        &#39;custom-fields&#39;,
        &#39;revisions&#39; ) ,
        &#39;show_in_nav_menus&#39; => true ,
        &#39;menu_icon&#39; => &#39;dashicons-megaphone&#39;,
        &#39;taxonomies&#39; => array(
        &#39;menutype&#39;,
        &#39;post_tag&#39;)
 )
 );}add_action(&#39;init&#39;, &#39;post_type_bulletin&#39;);
 function create_genre_taxonomy() {
 $labels = array(
 &#39;name&#39; => _x( &#39;公告分类&#39;, &#39;taxonomy general name&#39; ),
 &#39;singular_name&#39; => _x( &#39;genre&#39;, &#39;taxonomy singular name&#39; ),
 &#39;search_items&#39; =>  __( &#39;搜索分类&#39; ),
 &#39;all_items&#39; => __( &#39;全部分类&#39; ),
 &#39;parent_item&#39; => __( &#39;父级分类目录&#39; ),
 &#39;parent_item_colon&#39; => __( &#39;父级分类目录:&#39; ),
 &#39;edit_item&#39; => __( &#39;编辑公告分类&#39; ),
 &#39;update_item&#39; => __( &#39;更新&#39; ),
 &#39;add_new_item&#39; => __( &#39;添加新公告分类&#39; ),
 &#39;new_item_name&#39; => __( &#39;New Genre Name&#39; ),
 );
 register_taxonomy(&#39;genre&#39;,array(&#39;bulletin&#39;), array(
 &#39;hierarchical&#39; => true,
 &#39;labels&#39; => $labels,
 &#39;show_ui&#39; => true,
 &#39;query_var&#39; => true,
 &#39;rewrite&#39; => array( &#39;slug&#39; => &#39;genre&#39; ),
 ));}add_action( &#39;init&#39;, &#39;create_genre_taxonomy&#39;, 0 );

在functions.php中引用該公告的gonggao.php文件,在functions.php的底部加上如下程式碼:

include ("gonggao.php");

之後,再登入wordpress網站的後台,就可以看到在文章的下面多了一個公告標籤。
上述程式碼中的

&#39;menu_icon&#39; => &#39;dashicons-megaphone&#39;,

就是我們設定的 Dashicons 圖標,效果如下圖。如果去掉這行的話,圖示預設和文章的圖示一樣。

2. 新增公告樣式

將下面的公告內容程式碼放在index.php 自己想要顯示的位置:

<div id="site-gonggao"><div class="site-gonggao-div"><i class="fa fa-volume-up"></i> </div>
 <div id="site-gonggao-div2" class="sitediv">
    <ul class="list" id="siteul">
    <?php $loop = new WP_Query( array( &#39;post_type&#39; => &#39;bulletin&#39;, &#39;posts_per_page&#39; => 3 ) );
          while ( $loop->have_posts() ) : $loop->the_post();
     ?>
      <li><?php mb_strimwidth(the_content(), 0, 70, &#39;…&#39;); ?></li>
      <?php endwhile; wp_reset_query(); ?>
      </ul>
 </div></div>

其中3代表有3 則公告, 70 則表示每個公告顯示70 個字元。這個可以根據你自己的情況設定。

3. 新增css 程式碼

將下面程式碼複製到main.css 檔案當中即可:

div#site-gonggao {
    line-height: 25px;
    height: 30px;
    background-color: #FFF;
    padding-left: 10px;
    color: #666;
    -webkit-box-shadow: 0 5px 5px #D3D3D3;
    box-shadow: 0 5px 5px #D3D3D3;}
 #site-gonggao .list {
    padding-left: 5px;}
 .site-gonggao-div {
    float: left;}
 .fa-volume-up:before {
    content: "\f028";
    color: #428bca;}
 #site-gonggao a {
    color: #1663B7;}
 #site-gonggao a:hover {
    color: #09F;}
 #site-gonggao-div2 {
    overflow: hidden;
    height: 30px;}
 #site-gonggao-div2 .list li {
    height: 30px;
    line-height: 30px;
    overflow: hidden;}
 #site-gonggao-div2 .list li p {
    display: inline;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;}

4. 新增捲動公告js 程式碼

添加公告的滾動程式碼,需要jQuery 函式庫,當然DUX 主題是已經載入了的,直接將下面程式碼複製到header.php 中即可

function autoScroll(obj){  var aa=document.getElementById("siteul").getElementsByTagName("li").length;if(aa!==1){
    jQuery(obj).find(".list").animate({  
          marginTop : "-30px"  
      },500,function(){  
      jQuery(this).css({marginTop : "0px"}).find("li:first").appendTo(this);  
      })  
      };
      }
   $(function(){   
       setInterval(&#39;autoScroll(".sitediv")&#39;,4000)  
     })  ;

其中,第4 行的「.list」 是呼叫程式碼中,ul 標籤的class 樣式;第12 行的「.sitediv」 是包裹ul 的div 標籤的class 樣式。

更多wordpress相關技術文章,請造訪wordpress教學欄位進行學習!

以上是Wordpress滾動公告怎麼做的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn