|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
9 q. |- d* [6 B! o1 Y+ q2 v$ P+ L) `9 n% p) X9 T4 I3 G) J- p
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
b9 ?8 F% Z f b/ L. X! l; t- v S& X) F* M* A2 }; Z3 Q
以下是创建自定义插件的步骤:- Z/ x; _# q$ k5 l+ L
8 [2 I# D$ k" y% o
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
" D$ g4 D5 E% {0 Q- s1 P% x6 v/ s$ _4 A
```
8 R$ f& q3 K6 J: g# v <?php
* F8 \5 B6 j2 K6 P2 Q /*
; N! Q( K/ K1 `4 t: q! t: K4 t Plugin Name: Site Wide Notices Plugin
; g4 V S0 W" w& n; ~* K. u5 a Description: Adds a new custom post type for site-wide notices.
! M. \) M6 }9 f E Version: 1.0
- ^! V! y# I6 G3 N Q Author: Your Name3 o4 o. S+ j, J' n- s5 c
Author URI: http://example.com
; b8 [7 v. H/ H; I; M4 W. x7 i" t */
% {7 ~# N: i3 \1 P- ?; T+ h3 a5 [9 K- Y8 |: A6 K
// Add plugin code here..., i5 X6 t5 \$ I9 Y- |! w$ V
```3 z! B8 B2 h8 b" r* R
W# S) ]7 A( \! U5 r/ v
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
$ Z, F3 Y0 S: ?& p9 H* M, H- k3 X0 _3 l2 c
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
% y. X* f9 g2 l0 m. p2 o) l8 ^2 k1 U# b- }& A/ V9 v! _6 y
```" y7 p: k$ l2 _3 U3 H
add_action('init', 'create_custom_post_type');" x9 t, j3 [. b9 |. l: m
function create_custom_post_type() {8 T! a- j% D1 a* E% e' {
$labels = array(
1 t- [! y% k' ]2 Y/ v2 U 'name' => 'Site Wide Notices',
: ]% F( u/ D& f! Y 'singular_name' => 'Site Wide Notice',
7 G Z% y( x! L) f. ^$ M' l 'add_new' => 'Add New',
1 ]' }' I/ }( G. w. q7 s 'add_new_item' => 'Add New Site Wide Notice',
2 i9 H% e% n' B& w% w1 s 'edit_item' => 'Edit Site Wide Notice',
) L$ h' E: U2 m: \ 'new_item' => 'New Site Wide Notice',
8 P2 N! [0 _6 g6 S( x+ A6 } 'view_item' => 'View Site Wide Notice',
9 D/ x8 E/ |" Q+ \ 'search_items' => 'Search Site Wide Notices',
4 I+ }6 n# r' H7 Q' L: Y0 \7 z0 T 'not_found' => 'No site-wide notices found',
* a, f: w8 U/ u& |! G 'not_found_in_trash' => 'No site-wide notices found in trash'- T6 B! d2 E5 O2 \
);
6 z, v7 k6 C- K- E+ U0 s
5 t0 ~ u6 R3 @5 g $args = array(
+ `/ a6 \7 A8 T 'labels' => $labels,- r7 W, b' ?8 x7 p# }! K
'public' => true,
$ L& B9 K8 Q0 W& L1 o" Q8 k9 L 'has_archive' => true,# ]8 t, o2 \, e8 p
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( ?0 F" D0 R% b
'taxonomies' => array('category', 'post_tag'),
0 m% I, }# X8 P4 i: _% E- ]7 v+ O 'menu_icon' => 'dashicons-megaphone',$ o3 J0 H8 k* R( W* O+ o
'menu_position' => 5,' ^4 i- ?0 }# r, g8 S/ n2 Q) J6 b; S
'rewrite' => array('slug' => 'site-wide-notices')
4 M9 r$ ~, n$ i/ A );5 k, S9 [$ y/ `% Q0 K- p u8 G
7 J4 e1 u( u0 N0 v
register_post_type('site-wide-notices', $args);3 F. {! W( q6 t9 {
}
* a0 H' Q. n9 ~ ```, D# E; V, k9 S( R3 d) Z8 n
. @6 `* ?- T% V: k5 S) w. J 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。! S8 B7 H% r- F5 G8 S. a
M) [0 j" Q1 G9 Y, c, r0 j3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如: L6 K7 a2 n( O% P* Z0 V
0 {; X8 `- [+ _( Q0 e7 Y% s- K
```
# V. Q- w) o" ^7 t add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( ^7 i: |8 Z6 k, o* o
function add_site_wide_notices_boxes() {5 K. o9 [7 M8 g( r; @
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& q6 c# w7 o) m. w8 ]. x) U- i
}2 i: {& ~5 F+ l0 [$ u8 H6 U
/ r, M) g/ J, k" A2 j, y- Z Y
function notice_details_meta_box($post) {; l8 r, y0 ]) I
wp_nonce_field(basename(__FILE__), 'notices_nonce');
+ I* U& _, b0 C $notice_title = get_post_meta($post->ID, 'notice_title', true);! D5 o6 W) \$ d9 Q
$notice_content = get_post_meta($post->ID, 'notice_content', true);
5 L% ]- }! C4 P5 z' R! }8 t ?>
I9 p6 [) _3 E9 V <p>
& k2 \4 C, H1 |$ y <label for="notice-title">Notice Title</label><br>
) J9 G5 k! E" d% O% f+ } <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">& ^8 j& N9 D9 N% l+ Y6 Y* V
</p>/ N' i- x& S/ l$ `; p# V
<p>
! v: x# E- ^, h- b% i <label for="notice-content">Notice Content</label><br>
8 }" ~- @: S4 h2 M2 x <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>3 S+ q& E* p A6 v/ x
</p>
& \( {& F4 I0 @ <?php4 a) G2 S: y* L9 y1 M! l
}
- @$ c5 @6 K' b1 D& q. a
E$ j* X5 T6 k+ ~3 P add_action('save_post', 'save_site_wide_notice_meta_box');
6 e3 i- R3 l. Q0 ~2 p a' l. ] function save_site_wide_notice_meta_box($post_id) {
2 }5 M G! g6 f2 \ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))" |" p4 J+ v" `1 f5 @& D0 H/ g1 a
return;8 J. t! ]3 r( x9 y) f& z9 q
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
. k* ]+ k; p) K) W; B return;4 Y$ c( y! r c5 [: h; M' }
3 E" W4 j0 e! n$ X, I9 N
if (isset($_POST['notice_title'])) {
% ]2 Z- d" O' N* {$ q& w! ^ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" I2 y2 j. Y" V" o! `
}
. V h! q% D4 x) }9 H if (isset($_POST['notice_content'])) {
% p8 r$ d: E. j* h* q" ? update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));& ]- M" b+ O# ~0 [3 ]9 L& W
}2 @! H# W7 o0 O: u2 J! h6 |
}
, w9 C, ~0 q3 m( G+ D- R o ```
2 S9 P H! I, _! P% \' }4 y" n
; ~# ^' K4 M) g9 _' ~ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。! S* B0 s* K6 K3 o
+ t$ O9 B) J6 @/ i4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 G: w _5 c( P% I5 p# }1 I) Q
4 W& z( r3 s# f; D. Z2 k2 u ```
* H2 \, a# }9 z& }9 j6 E2 U: p$ T $args = array(
. n1 w. h/ @/ L 'post_type' => 'site-wide-notices',& }: R3 j8 b! D
'posts_per_page' => 3,1 B" t/ s: W y
'order' => 'DESC',
0 H4 z8 Z" x% A; P; ]& j 'orderby' => 'date'
0 t$ E& f' r8 E6 B );$ T0 p- ?: i" h1 i
$query = new WP_Query($args);3 D( X, K! ?. {3 E$ F& d
if ($query->have_posts()) :
9 d2 G: D3 C- | \: s9 ]! }8 x while ($query->have_posts()) : $query->the_post(); ?>4 ?* s' F( e* W. m
<div class="notice">
6 Q/ I& n- e- l* N* B+ n$ H" C <h3><?php the_title(); ?></h3>
- n7 T+ _% F1 ^- D* b' w <div class="notice-content"><?php the_content(); ?></div>
) b' R( S6 g, k K j' b </div>3 n `) r/ p$ |7 z( c' i$ z
<?php endwhile;
2 ?1 J+ P7 o8 y$ I8 n, K# n wp_reset_postdata();7 q# k7 A5 s( Z3 b; j2 E$ S6 j/ ]
endif;8 g, ~: p/ g8 _! v' n8 I6 e
```4 j! c" z, |0 c- O1 a# c9 Y. N
8 ~- t: @# c' ~% }: Z 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|