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!

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.

WordPress is a Content Management System (CMS). It provides content management, user management, themes and plug-in capabilities to support the creation and management of website content. Its working principle includes database management, template systems and plug-in architecture, suitable for a variety of needs from blogs to corporate websites.

WordPressisgoodforvirtuallyanywebprojectduetoitsversatilityasaCMS.Itexcelsin:1)user-friendliness,allowingeasywebsitesetup;2)flexibilityandcustomizationwithnumerousthemesandplugins;3)SEOoptimization;and4)strongcommunitysupport,thoughusersmustmanageper

Wix is suitable for users who have no programming experience, and WordPress is suitable for users who want more control and expansion capabilities. 1) Wix provides drag-and-drop editors and rich templates, making it easy to quickly build a website. 2) As an open source CMS, WordPress has a huge community and plug-in ecosystem, supporting in-depth customization and expansion.

WordPress itself is free, but it costs extra to use: 1. WordPress.com offers a package ranging from free to paid, with prices ranging from a few dollars per month to dozens of dollars; 2. WordPress.org requires purchasing a domain name (10-20 US dollars per year) and hosting services (5-50 US dollars per month); 3. Most plug-ins and themes are free, and the paid price ranges from tens to hundreds of dollars; by choosing the right hosting service, using plug-ins and themes reasonably, and regularly maintaining and optimizing, the cost of WordPress can be effectively controlled and optimized.

The core version of WordPress is free, but other fees may be incurred during use. 1. Domain names and hosting services require payment. 2. Advanced themes and plug-ins may be charged. 3. Professional services and advanced features may be charged.

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

People choose to use WordPress because of its power and flexibility. 1) WordPress is an open source CMS with strong ease of use and scalability, suitable for various website needs. 2) It has rich themes and plugins, a huge ecosystem and strong community support. 3) The working principle of WordPress is based on themes, plug-ins and core functions, and uses PHP and MySQL to process data, and supports performance optimization.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.