|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
! h+ W1 J3 ~6 D+ U
- n8 |! z; ?: ?. A. W( R如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
, e" j) X0 S+ `+ h- q: y3 O! r( N! w& L+ Y# Q
以下是创建自定义插件的步骤:
, c5 h% o! A, c* x- t3 S
# g! | I' x$ D1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
, r; p4 R% u5 X% m
' V8 l2 h8 R+ |/ P6 | ```
* Z. U; `% ?& M: B! u <?php7 h& b$ M5 E% u% G3 G/ A6 E5 J3 @7 f/ u- w
/*" @# c, L7 L2 Y# o3 j6 W3 O% H
Plugin Name: Site Wide Notices Plugin! e: f4 |) R, j3 _
Description: Adds a new custom post type for site-wide notices.
8 G- j0 H8 X2 n' x) G Version: 1.0: N$ s e( s7 ]6 G4 C3 q
Author: Your Name
0 x( F7 @9 e P5 C5 k Author URI: http://example.com
6 k6 A4 T2 c& ?* V */6 T/ g* p" y$ j$ w" }/ |
# V. `4 L/ Y6 Z) w9 H // Add plugin code here...
' ]* E* n, m- S+ D0 M ```4 f, s1 A: R- A& A& G5 _, Z1 y Z) N# }+ g
0 ~; ?4 \9 _2 V0 \8 [; P 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
' q- a$ J, x5 B5 u" @4 q9 `" M! _/ r6 g% J% U
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
2 R' z% T, Z0 ]) Y' D8 k( V i; V" y
. n7 m$ ]$ E. Q ```" o5 L6 S0 s6 N5 x/ Q. z0 o
add_action('init', 'create_custom_post_type'); j( _1 A8 Z" ~* |5 n0 [7 J. |' Z
function create_custom_post_type() {( F- Y' w# y' T4 b
$labels = array(& `5 h4 M* |) x% _! k
'name' => 'Site Wide Notices',- J; u- L" s9 B
'singular_name' => 'Site Wide Notice',
D1 B6 Z3 j1 [$ R5 J 'add_new' => 'Add New',6 a) B7 b1 v5 Q4 u8 V/ U- K
'add_new_item' => 'Add New Site Wide Notice',0 @7 U! N9 d2 z0 ~
'edit_item' => 'Edit Site Wide Notice',& V4 c0 z* X6 g Z
'new_item' => 'New Site Wide Notice',. m% ]& ] Y4 l8 b: m: R" ` R
'view_item' => 'View Site Wide Notice',' J. M/ J4 R' J0 c2 R
'search_items' => 'Search Site Wide Notices',
1 p4 p0 C" u, L0 \ 'not_found' => 'No site-wide notices found',4 c2 M g& P1 `8 \
'not_found_in_trash' => 'No site-wide notices found in trash'
4 \7 d; k. P- y1 S2 k" z );: U+ f3 T( d3 `/ i. @4 x
/ y) g" `" n' [4 `8 n5 A: O
$args = array(
9 d' n% K& N8 m+ G+ J- R 'labels' => $labels,
+ j8 V% H0 ]- U% \( S3 ~8 W2 z5 x) o 'public' => true,8 d) H2 h+ d" l' a& t& c
'has_archive' => true,( a% I6 F# _: m/ E* d$ S
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
( W# f( A' @. j' n" K6 a5 X 'taxonomies' => array('category', 'post_tag'),
r# ~* }& _/ E' G 'menu_icon' => 'dashicons-megaphone',' \5 B% F6 K8 {6 ~7 `
'menu_position' => 5,
+ H# ? C' ?7 }" I5 V( u 'rewrite' => array('slug' => 'site-wide-notices')- o- ]. S; W- [ Z- f- I
);: P% ?# I- y$ j
$ K1 j& V( K( a' s4 T) C5 w" H$ h
register_post_type('site-wide-notices', $args);
9 L3 i4 i2 j! O, Y }
3 d. C8 z& `7 y ```# v/ W) p% y! t
2 A& L8 Q* X8 O9 F5 ]7 I8 A8 _- C 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
5 B' s5 ~: Q g$ {8 l7 j7 k6 v+ x3 c9 Q( F6 b" m
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:! K) ?/ t; ], y7 j; i" J8 p
4 i0 e% t1 ^% [
```
$ ^, }) K+ ^# O1 L: Q add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
9 n& f) ~9 X2 j' j function add_site_wide_notices_boxes() {
6 D! A- b w' w add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' d: l6 V; O. e }- }) \$ I" Y1 R& x t
% q; ?" h8 }, h' z& N1 G% Z; R
function notice_details_meta_box($post) {
6 r; h0 i8 i& L, u wp_nonce_field(basename(__FILE__), 'notices_nonce');
1 S5 B9 M: R% q9 |/ s9 j $notice_title = get_post_meta($post->ID, 'notice_title', true);
O _# j* c& P/ S3 D $notice_content = get_post_meta($post->ID, 'notice_content', true);
( B' _" M- |3 p1 l( Q ?>
9 |- ]: V6 N1 S/ @1 U9 g <p>" o) k5 L/ P( ?7 P4 m
<label for="notice-title">Notice Title</label><br>/ ^7 Z# L& B7 w7 y$ M
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
6 ~# @( S2 s- y+ u8 t </p>' @1 |; X; E; X2 \* w- z
<p>, r& O* E+ @* d) B- h8 b
<label for="notice-content">Notice Content</label><br>
$ e. w, x# O6 |1 ~; }) g <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>; t! G$ A' v8 U5 ?4 G
</p>
( y. R& u& ?( F) [ <?php
! Y/ R/ f3 [" f) z6 K }+ ~ k3 F6 }& r" N: Q; o! w
7 ~* `* T6 B* | add_action('save_post', 'save_site_wide_notice_meta_box');
; X+ p f2 \. X0 l0 m" T function save_site_wide_notice_meta_box($post_id) {2 W: _. i! ^8 w% J( `' n
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))+ V: o2 c% z, J O. [/ ^
return;
7 Z$ F: g0 v/ ^: V* P+ m- @ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
8 ^% o+ n; i3 @6 P4 @ C return;4 P, X# P0 @" A2 p
1 a) d0 y4 A ^- w- S if (isset($_POST['notice_title'])) {
/ A* Y2 A" J j6 @6 g1 D$ h/ N$ g update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
0 F8 U |" g) A6 m* V4 i( T }
: E8 s; N4 o6 ]' I if (isset($_POST['notice_content'])) {
+ [( L: n {' {! a1 X# M: V1 K0 Q* k update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));8 _8 H5 N% E4 e4 n! D
}2 z! N, c2 N9 _3 {
}
6 {0 T4 I" r' `/ u ```
0 w9 d- {/ f4 U( u6 q6 L) w/ {; q9 G
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 O) z/ {7 g m8 r6 ]" Y& c
S) X" B9 r% a' h4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
. I+ V$ M5 N# \; E% J0 H" W5 V. B4 j# w0 [( ]& ]
```
# K* v1 d4 U7 G3 z1 W $args = array(
|4 B3 x6 v% r/ n- |. V; ^1 p 'post_type' => 'site-wide-notices',3 E% \" n. O$ y; N
'posts_per_page' => 3,7 W* x; `. }; `2 d
'order' => 'DESC',' V/ \0 q/ C, I, R: v; [5 f) N! C
'orderby' => 'date'2 ~* W% j: ~. E. f, G" t4 d
);- h5 H, ]5 s [3 r
$query = new WP_Query($args);
) C" ?2 q& u2 t if ($query->have_posts()) :
+ I1 r' N$ C% B& d1 b# | while ($query->have_posts()) : $query->the_post(); ?>0 l/ n2 [7 N, x4 s! r+ W6 s
<div class="notice">
6 J6 O# L0 t; m <h3><?php the_title(); ?></h3>
) `8 O0 N' B5 }' _ <div class="notice-content"><?php the_content(); ?></div>
# A1 V, ]; J3 z </div>
" p$ P7 Y+ O# s4 J+ x <?php endwhile;7 p$ ^ ~( X2 p6 g+ [( R9 s3 F6 ]
wp_reset_postdata();
% m) l& j. c6 K8 J! Z6 y+ D/ l! n endif;: O5 b& s6 g1 ^* c; S2 d/ o
```5 e$ `! ~7 N. D1 e' g$ v
( b& Z: l1 W1 A' H0 ?% [2 V 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|