|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?. e6 e2 x0 s) q( [1 H
6 l( w, ]2 U4 U) v0 U
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
( c; f9 N* y2 t0 H3 B
9 v# T* m! [3 [8 \& I. c6 H以下是创建自定义插件的步骤:
' l3 |7 ~1 O% n9 o1 P( U
$ M' x' G; N' s+ ~* R# n$ b1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:) O- L0 E8 O+ t2 Q% k* @6 c
. `$ f' @% q5 }6 Z( z7 P6 H/ J( N
```4 P' Z, w+ H" ?2 h6 i
<?php- |5 h+ |5 `% x; U
/*
$ I9 Q3 {; \6 Z0 p1 B! p Plugin Name: Site Wide Notices Plugin6 ^8 A& V/ {% y. W! u5 }! D# V
Description: Adds a new custom post type for site-wide notices.
2 V. y$ l) c6 E# L Version: 1.0
' N1 n: y& w/ m' h( n* m Author: Your Name, ~2 `+ Q$ m% E. m2 U& d
Author URI: http://example.com
1 r* _3 {0 z2 l0 g */
$ E: W( Q6 U( v0 _* k _/ ^# M, B0 @3 \0 c& s$ t, ? V! P
// Add plugin code here...
" s2 G4 U1 n1 E6 Z- t3 R ```- D5 T: I8 ]+ o7 W
1 q, r: L3 J! T: b$ f$ v$ x 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
+ e) t, h3 o, z. c! E1 ^. q- ^8 {8 h" j! x, H
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
3 l/ |- H5 S2 l- s, a9 W1 u( _: F
```! N# v( Z0 V3 W) o
add_action('init', 'create_custom_post_type');
3 J5 I6 ?/ f6 \+ q9 N# s( l function create_custom_post_type() {
. u0 q# J+ |7 b4 ^7 U- J $labels = array(- L8 G1 J9 H0 E
'name' => 'Site Wide Notices',* D e% U) C8 [/ u/ y( n3 R! Y3 Y; v: J
'singular_name' => 'Site Wide Notice',: e* v+ h' R7 c$ F
'add_new' => 'Add New',, Y2 B. O$ D: T t
'add_new_item' => 'Add New Site Wide Notice',$ c# N5 y* S) ^8 o2 ^+ z t: `( y2 |
'edit_item' => 'Edit Site Wide Notice',
" R0 W9 |/ a3 \* T2 j 'new_item' => 'New Site Wide Notice',; }3 F0 P" ~' v1 f2 g
'view_item' => 'View Site Wide Notice',
; K6 w8 F' K/ T# `0 q 'search_items' => 'Search Site Wide Notices',
( L4 K7 O8 ?( F% N 'not_found' => 'No site-wide notices found',& `) R5 ~' z- h2 Q0 f6 i
'not_found_in_trash' => 'No site-wide notices found in trash'8 q" o6 b4 y1 [
);% b; J* S: z- g+ A& U0 k
" f' z% I. O. D+ f1 M5 h5 e- K% C $args = array(. |) t# `2 g' J$ q Y3 G' U9 r
'labels' => $labels,( Z" ?! t( n! M9 l3 B
'public' => true,
; _- I6 A: ~$ v2 ] y6 l 'has_archive' => true,# `9 T7 [" Q+ H0 i
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 {/ q4 N, R1 T/ e& w
'taxonomies' => array('category', 'post_tag'),* S) u* |1 C) ~
'menu_icon' => 'dashicons-megaphone',7 q- ~% w: Z( B) H6 T
'menu_position' => 5,2 ]/ Y0 j3 }2 u i% s
'rewrite' => array('slug' => 'site-wide-notices')
* `6 N$ T1 z/ R; j; o3 h );+ }) M! V* o7 p t2 F0 e: X- n& x) K
- O( j* ?. b. q8 }* b
register_post_type('site-wide-notices', $args);$ Y% z3 o6 `9 |- q2 B
}& H# F8 v2 e, C2 Q) k, D, y
```+ ^& m1 O! i* r' ?$ B3 E
' ~* a1 M$ e5 M
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
; K# L/ R0 H2 \- B
; r& e$ Q7 {( h& g9 E3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:& A; L. E% r+ B& T( m' c5 }" R
: Y5 K! @# S I+ d! F% {' ^, p
```
- N& b: G U9 x! C add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
* v" h/ S/ E) r; P5 D' m* Z function add_site_wide_notices_boxes() {
1 k) l& \0 v+ a* ? add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');- s% V6 R, n. W( A$ E
}
9 T& H$ p2 Z0 k- e( r* f' h. [
+ c e9 n" G$ E6 C6 K function notice_details_meta_box($post) {
+ u; \, j7 E7 [' f- s$ g wp_nonce_field(basename(__FILE__), 'notices_nonce');
; l5 M* I( M. D1 O6 S/ \; T3 Z Q ` $notice_title = get_post_meta($post->ID, 'notice_title', true);
4 H. |9 M: T: s, B4 |" S1 A $notice_content = get_post_meta($post->ID, 'notice_content', true);
0 |" n3 `) e0 w3 j, v ?>9 _" K7 `( ?2 u5 g8 t$ r8 _
<p>% r. n6 v3 j" `0 r
<label for="notice-title">Notice Title</label><br>8 U! o) |/ W% Y/ [6 b3 a+ f
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
6 C8 i5 y3 H9 d. l2 ?8 N </p>
, j. v, l! g5 E: r$ q( j <p>
8 I& Q) F/ |% V% C/ J7 D) E <label for="notice-content">Notice Content</label><br>) w3 i) Q$ g s0 J* Z
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
* @2 x, Y- `1 o; L8 U) I </p>4 z& K& e y' ^; w& D) t j/ a
<?php3 a4 h \3 p4 H3 Z7 n
}
9 J( b- p" w% F ?- }) W7 t z9 y7 A3 o' g+ o+ Z2 n
add_action('save_post', 'save_site_wide_notice_meta_box');
( M P2 r' H3 o7 s/ E1 l. ] function save_site_wide_notice_meta_box($post_id) {# i5 n4 J. c1 [/ p; O
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))+ R/ L% {" n; @
return;$ R$ [4 K8 Y7 s W( \
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
9 b% B# D0 }+ y, D! ]$ T return;8 W, E5 ^: }" S- C( O" r1 E
6 H, `( {" W# S& l) f) j if (isset($_POST['notice_title'])) {
) N7 ?4 {- [# D9 E update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
1 C- ]* y4 }4 z2 w- _$ g }
: x- A, U% t4 m7 Z if (isset($_POST['notice_content'])) {& s$ P( S J! q/ w, l# |6 h
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));" T& l( ?" `' m0 F" x
}
7 l; O' }! w* d }1 d" q) L, y) G) o9 T5 ]! ?% L' \
```. ~ h1 M+ M& C. d1 }
2 a7 Z+ S! }+ Z k+ U& o
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
}& Q; ?$ {" T/ G
0 G6 w- {9 V; \2 V6 N" x6 V' E4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
& b! \: i' M7 r9 `# I4 B: E7 b* B& Q: u4 h; X0 a
```& T2 [( M* `1 m7 |0 D
$args = array(
. `; w; M- i8 q, m% P; G. | 'post_type' => 'site-wide-notices',1 N2 G( c1 f1 K. `$ ^4 m! E
'posts_per_page' => 3,# v0 a. m. R9 S$ o$ |# j
'order' => 'DESC',' `* D5 a, g& U; \2 F
'orderby' => 'date'
( f0 ]7 l( [, F );1 I- T" M! p) ?. X. t4 i( g# Q7 F
$query = new WP_Query($args);
) k4 ~! g- v; }" O# C, Q& O& L% ]/ _ if ($query->have_posts()) :- z5 X; C9 P. @& E% o
while ($query->have_posts()) : $query->the_post(); ?>6 a- R* l* |. W, ^* g% _& m
<div class="notice">
: l, x9 z& F* w; _$ | <h3><?php the_title(); ?></h3>* [* g$ Q6 r L4 x2 W
<div class="notice-content"><?php the_content(); ?></div>. I, E0 l* E" G+ [- S4 G( |
</div>6 S: h3 G2 f# s! }+ c
<?php endwhile;
. Q+ y% z5 D7 \' B& t wp_reset_postdata();) I: Z3 A, ~) K0 _, U: b- R' L
endif;
8 v d b) H$ K4 U* s1 V ```: y5 E( p: {2 H2 I
. l5 j2 G+ k6 g
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|