|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
q) Z4 C- I3 x5 e- D# n4 q. ?7 g$ c, D( m, \0 y
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
b' k" M% L0 _ {8 d# p2 F: J K: }8 e; Y8 M, b
以下是创建自定义插件的步骤:
. D) k$ j9 a2 J5 b1 w2 a6 k% q6 v7 M7 K% h0 W
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
8 a/ }. d9 V2 u5 p( C: {; U! ~4 T* D) V
``` p: v0 G% k: F3 V3 a5 }! }5 j4 B
<?php
+ H8 t/ H W$ A& K /*
+ B0 b& n$ s8 ^/ e/ i y1 ^ Plugin Name: Site Wide Notices Plugin9 ^& t0 _* @2 a, U# k. ?
Description: Adds a new custom post type for site-wide notices.( }' p5 [, N& [+ |7 f6 i7 A
Version: 1.0
; Y/ Q' B0 A3 k% M4 c9 U8 N& T Author: Your Name6 q$ k5 ~+ h5 B8 J% U8 }
Author URI: http://example.com
8 x+ ]2 ]. U6 I }8 j */
# U4 ?/ e. }) K4 I: E. M- D4 P
# r8 X( }9 R& u1 w' y" h9 X // Add plugin code here...
$ l7 S' k" O- Y u ```
: a. f( L6 j* K# N0 G) C4 X" A2 K; G ~$ ^; X7 M
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。& n$ s f. E, C( S! ^7 \& F) [7 O
5 z* R, _. s% J. l2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 r6 t4 F! N2 _3 @* R
& g" Z, F2 v+ H
```, L, q2 @& x4 j: T6 y
add_action('init', 'create_custom_post_type');- g% q4 _1 g: U# N+ f m4 |
function create_custom_post_type() {
) F( ]. A* w: z3 R $labels = array(& a# c2 k W% M" C0 N
'name' => 'Site Wide Notices',* l) |& ]. g; ~% ], C; X# Z
'singular_name' => 'Site Wide Notice',
! ~% c5 u9 R- ^1 k3 _6 I/ r 'add_new' => 'Add New',' f, I7 O8 Y1 r0 q: P7 X# Z5 c
'add_new_item' => 'Add New Site Wide Notice',
* L: O+ X2 ?. n) ^ 'edit_item' => 'Edit Site Wide Notice',5 q, [7 z- e! l9 j/ S' |# N3 a7 M
'new_item' => 'New Site Wide Notice'," [9 v* {# l: K; j- V' J
'view_item' => 'View Site Wide Notice',3 B1 @9 [$ ^& p7 C* C
'search_items' => 'Search Site Wide Notices',
N1 r; Y3 ~3 _( X# i7 F 'not_found' => 'No site-wide notices found',9 l- Q5 O }2 G1 F
'not_found_in_trash' => 'No site-wide notices found in trash'
! L9 h+ t; f, P5 R& F1 q0 _4 P* S );
- L, Q$ t1 s. J$ m8 Z, K* P8 S6 P8 Z7 s
$args = array(
( \# y# S5 \/ A- l5 h& T) X2 L 'labels' => $labels,
3 i) Y8 I1 l- m4 y! E9 G 'public' => true,5 _8 `/ J% X) B1 p) C" _( H( T
'has_archive' => true,
g/ T: k3 A' J* v; b: T 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
$ r4 N& \3 C3 S0 E' F) n 'taxonomies' => array('category', 'post_tag'),
% V! W, a$ _& [- p 'menu_icon' => 'dashicons-megaphone',
, q! I" S$ z4 L 'menu_position' => 5,# O# t' z( F/ o' `
'rewrite' => array('slug' => 'site-wide-notices')3 g) }" r) s7 e
);7 \" e) f' M" W f+ c r+ a d: ?9 E
8 G+ v: c, H' x register_post_type('site-wide-notices', $args);6 N" M. v: r7 W$ V" l3 Y8 t
}
: |5 X8 Y/ N3 M( J) v6 u4 A ```
* q8 ^4 ?0 h; j$ z3 @* e. e9 v" |: }# ]6 ^. O
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。# l# Q6 m" x* x( i: z2 U. M: D
! l. N* r, y" R R6 ]5 p/ ^" u
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ Y3 f j" P; l% n6 ?8 m! A8 W
' X( j) ?% e( [ a2 Q) \6 [ ```; r; b$ _7 s& D2 Z
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
. E9 e$ |/ e6 x& A function add_site_wide_notices_boxes() {
1 p5 M% X5 z; K" {/ l, R add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');2 L$ O; B) T" R' A1 b3 ?. y/ W
}
' ?; B7 ~5 E' E S7 a- K J
1 x2 y6 j: N7 J v8 k( M function notice_details_meta_box($post) {
$ ^: Z- E/ g0 d3 U* f- b wp_nonce_field(basename(__FILE__), 'notices_nonce');) n# j. f4 I! F
$notice_title = get_post_meta($post->ID, 'notice_title', true);! b, Q7 i) |1 K- T2 r0 w1 T
$notice_content = get_post_meta($post->ID, 'notice_content', true);
# i7 s) D6 V# H& Y ?>4 V/ i! X" n4 j
<p>
( _; N/ a l; l" w9 a) f <label for="notice-title">Notice Title</label><br>
5 F5 v( ^2 j& X <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">" V- E2 @1 h/ u
</p>
. W" l3 h9 }) E% I5 ^. N <p>5 d2 c" Z: j( \: ^3 z/ m. A1 W( A
<label for="notice-content">Notice Content</label><br>! }! `6 |( H4 n; E2 c
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>" S. O% H3 f# }- K* ?9 a0 M. n
</p>7 B y6 q S" ?7 R
<?php: x" U$ F9 y" `. o
}
4 j& r1 o+ }3 N9 e8 C d+ O; u" U7 @4 \
add_action('save_post', 'save_site_wide_notice_meta_box'); a4 A8 b0 ^, X9 \+ o# R
function save_site_wide_notice_meta_box($post_id) {0 \' z) h! O8 e) s- @) N
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))1 K' L# I& ~6 @
return;" {( e1 F8 m- e/ m
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
) P0 Y9 m# q' y- A return;
; O- q* w1 |( w9 {' g, T9 |+ C# A8 A9 r0 a
if (isset($_POST['notice_title'])) {4 \. Y8 D$ x+ A% ]) p
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
+ G6 k6 ?2 ]2 s/ S& W( L& E }
, Y/ _ G _" {) t: ~8 l# @9 Q if (isset($_POST['notice_content'])) {
$ ?6 s6 D# G/ c update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 Q- D: z, O, X- ^ }
9 C6 C7 z$ q$ g2 D }
/ J+ N. a/ g: g4 D ```4 u; i: ?/ x8 M
r h+ U; U- l5 ?5 s( o0 U) ?7 _
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。+ Q7 W g; { F5 n$ \) `# ^ L8 O
" A5 n: k3 Y0 \8 ^6 M4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
! p9 w% Z! S- M6 t1 j4 {1 y$ J
4 @; R! e3 F( o9 n8 { ```
3 G1 A9 H- T. Z" w. h1 r $args = array(
( N5 X. v7 ?' x( B( j- r/ T5 s 'post_type' => 'site-wide-notices',- y H2 G0 t! l+ P9 o9 a) X, @( F
'posts_per_page' => 3," w( [4 E1 x# f6 K2 Z4 H
'order' => 'DESC',
8 k4 @9 m' Q4 v+ | 'orderby' => 'date'
P# h* f3 d& Q* N( t );
?: K- S2 K, l0 B! R$ L $query = new WP_Query($args);
1 S- |1 F4 W l; G: d if ($query->have_posts()) :
) H; L0 |' A8 h7 F* B while ($query->have_posts()) : $query->the_post(); ?>: c y* H* x$ k9 j4 V% L- {: w
<div class="notice">. }# t2 X/ \7 B& S7 w2 I9 l! r
<h3><?php the_title(); ?></h3>
2 V4 e# B' p+ S! @ <div class="notice-content"><?php the_content(); ?></div>( I: [3 e2 _7 f* x8 C7 U
</div>
6 M$ z! n4 A9 D5 G) E! | <?php endwhile;% O, F/ l" x, e) S
wp_reset_postdata();
1 H9 {5 a. K; \5 t( W endif; v' K7 J; f" O9 m2 Y
```4 u- T. i6 A, C- O0 U
) d5 w( v# W! g* j' N2 F& h0 q& Q 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|