1. Add announcement article type
First, register an announcement article type, including creating, adding, editing and deleting announcements. Create a new gonggao.php in the same directory as functions.php. The code is as follows:
<?php function post_type_bulletin() { register_post_type( 'bulletin', array( 'public' => true, 'publicly_queryable' => true, 'hierarchical' => false, 'labels'=>array( 'name' => _x('公告', 'post type general name'), 'singular_name' => _x('公告', 'post type singular name'), 'add_new' => _x('添加新公告', '公告'), 'add_new_item' => __('添加新公告'), 'edit_item' => __('编辑公告'), 'new_item' => __('新的公告'), 'view_item' => __('预览公告'), 'search_items' => __('搜索公告'), 'not_found' => __('您还没有发布公告'), 'not_found_in_trash' => __('回收站中没有公告'), 'parent_item_colon' => '' ), 'show_ui' => true, 'menu_position'=>5, 'supports' => array( 'title', 'author', 'excerpt', 'thumbnail', 'trackbacks', 'editor', 'comments', 'custom-fields', 'revisions' ) , 'show_in_nav_menus' => true , 'menu_icon' => 'dashicons-megaphone', 'taxonomies' => array( 'menutype', 'post_tag') ) );}add_action('init', 'post_type_bulletin'); function create_genre_taxonomy() { $labels = array( 'name' => _x( '公告分类', 'taxonomy general name' ), 'singular_name' => _x( 'genre', 'taxonomy singular name' ), 'search_items' => __( '搜索分类' ), 'all_items' => __( '全部分类' ), 'parent_item' => __( '父级分类目录' ), 'parent_item_colon' => __( '父级分类目录:' ), 'edit_item' => __( '编辑公告分类' ), 'update_item' => __( '更新' ), 'add_new_item' => __( '添加新公告分类' ), 'new_item_name' => __( 'New Genre Name' ), ); register_taxonomy('genre',array('bulletin'), array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'genre' ), ));}add_action( 'init', 'create_genre_taxonomy', 0 );
Reference the gonggao.php file of the announcement in functions.php and add the following code at the bottom of functions.php:
include ("gonggao.php");
After that, log in to the backend of the WordPress website, and you will see an announcement tag under the article.
'menu_icon' => 'dashicons-megaphone',
in the above code is the Dashicons icon we set, and the effect is as shown below. If you remove this line, the icon will be the same as the article icon by default.
2. Add announcement style
Place the following announcement content code in index.php where you want it to be displayed:
<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( 'post_type' => 'bulletin', 'posts_per_page' => 3 ) ); while ( $loop->have_posts() ) : $loop->the_post(); ?> <li><?php mb_strimwidth(the_content(), 0, 70, '…'); ?></li> <?php endwhile; wp_reset_query(); ?> </ul> </div></div>
3 of them It means there are 3 announcements, and 70 means each announcement displays 70 characters. This can be set according to your own situation.
3. Add css code
Copy the following code to the main.css file:
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. Add rolling announcement js code
To add the scrolling code of the announcement, you need the jQuery library. Of course, the DUX theme is already loaded. Just copy the following code to 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('autoScroll(".sitediv")',4000) }) ;
Among them, line 4 “.list” is the class style of the ul tag in the calling code; “.sitediv” on line 12 is the class style of the div tag that wraps the ul.
For more wordpress related technical articles, please visit the wordpress tutorial column to learn!
The above is the detailed content of How to make rolling announcements in WordPress. For more information, please follow other related articles on the PHP Chinese website!