|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?. b4 R# n0 W, W% F' Y
; l( x; s% I$ Q+ w1 k4 z0 B如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。0 B% `/ ^: L S6 `, S
" y" c; P; K9 a* N5 B; i" ^
以下是创建自定义插件的步骤:
+ z7 B* C$ i) d. j0 ^3 {( a+ K2 r: b/ h; f0 P# o" `
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
, i$ p" U. G- k- Z. S" M
& w# @+ q- T( \, ~ ```5 E5 ^5 m' }' J
<?php
( c% V g, e8 U. o3 c1 s5 n /*) c3 f- ^ l' ~( t# T
Plugin Name: Site Wide Notices Plugin
9 j! ]8 m K2 S2 O% B Description: Adds a new custom post type for site-wide notices.
6 A0 L$ `$ B h0 I Version: 1.0
! ]) V+ O% Z7 f0 {9 K1 ~5 q& ]- z0 R" t Author: Your Name2 \0 q: X. Q: j* g+ M h
Author URI: http://example.com; E/ E( S, `# G+ K6 e9 m8 X
*/" z" i% b# e0 O/ g" W! t A$ v
! t4 e. D9 v4 J, }! c7 m
// Add plugin code here...
8 ^* |1 N& [: V: W+ f ```/ v9 k0 o- z3 r- k, k
7 L$ I3 e- L8 \) c" `0 z, d
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。. Y4 f T2 a& N6 ?, K9 x/ P8 f% Z
. ~7 w7 L6 Z6 o ~2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:2 y* I- A' [- H& ?* s
. H" j: Z: z9 S1 N ```
. C; ^1 G# @7 V6 I add_action('init', 'create_custom_post_type');
( d* K7 D9 g r2 V# S$ D# ^& C5 N! j4 C function create_custom_post_type() {. p% `) H) V" @- _! g3 W1 J
$labels = array(
* t4 Z; e1 r8 v) `( \' ^3 z6 }+ O) @ 'name' => 'Site Wide Notices',
: |$ h7 S4 t& [1 N- M 'singular_name' => 'Site Wide Notice',
" Q) U! E3 F* W% q5 t4 | 'add_new' => 'Add New',% S( {% `- k% Z/ r9 A9 A
'add_new_item' => 'Add New Site Wide Notice',1 T4 x. K( x- q
'edit_item' => 'Edit Site Wide Notice',
% C' o; S, K4 r1 S. y 'new_item' => 'New Site Wide Notice',& Y d5 U: K1 S
'view_item' => 'View Site Wide Notice',
- Q. H+ w: [/ V* S 'search_items' => 'Search Site Wide Notices',, y0 \& X4 j6 v1 ?3 J% Q
'not_found' => 'No site-wide notices found',
7 ~0 s, a- X. [+ P i; e 'not_found_in_trash' => 'No site-wide notices found in trash'
' F: O6 u+ n# r4 l$ R) T7 ?" j% W );
1 j7 ~- O9 s y2 H" Y4 b; p6 C4 }3 [, v& Y, {5 W; D2 @/ Z/ p) C9 W
$args = array(
% K# T2 ]; @/ A. e4 A1 l1 r 'labels' => $labels,! J. r. C5 B4 @9 o: l) v
'public' => true,
8 U( J( I# b& `( F9 } 'has_archive' => true,' |$ J+ Q7 g! J1 F+ r& x
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),1 G% g6 U) D5 B- [1 B
'taxonomies' => array('category', 'post_tag'),
3 ~$ @2 D0 ^4 P 'menu_icon' => 'dashicons-megaphone',
F' V9 C$ ^: C! P2 S* O+ d9 A/ [ 'menu_position' => 5,
8 _) Y; O' t0 M) }; O. b 'rewrite' => array('slug' => 'site-wide-notices')4 `; }7 o, _' j$ b7 H1 s$ e$ r/ r/ w
);
# V- F1 J; T) b: r
/ r. N) t2 I; z& i register_post_type('site-wide-notices', $args);# l7 c! z% x4 d
}2 c+ S0 Z7 [- Q: ~! ?7 U* h. @
```7 X* y- V& m3 O5 S& Z
: s* I# I3 F6 T 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。* T- F- M8 x0 S( ~& z# c
/ b+ N3 m6 G R# {9 q
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:7 u- w: c& f; ]
! V$ @ a' B2 n2 O ```' R D: {$ k& {( Z7 I
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
5 a5 ]) @# R/ U. O) d0 k5 Q function add_site_wide_notices_boxes() {' W8 O& I' E( @( L& A
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
. k9 \. H& Z# G8 `) z! O, R5 X }
8 H+ C2 k) W/ v- T" o9 V7 s' y: R% [/ J$ g, F
function notice_details_meta_box($post) {2 j7 J- x7 L0 o! q6 z
wp_nonce_field(basename(__FILE__), 'notices_nonce');
! I1 R8 f2 q/ A0 J% M $notice_title = get_post_meta($post->ID, 'notice_title', true);
% C2 |$ r+ o4 s* g $notice_content = get_post_meta($post->ID, 'notice_content', true);
0 I! ~2 p0 s# R3 M; { ?>
* U1 }( C5 L$ M( \- ?, d; i <p>3 f& ~8 x8 _# T) {2 j/ ]. {
<label for="notice-title">Notice Title</label><br>6 r# ]2 e& m. T' P, M8 r j" W
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">0 ~( [! v) L2 B, Z5 u1 f( c
</p>
) @3 w/ b. n: u5 Q! S <p>8 P% u A; p1 q# w- [0 M U
<label for="notice-content">Notice Content</label><br>
" z$ `; S/ p1 M: g% k <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
3 k% C5 \, r( ~% ^$ |3 U; h" H </p>
) e5 a, _3 }/ X, _' G+ q; S <?php
& v+ {% {4 G7 M# K* ~ }
* Y7 S: k" C1 x4 S; I4 L+ r$ T% Z; j
add_action('save_post', 'save_site_wide_notice_meta_box');" p$ l3 X, k: M, j* Z, }3 Z
function save_site_wide_notice_meta_box($post_id) {2 |) `9 t- v7 _: Y# O5 f0 a
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
& r# a0 n5 H% i) Z2 \9 G return;# b6 k: s+ \; Q
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)8 D2 x' t; M& X, ?' J9 ~$ f4 L+ {
return; K) C ? b& S# V
3 j- x5 r; k4 M; A% D% J4 p
if (isset($_POST['notice_title'])) {
% W2 ~ _! ]3 Y* M( R update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" Q' w8 Y/ H- @2 n# R0 r: V6 O- ^* m
}, K4 U. [9 V( r) a
if (isset($_POST['notice_content'])) {
$ A1 `% \" O) v; o; n( \1 Y% L update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));1 {. L( V& c: i$ C! U
}( E- z* g- ?$ f1 y3 T
}/ v1 J% y% O; ]+ s/ [8 D7 i
```
6 @" j4 H) A. |0 {- d
, f2 q. U) X5 Z% p( D7 r, ` 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。1 G/ _. U8 N4 D, j8 F( }
* M8 Y Q+ q9 _7 r3 R4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
8 j7 Y$ h9 n; S# Y" Q: z: {) B3 s- K9 X) }# S4 b. A" L
```; C. G7 C9 h R5 r: p" {
$args = array(
4 R/ o' S( Z7 G- V" C# i# O; R 'post_type' => 'site-wide-notices',/ g* i. A7 J/ L) S
'posts_per_page' => 3,
% o; W: \* X4 X+ W0 y' U 'order' => 'DESC',* o+ f: F4 n. t- v* V, Y: Z3 i
'orderby' => 'date'
% V' o; i3 }& h" J" s );8 E4 G( `+ M6 [8 ~
$query = new WP_Query($args);
0 y7 {6 U& e; H3 y if ($query->have_posts()) :7 `& G3 j+ c$ u. P& b- w
while ($query->have_posts()) : $query->the_post(); ?>, n# v% \) n! E
<div class="notice">
+ U, c+ D) [/ F. S9 V <h3><?php the_title(); ?></h3>5 A9 K$ f, Y7 V' F
<div class="notice-content"><?php the_content(); ?></div>
- O$ e& t) {- p" z# b% m/ c </div>1 m! i9 O$ k8 W: T7 o7 g3 g
<?php endwhile;
/ ~: [! K4 F# S2 j$ u wp_reset_postdata();* ?- Z/ ]# s4 b2 f( U( V4 m# N
endif;
& W J7 n; E% a ```/ F+ e5 f3 X7 k- y5 E; R
- x$ K! R: n# C& d: n0 U 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|