|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
, v9 b$ J: [( S. i! g7 l D# U* i* N! z' v* y+ O
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
* p* u& }( b/ C! P# v" r( O& G+ K
5 K5 |9 H" V' s; ~% O7 H以下是创建自定义插件的步骤:
) T, V) |, G2 r* X5 `% T; e8 b( g W D2 v9 [2 g" ^
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, Y _! a4 X% C- g
% ?) }, b" g/ z1 C$ i1 C& y! r
```( a. R5 T5 h0 @ z
<?php
( G+ t6 `& \& M* A0 J1 J s0 U% a* P /*
/ w8 S r+ u0 C' O2 a: u( \ Plugin Name: Site Wide Notices Plugin* v# r& X6 T, b2 B
Description: Adds a new custom post type for site-wide notices.7 D5 R4 v4 }! C* [
Version: 1.0# k" v9 u3 b; h% w' n8 C
Author: Your Name8 M/ w t" u$ @/ I* S* u t: K
Author URI: http://example.com2 o! {0 }# V9 S$ a. k
*/
; C2 q+ O$ z2 w6 F9 ]" v& z5 h5 g5 c0 ~( q
// Add plugin code here...
. @% Z8 M" o$ _3 l ```
5 K6 r# ]+ I" [; t
l% o3 x' f0 X: x, h6 H! T 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
( E2 e4 ?: ] O7 Y/ w5 u: ~$ |% j. [+ j+ n8 J. _
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:( ]/ }/ Z6 n5 E& p, f% a
1 f" y- ~. Z1 Z; H ```5 W/ t9 F3 ~9 S" G
add_action('init', 'create_custom_post_type');
0 Y: K9 m8 a8 k! g- u) Y function create_custom_post_type() {$ p. D( }$ D; ~4 }) B
$labels = array(3 u! \ U; O. m, O" X5 ]( N
'name' => 'Site Wide Notices',
5 T# b$ L6 a5 m+ ^6 ^ 'singular_name' => 'Site Wide Notice',
8 F1 M1 d- U$ I" k( l# g 'add_new' => 'Add New',2 y5 O: J/ `! i$ i7 c# x
'add_new_item' => 'Add New Site Wide Notice',; c$ @/ [; m0 }' f
'edit_item' => 'Edit Site Wide Notice',( B: y7 \( D q/ S, O& G6 N
'new_item' => 'New Site Wide Notice',
! D3 F* D# ^' D- P 'view_item' => 'View Site Wide Notice',
, Y0 r' N6 K* ]; K 'search_items' => 'Search Site Wide Notices',
2 b& u5 S7 l1 b% E0 I$ ?: p+ {$ C 'not_found' => 'No site-wide notices found',
2 o5 d% B+ @" x 'not_found_in_trash' => 'No site-wide notices found in trash'! F {! }6 d; m/ G; g% Q
);$ o2 o: J& T3 y; p# O
1 I) A7 f5 H& p. K $args = array(
8 }1 w+ Y: w, B7 l 'labels' => $labels,5 P8 t/ d4 l, J1 \9 W
'public' => true,
& m4 N& E, _; L 'has_archive' => true,
* W% {, I9 |- ?( b# O; f 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),- R5 A5 V' ? X, i1 N a# `: w
'taxonomies' => array('category', 'post_tag'),( ?' C9 B$ q! V/ ~" s) Y9 F
'menu_icon' => 'dashicons-megaphone',
" G8 q# q. V3 F% p 'menu_position' => 5,# ]0 I" h( Z' Z8 F: r
'rewrite' => array('slug' => 'site-wide-notices')
' @6 B) w4 f# G) D; p4 G7 N" C );+ a+ o/ k7 ^( j
% w8 P+ u, x9 d( S% k5 F: D' R register_post_type('site-wide-notices', $args);8 q5 G- x! k5 C, W
}
5 S# l2 V- M1 z6 ?2 ?2 o& A( f, F ```
, v/ ^0 v/ ?4 `; s! ?) v( N$ d3 d7 c
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, g2 A9 M2 y. `. g9 E( A( {
: V9 g! I1 X4 ]4 J& ~0 v" e: ^
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:# D4 U$ l0 w7 V: J, G$ P. D
; n# A* }& {4 @5 \* o8 T7 q. g, j ```; H) I; A5 D# B) w- R- d
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');, I# l( _+ l4 x- G5 O- W( v& L
function add_site_wide_notices_boxes() {
$ Q' t6 G: u3 j2 P# n, p( E2 f add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
* P% h, L1 [9 o/ t: z4 j f4 D }
9 m6 T! f8 ~- L# O1 E( x: [" T1 O s3 r9 |! a
function notice_details_meta_box($post) {/ v" r( ?6 M7 v1 U d, W! p
wp_nonce_field(basename(__FILE__), 'notices_nonce');" p1 \" t1 Y- K( d T" H' n
$notice_title = get_post_meta($post->ID, 'notice_title', true);
- w5 E9 V4 ~( f1 }4 ] $notice_content = get_post_meta($post->ID, 'notice_content', true);
: I2 h# \2 ?; ^- E/ v2 z ?>
7 N. y4 m9 H$ J <p>
# j& v. l5 C" W <label for="notice-title">Notice Title</label><br>8 J( y5 \, ?+ Z! C3 N
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">' ?. t2 t! [( W3 r4 e
</p>; C4 z( S2 H, o# g: E: a. a
<p>( }0 Y" i, R9 c% u
<label for="notice-content">Notice Content</label><br>& h; Z' c! w5 N" S* U9 n; }
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
+ z& d' C3 U# |; G; o </p>3 N2 c- j+ \5 a
<?php
% y2 {! {) n; Q. E9 t }
" \; g0 V. F" h) f0 I8 O! A* t) o# H0 ]; K6 L5 N r4 N
add_action('save_post', 'save_site_wide_notice_meta_box');, j3 m9 r! E' N. g: L
function save_site_wide_notice_meta_box($post_id) {# M N" I% e6 P
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))( s8 n2 c5 c7 `( i
return;* Q) n* u* i* M+ o0 q6 z) \
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE): [6 E9 d4 l# }$ L' u
return;2 E4 a p4 W( ~4 c( p# b: D
* M5 N8 y z! f6 M1 C( }% ~' R if (isset($_POST['notice_title'])) {
6 _1 F/ x3 M6 U0 H6 ?8 P update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
% A/ y2 }. i. v% A# J }
5 S, _, H+ J4 f9 o if (isset($_POST['notice_content'])) {
: e% I6 Y" H n6 Z, I0 R update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));; z6 q7 c6 E, x; A2 \: m
}
! C4 f0 o. G. i9 S0 b }$ D! A5 T- v/ A+ ~) E' Y
```- l8 k/ Z! S* |, G& ~
) X3 B W" G0 h7 I
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
1 Q+ S. O4 T4 `- S G! c6 h/ @: v/ U- x0 J1 A6 d
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
/ }2 G, B2 ?3 Q! W4 G+ l0 Y! s7 S8 |9 { c' E7 Q$ z
```
# b+ z2 N8 d9 T2 f4 s* l- k $args = array(
5 }& Z- t! O& S% g0 R/ x* O& V5 S 'post_type' => 'site-wide-notices',
- y/ \$ Y( Z$ N, Y3 `9 F; H) K% { 'posts_per_page' => 3,& o- {9 Q& x4 i
'order' => 'DESC',
% q2 c1 B8 B) x4 d: u; m" U8 R 'orderby' => 'date'
: z9 ]3 c4 V l& j: A* w* b& Z1 s );) m' b! ~2 i b1 l
$query = new WP_Query($args);
# k( b( P) p7 x1 ^& ~ if ($query->have_posts()) :* [ k W2 |9 h1 R' p
while ($query->have_posts()) : $query->the_post(); ?> J7 G" k0 f6 A9 Y( b" C) V
<div class="notice">
" u3 R! u7 a+ V0 c( P+ p <h3><?php the_title(); ?></h3>0 P7 e! ~4 I7 |( Y. t! e
<div class="notice-content"><?php the_content(); ?></div>
9 k2 M. {" P0 S </div>
+ L1 K) ^: P" Z3 L' v, x <?php endwhile;
* s7 l$ T! \* G& X: X% ]# B4 E wp_reset_postdata();! Y' d( h' r4 B+ p- j( R! U
endif;) b3 T: P* w+ x! Q* h4 P
```
2 B n4 N/ b6 C! Q
" \( z9 c m: ~& n* j, M 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|