|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?6 x2 H! ~5 D* L) k2 w) O
( G" G$ ^7 K- o2 G4 U5 A, Y如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。, B. E' o+ t# r& c
: W: t- T9 T0 @0 l
以下是创建自定义插件的步骤:5 Z" r" a" }/ S7 w8 e
) P- Y, }) V0 p2 X
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
4 y. g8 u( [0 ~4 B. S @0 A1 W. `( s( f
```
% Y1 g, P) B1 A4 q4 Q5 J( Y* K. u4 C <?php. _; p8 Q0 O6 W( V
/*
: j) F( R1 H& }( w0 _- e8 \0 ~ Plugin Name: Site Wide Notices Plugin
1 B5 u2 Q _6 K* N6 Y5 L Description: Adds a new custom post type for site-wide notices.7 j5 {1 _# w$ S+ g. c; b; n- e& b' a
Version: 1.0
7 B1 o$ _9 }0 w8 r* A _ Author: Your Name
0 J, G% B6 q: X f& H8 l/ { Author URI: http://example.com
" P3 j( R+ x7 F& J, P */
" z! W+ h& o+ E7 U7 e
z6 L+ w- P9 J+ R) `" z3 _! o$ w // Add plugin code here...7 F- q- Z+ M6 N% U( g' v
```5 o0 i8 s& E6 f
0 w5 v; s' f% u 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。( y" X% `. m; q/ d
, y& \( q$ {% u4 N3 H, |: O/ @2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
' z) ]2 @$ u% P8 k7 c
6 s C2 C2 ]# ^! h0 a8 u ```
' d. A, |8 N9 O; Z: H add_action('init', 'create_custom_post_type');
* u9 W& ^" @7 B* c% j# d8 F) F function create_custom_post_type() {
' H: j" x: ^/ Q: y+ @4 h9 C $labels = array($ Q9 V6 U& W3 C' Y1 r( a" X
'name' => 'Site Wide Notices',
0 o! k7 N" T2 \ 'singular_name' => 'Site Wide Notice',
9 G1 i- a" r' t G 'add_new' => 'Add New',0 k0 n" b2 T* j
'add_new_item' => 'Add New Site Wide Notice',* g# l+ u* Q8 X, J' y8 [9 }
'edit_item' => 'Edit Site Wide Notice',
9 A, R8 n3 B" V7 H7 M% v9 t, F 'new_item' => 'New Site Wide Notice',
4 q, ^2 ]( o E8 { 'view_item' => 'View Site Wide Notice',. a8 @5 s Q, q1 g5 g
'search_items' => 'Search Site Wide Notices',
: K5 u8 X8 H$ l- S( e! b. D- U& Z7 m( i% l 'not_found' => 'No site-wide notices found',
: L0 P" i& B3 V" r 'not_found_in_trash' => 'No site-wide notices found in trash'
" P- U3 f1 k7 F7 K# t7 o2 i" ?7 r7 W/ i );
$ Z; B* C7 @ P* z/ q
. H1 f$ O9 P. W* A( c# I- q $args = array(
1 v3 E- c& U/ }9 r$ O9 l# l% z 'labels' => $labels,$ K1 O! x, {$ ^, P
'public' => true,8 p, U' R9 M ^9 M" l- {+ }
'has_archive' => true,; F, Z2 M& n2 E* T. |' S* q4 x
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
) E. |) e. D: o$ X 'taxonomies' => array('category', 'post_tag'), X6 M) e& b% ~7 |
'menu_icon' => 'dashicons-megaphone', x# b) W0 [: ^, N5 |
'menu_position' => 5,2 `" d+ i# a: d s X: X" V
'rewrite' => array('slug' => 'site-wide-notices')2 Y( \+ ^, ^6 c$ A' G( }% V. l
);
) ~3 I: V4 G! l& y. a! `. U' d2 O6 S
register_post_type('site-wide-notices', $args);& |6 C& w, }8 H9 S! Y
}
( E& D# `/ L( J* Q% i: M. ~ ```
3 k# G8 e- G, W: R6 L& E
9 _5 U& ]( k' q1 L 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
- H1 ~4 `9 {+ F1 b9 K& |
1 @/ k9 @4 O6 m$ z' s3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
0 G7 K7 h/ Y' I3 ?0 @& C
5 [! f O: V4 e6 T; p; e" G# I ```& `: u0 D* d2 L$ H& |
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');3 v& O! J6 z& ~- E8 p- A) p
function add_site_wide_notices_boxes() {
% {- x, r) u! {. k x add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');4 M6 w2 [8 `4 W8 m ?$ a
}: F: n8 W/ Z/ D3 h5 [5 V: G
+ Q% b1 w: L- A8 [ \* M: i# `8 J function notice_details_meta_box($post) {- K( Q2 ~5 `$ n* `+ }
wp_nonce_field(basename(__FILE__), 'notices_nonce');
) `6 n: J9 ~* H3 n: E( g# I $notice_title = get_post_meta($post->ID, 'notice_title', true); Z9 F* B: L6 v; c
$notice_content = get_post_meta($post->ID, 'notice_content', true);
/ {' G' h, O$ w; h ?>7 l' S5 N% P% _5 H5 d
<p>3 L: m/ v! o$ q) ~5 H; L. z
<label for="notice-title">Notice Title</label><br>
8 N( J9 k; n( t <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
0 J J( F. d- @: F+ C </p>
( ?: Z4 P) e1 S' {, A8 @9 m# G3 n+ d' j <p>
6 S. X* ~# f$ b3 v6 x <label for="notice-content">Notice Content</label><br>3 g" v# V+ Y; T" l$ }% \
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
0 f1 F, ^9 I: v7 p6 E2 l* r </p>7 Y; I, B# u7 v( q) Z, ^: Z
<?php. t, ]6 W- s+ S& F" Y/ y6 M6 L
}
; t Q& \& l3 I
! t: A+ T2 i5 R* t N5 @ add_action('save_post', 'save_site_wide_notice_meta_box');1 [& \ l& I6 U; k/ |6 W3 j
function save_site_wide_notice_meta_box($post_id) {* Q, z1 W* w+ ~/ t
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
0 u) `1 P# E$ z5 G/ @0 H6 h return;
3 q8 a( r4 g( P. r2 R3 s if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
* ]& o& h+ U0 o/ O+ } return;
7 P5 h# H4 P0 A
$ u' b0 Q: n, A4 d, a7 ] if (isset($_POST['notice_title'])) {
2 q) k3 o o; k9 z7 A# H update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));- F9 b- s3 H- V& k% o4 J
}
: W% A" E2 `6 u* ` if (isset($_POST['notice_content'])) {
- N/ C5 B; ], _ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
( Y) k4 ]- O( n }
" s5 A: Q5 y4 K% v4 x }4 W4 w) ~8 Z: k6 n' Z- H
```4 Q9 u/ \) N2 a8 v% T: ^ c
! }3 D3 Z! N* W9 y6 ^6 v 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。( l5 ^ D/ W0 ~9 k' v5 q
& g% u3 T+ J) T" g/ [7 N4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 ]0 l- {* F1 n! ~+ P" F
, @+ m* n/ U8 M3 B- f( y D/ N# t ```1 X/ Q9 U4 h6 }2 p6 v5 U) }( P
$args = array(
" `" e s# S# h' t) | s3 z 'post_type' => 'site-wide-notices',
1 e$ y7 z/ D/ u3 L: T 'posts_per_page' => 3,% l1 ]5 V9 g1 L: w6 i
'order' => 'DESC',
9 p4 {) Z2 u" B6 g, w1 ? 'orderby' => 'date'+ T) i5 i( Q% s& y1 G8 O& I4 d
);
- C) l! q2 J2 J% m $query = new WP_Query($args);
; s1 Q4 d# \* L; z# a if ($query->have_posts()) :9 a! z S* i- h8 h d, j" r
while ($query->have_posts()) : $query->the_post(); ?>4 y2 O6 x( {4 ]0 z8 z! m( I3 M
<div class="notice">
# ? ], l( s) s2 Y& J; O <h3><?php the_title(); ?></h3>
3 e: \! _$ @4 H! o' T" C <div class="notice-content"><?php the_content(); ?></div>
- {# A5 a, f* H- }" s+ C. x </div>3 C3 @3 `/ `& w& Q
<?php endwhile;
" M" y; R: a5 E" l wp_reset_postdata();( ?+ e1 f% n" W4 \0 a
endif;
& C' w: l4 y' \/ F4 O$ s ```: N$ m. i5 R: B+ G3 }4 t; S& ]
/ ^6 w! s7 y, A: A8 X
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|