|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
5 c G4 k' a( F, E6 s1 P. |) h) p
& e9 _4 E7 t' S6 D+ T L# R: z如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
2 ?) o, H1 U6 Z
: Q+ o5 P0 S( o! f以下是创建自定义插件的步骤:
2 T' ` I" w7 x, A- C; v
1 ]6 G! {; [" W/ m$ W1 c7 K- x1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:7 @ z# f h- Y9 Y7 n, B
5 |; x4 I' c: o, G ```) O$ {% Y, \7 \6 h+ _0 U3 \9 O1 x3 f
<?php3 H8 r; m1 V5 [+ o
/*
/ A) U$ }* H. }% E1 } Plugin Name: Site Wide Notices Plugin
9 T* H2 L; i2 I8 P d5 Q& T5 v( G Description: Adds a new custom post type for site-wide notices.+ E2 ~5 }- f1 e5 S1 a7 M
Version: 1.0
$ Q4 \- f8 m+ L& H" Y N Author: Your Name, b5 H. B6 d m2 Z2 D
Author URI: http://example.com2 g. e- P( Y$ z; Y3 n' U
*/9 T; u3 [7 p0 L! _ o2 t1 b2 I" b) I
0 }) K0 w3 T; c/ f // Add plugin code here...
7 M1 T) Y- p* p8 a+ ~. B ```, t; q, P" O7 D' B
) {0 j0 R' `9 x) d* H/ \
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。: l3 Y2 Q$ a& ]9 a8 ?$ X
- b* H! t, K/ Y. s3 ~! `. @* m
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
2 X' X* X/ J( c: J: ^* G% I' w) n( B; m, z8 K* v8 D
```
" B* B( \( c+ I" ?: N+ K2 r8 I1 v4 N add_action('init', 'create_custom_post_type');( b4 x8 ?' V V
function create_custom_post_type() {! ^8 K/ F/ @( U. J8 C
$labels = array(! e+ X k$ L+ N( s* e/ ^5 |$ `
'name' => 'Site Wide Notices',
! ^& G$ q9 s. j7 N4 X 'singular_name' => 'Site Wide Notice',1 t. e- k* l* Q. a) \
'add_new' => 'Add New',* a7 H6 \3 G& c: f9 l" x) a
'add_new_item' => 'Add New Site Wide Notice',
1 l% l2 X/ R3 l! o; O2 @ 'edit_item' => 'Edit Site Wide Notice',0 J1 d. s( V g$ q6 m$ W W
'new_item' => 'New Site Wide Notice',
! X2 }+ Y8 U' v; L: ^) X- c0 j 'view_item' => 'View Site Wide Notice',
6 H/ ^5 f$ w8 C4 T2 c9 z) F5 R 'search_items' => 'Search Site Wide Notices',2 a, M4 v B7 G* u( z7 m
'not_found' => 'No site-wide notices found',0 n6 M; M1 @4 C8 j' F
'not_found_in_trash' => 'No site-wide notices found in trash'3 q! E/ A* v( q$ ^1 R3 L9 c( l
);
+ D6 T6 p }0 I) W+ \
+ J1 ~) N3 G- `6 b3 c $args = array(% n3 I6 @# w1 c: E
'labels' => $labels,
# m) Q Z5 B3 |1 a1 l; q, k# J 'public' => true,
* E: _% N( l: b0 v! a3 o 'has_archive' => true,
) E0 ]" r4 S: r' I. u 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
% C; E. W: ^( f2 v: ]8 A 'taxonomies' => array('category', 'post_tag'),
) d+ \9 z& n( J: e, V1 @ 'menu_icon' => 'dashicons-megaphone',
5 B$ C6 m- H9 }0 ~/ N 'menu_position' => 5,
7 d7 D/ L7 K- ? R 'rewrite' => array('slug' => 'site-wide-notices')
6 [. b! H7 Q( T );& ^, N! t6 m6 q! X9 d/ Q7 x; y) v% U8 R
* X+ V x: Z" V7 J9 K5 A; D' q' w$ g
register_post_type('site-wide-notices', $args);
8 u& u# J8 L8 l& M' i }3 {/ o0 W1 K+ P) b0 D' Y- l' \
```
* d: @8 w# F& l& K9 |; y S9 x3 e7 ^+ U" a
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。% T. b3 G1 C8 n) [1 |1 _
$ q# h9 U. q" I7 l; N& d8 S
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:5 Y) p) w1 U, _4 g
1 A8 ]! T7 p8 M3 q0 W ```
2 H' ~! f8 |/ Q) R" _! X1 q3 r add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
Y" V F |2 ]3 }( S4 Q8 \% v function add_site_wide_notices_boxes() {/ d5 n6 h& t1 c, T
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');- ~- ?* ^; h; B0 o& _, E3 @
}% n; H9 I" i- w$ E8 u: l
# d1 J# A8 E8 k
function notice_details_meta_box($post) {
- s; Y0 B: [* B( q1 S" g& i7 K wp_nonce_field(basename(__FILE__), 'notices_nonce');
' P4 P+ h$ n5 m# E+ Q+ R( v9 @6 t* | $notice_title = get_post_meta($post->ID, 'notice_title', true);/ z* C- B6 s& D% Z$ {0 w8 M
$notice_content = get_post_meta($post->ID, 'notice_content', true);( o" R, U" J1 m9 o, D' K
?>* \6 o' K. M0 C) |# {
<p>
; J Q8 d3 {4 F( W% w0 v8 c2 _ <label for="notice-title">Notice Title</label><br>
, L6 S8 {2 w: P4 e, H2 E0 i <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 D: b. y; r& r3 W9 F3 O _& O, T
</p>
/ F- x& J2 [7 S2 ^7 [8 Q <p>
# s* R) h9 g1 |* t; Z <label for="notice-content">Notice Content</label><br>9 A& w. S$ U }1 x7 x1 D3 \
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>0 P: F" c3 {1 D7 j* W4 Y1 {/ _
</p>5 `' t0 V" E/ g1 _* r
<?php
; q0 Y2 h/ u2 R9 _9 X) `( e }
0 u" \+ ^5 _. W" p
4 ~# S! P" V7 r* n- R1 W# _ add_action('save_post', 'save_site_wide_notice_meta_box');; w9 b& f- o' ? \4 u" |
function save_site_wide_notice_meta_box($post_id) {
( i4 q1 W9 q) ?1 {/ ? if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))3 C4 p. r) \2 T3 B2 M5 Z0 m
return;; U! R0 _6 P9 R) l J0 _4 w# t5 R
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
9 T4 R0 v! t% E' F, |& ]0 O* m0 x/ C return;
$ Y, N0 ~( O: s% @3 ^: W$ _
+ t6 r3 [- D- G$ N u. G& h2 L if (isset($_POST['notice_title'])) {) }. F6 @0 l4 ~% o9 V9 ?0 @
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
: ?( Z/ K% N; r" ` }
2 |" z& U/ e( l if (isset($_POST['notice_content'])) {2 N* \3 z3 o4 n, T/ q
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
+ y; q1 Z) ^: ]& n2 y: @; Z5 y }
. |4 R# K' \' v, r5 q( X# L, a }8 ]8 ^) ^2 X0 h- R# i
```3 H5 N, r, Q/ g2 s! m. R- M
# y" A" J( D6 @0 o" e9 o& y 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。. J2 j5 A8 v1 }8 Q/ m9 ?; Y+ T- q
# `3 T' R9 Z5 U! m4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:& n2 P# D3 j$ O$ Y8 ~! r; l
) m/ j0 R; E4 s: |
```: i6 b. A( O$ T9 ^( B' j6 g2 M
$args = array(& [7 E! Q6 Y9 r9 ]: h, h
'post_type' => 'site-wide-notices'," O, ^% x8 ~ C: S& d
'posts_per_page' => 3,9 M' P3 m' o* E7 c! w' l
'order' => 'DESC',
$ ^3 L) F7 I8 w1 ~8 W 'orderby' => 'date'; [& f) R( G( x
);, g9 ^7 V5 b6 V' C- \6 C
$query = new WP_Query($args);( |. `5 ~/ h* b& S+ q
if ($query->have_posts()) :
4 f( |7 y8 }! ]3 t1 J while ($query->have_posts()) : $query->the_post(); ?>& X |! ]9 C7 f% ]) q
<div class="notice">
0 U: ^$ ?7 e0 D <h3><?php the_title(); ?></h3>
& F* i2 S0 T: s- D; } <div class="notice-content"><?php the_content(); ?></div>
/ h) m2 K9 o z% V; d8 s3 b* y </div>; _/ l, `# v J. N* L: C7 q6 {
<?php endwhile;
. X6 O$ p: Q8 @3 t: { A wp_reset_postdata();' c: g0 P/ ^2 T9 v8 S! G
endif;
( [5 d/ t5 m; a# d ```
7 Y4 u$ Z& v, g
+ @; k6 W9 r( X( v; C! d: @ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|