|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?- ?) g. [; ?$ b2 f- x3 V1 g
+ h2 k+ r( h. g) a3 D如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。) p4 k! I9 @' v
6 V& ]8 t Y( \9 _以下是创建自定义插件的步骤:
1 R. z0 t! F0 ~# y$ R% K) k' o
% [7 P: T+ f1 X) i0 h1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
L8 r9 }) `+ E& N3 ^7 w9 E
, p) }& |% L% s2 K: P! B% M ```
/ L5 {& H) W8 {% C, S% {6 `8 [ <?php
8 L. L9 B4 G7 Q% Z /*
9 \5 _+ u" k- R/ d2 `2 [* s Plugin Name: Site Wide Notices Plugin
/ P, h# n. m% N0 }1 Z- U Description: Adds a new custom post type for site-wide notices.
; z% c3 ^5 y4 O Version: 1.01 n" X% e3 c- R* O: C
Author: Your Name
" Q. z; W) o7 q+ l" f9 d+ T% h Author URI: http://example.com9 M* G2 h8 d- C9 a; u" E
*/
1 j' I9 [& c; V: \: M( t. s. F1 |4 z! e! N! W# J
// Add plugin code here...) m9 f1 n. Z `: p% A' Z
```2 m- O4 ]* E9 t1 m* N
& R4 s' H) o% L0 l, O$ |
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
$ w/ z6 G: V- j9 _; u ~2 U) G$ W6 T0 f. W
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:5 ?" K) g e1 M2 e7 C$ p0 V7 A
) L5 i6 i2 M7 t ```# j6 y2 Z3 f, v
add_action('init', 'create_custom_post_type');
& o: P9 b' m0 n' a, Y3 Y function create_custom_post_type() {, F. a% K4 e D+ G0 D7 \ f
$labels = array(
, F" F- C. @2 W4 Y 'name' => 'Site Wide Notices',
7 }) R/ }' ]- g( W 'singular_name' => 'Site Wide Notice',$ e* [ ]$ |$ Z. V( U9 i8 q
'add_new' => 'Add New',- g1 C, W; M- X: P
'add_new_item' => 'Add New Site Wide Notice',
1 u! ^1 T9 N6 D 'edit_item' => 'Edit Site Wide Notice',8 I: S- N+ z, b; e$ c; j
'new_item' => 'New Site Wide Notice',: } V$ x) R( w/ n7 _8 V' K, u1 q
'view_item' => 'View Site Wide Notice',0 T [3 t$ _6 ?: L0 B" b# A$ P6 H
'search_items' => 'Search Site Wide Notices',+ Q- C( U8 J; `) A' n8 x' o
'not_found' => 'No site-wide notices found',0 n9 V5 Z2 T7 e
'not_found_in_trash' => 'No site-wide notices found in trash'4 R& @+ S$ D7 F+ ^) ^
);
0 g" \& e& V. ]0 c& T4 H2 }
. t4 F, h/ T0 E( { $args = array(
* K( l$ G R! P- y# v0 S 'labels' => $labels,
: ~7 S2 m* |' X. u( `! M8 o; L1 j 'public' => true,
. G6 f8 N) o+ ~2 Z5 Q/ |% h! ] 'has_archive' => true,
4 C! C8 [! T" N( F8 r. S6 k. | 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 F5 p: ]5 B) M
'taxonomies' => array('category', 'post_tag'),$ k" f$ F; } {8 k% P. H5 @5 D
'menu_icon' => 'dashicons-megaphone',
' P8 P# B( Y8 R7 k5 ]* j+ z 'menu_position' => 5,, M) |' N% o, Z9 q( A
'rewrite' => array('slug' => 'site-wide-notices')5 [# H. A! I7 t* u/ h2 F- m
);5 e' ?7 g0 ~, r
4 t) v3 s$ q0 x0 _4 h2 t0 n
register_post_type('site-wide-notices', $args);, C0 L- D! O8 F: d
} [3 F: H5 l0 U' v& `
```
# F2 r! F3 l! b3 k) x) N( R
& F9 v0 u& F$ ~6 _% B 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。( k, g/ o& t& x+ Y4 t5 [
0 L4 H4 C' w9 J0 \3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
( v8 l% Y/ D. T& y7 s- T8 j+ M2 ~5 `9 r
```
8 A* g3 V1 O: b3 E6 m add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
. L0 {7 L$ H3 }5 T5 R7 x function add_site_wide_notices_boxes() {! D% m g% u* A5 k0 ^8 [
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');# ?; v. G) ^% s8 u& J' v$ ?
}0 s2 R) ~' r# p4 r4 u
, k2 P& `9 t" G6 n% N. U
function notice_details_meta_box($post) {
5 C; B: z$ G. _; X/ R wp_nonce_field(basename(__FILE__), 'notices_nonce');7 i( j( G j& r+ d
$notice_title = get_post_meta($post->ID, 'notice_title', true);
f) z5 u! X% D7 r1 i" j $notice_content = get_post_meta($post->ID, 'notice_content', true);
( v$ Z. C N( p1 ^0 C7 z3 I; Q6 v ?>
7 c2 q/ U5 l0 K+ }: _ <p>
9 {% S# q, m. ~6 ^* r <label for="notice-title">Notice Title</label><br>' C- P" A& Q8 i1 Z* T N% k7 a
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
% t3 o3 N+ C" I( t+ Y </p>
8 x+ Y9 _ r" P g" _4 o <p>
. V5 e5 `) o- m" x c <label for="notice-content">Notice Content</label><br>( I! ^+ @# i& F1 c ^1 G
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>1 P' B* v. v3 Y* N$ U* k+ Z2 M
</p>
6 y% e, P2 I! p! w$ R <?php* G+ M) d: [: N. F6 o0 y8 B
}+ m$ O# g0 l X" {" L; b& A+ [
3 L7 S* G* j" E" j
add_action('save_post', 'save_site_wide_notice_meta_box');& C4 [' @0 e4 B* f
function save_site_wide_notice_meta_box($post_id) {) l' q6 z& W* w1 D
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))) o5 A# Y, ?2 g8 l( i. l
return;
8 r5 {9 [% w6 G8 h: x/ ] if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
( _2 P0 q) J8 G4 A j, F/ W' t2 o+ _ return;1 v5 ~0 O# t1 z. C% K6 @- j
, S, H& Z7 U. m8 n \. b" q; {4 ^ if (isset($_POST['notice_title'])) {
5 i+ O& l5 n* e- s) F5 w update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
8 o# y- j- A' L6 x# T( v }
# ~4 j% P4 c* y; G8 T0 `" K5 F if (isset($_POST['notice_content'])) {
$ H _( K* z! {! E( F7 m- b update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
7 U, {7 _- U* ?' k8 S% d }+ K- r0 x) n4 ^
}/ L: ~ Z$ G1 r d
``` c" e1 i! c& q1 ~: Z: ?! {3 f: H
& @, ~* o9 V" ]
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。; |2 ?, l( f- c8 ^
; K2 Z5 j w" Y" H7 `5 y4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
6 C4 K, U: O% N' o9 x0 K! o; S- q. \" x8 q& m+ Q
```
8 H1 x* [, A7 ^! P0 L; i/ j $args = array(1 c0 a! \$ h5 f
'post_type' => 'site-wide-notices',
. S; \0 g. u: x, O# c 'posts_per_page' => 3,) E! P4 k; ?1 w/ d4 P! c! D5 c
'order' => 'DESC',
. S9 a0 b. e3 s4 l+ N 'orderby' => 'date'
( I. _1 l# d' p. @* { );
6 P3 _9 s9 x9 f! E+ p$ I9 H5 _ $query = new WP_Query($args); m9 K' ^3 z# b- x
if ($query->have_posts()) :
0 I$ ?3 V7 U) i# _, p# J while ($query->have_posts()) : $query->the_post(); ?>
$ j6 x6 L, q4 ^7 R' p <div class="notice">
+ S0 r1 o7 z6 a X) @ <h3><?php the_title(); ?></h3>
+ e3 R$ L5 @4 u# O ^7 v4 n <div class="notice-content"><?php the_content(); ?></div>) |8 h" R& M; a- k# J! k6 m
</div>
" [- ^: w2 _: W <?php endwhile;
5 T- v, P+ j- a) P wp_reset_postdata();
W5 Q E) b9 y) A endif;3 H. N( r" O4 H% v
```
/ W. X0 [0 F7 i9 p* L1 X& u* l8 V6 c& E& M) z
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|