|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?2 |# @' K( u+ }. e
- ]: L7 m% E- a$ m8 W1 f- Z: M$ ~. i4 R如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。 _7 C2 h2 P9 u& q9 \
- D- g# L+ a, A! ?3 P
以下是创建自定义插件的步骤:
! Y1 e9 Y+ U6 x/ V2 ^( R/ a# [$ Z) q7 B$ o
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
3 i( E3 p$ W: H2 N7 Z5 y: z( U* B7 S
```) `$ T# E& s( d. J5 \" L
<?php+ I, ^: K5 @) i& z; x b7 k
/*
. B) ~7 H6 ?( Z. l3 ^+ J* r Plugin Name: Site Wide Notices Plugin
1 b/ g$ R" @. ^" v' G Z H; k Description: Adds a new custom post type for site-wide notices.
9 \1 A' F2 z" g5 L+ y- p |1 k Version: 1.09 G8 f8 M& I+ O) @( D
Author: Your Name w7 l5 M; V% K5 c% }
Author URI: http://example.com2 p+ r' A) M$ k6 U& I3 j# _
*/- ?5 `& s8 w; ~- f' @. E
& v* e1 I8 P w8 { B
// Add plugin code here...
6 c) q) B. c5 J8 m1 E3 h( F ```2 Q& Q* W1 |0 Q6 K- S/ a
3 |2 v$ ~8 k9 N- w 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。% }" |4 {! U e! A! Z3 x! T
, X6 c; [, e* @% B! @) O; W2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
G/ E" \( j) n4 t& U5 ?* m6 g
7 `" j# L( P/ p ```; K3 X! I; a( ~5 r! B9 C8 B3 @
add_action('init', 'create_custom_post_type');' U& m: |% l) e; e
function create_custom_post_type() {
" z/ i+ p3 e* B$ o0 F4 S $labels = array(
3 }2 b* a# B, y( B0 g 'name' => 'Site Wide Notices',
! R5 @5 |0 T. n; u! l5 N5 P 'singular_name' => 'Site Wide Notice',3 v: c8 H' D- A7 g# ]
'add_new' => 'Add New',
, ^' W8 s% H+ K( [9 i, L: G 'add_new_item' => 'Add New Site Wide Notice',
( q- M, p9 t7 \. v( E 'edit_item' => 'Edit Site Wide Notice', r6 q, e4 b% _/ L1 m0 k2 w; C
'new_item' => 'New Site Wide Notice',
C7 n1 Y8 T) m2 s$ I* Z5 z 'view_item' => 'View Site Wide Notice',7 c& K0 W6 ]& y8 k0 A' K5 w
'search_items' => 'Search Site Wide Notices'," S% m) p* d$ P3 d2 @0 o
'not_found' => 'No site-wide notices found',' P h* _! @! z3 l% s
'not_found_in_trash' => 'No site-wide notices found in trash'2 m- T2 a# y: b4 t( H
);% n) V. q+ t8 Y8 T+ P# S, O
5 c3 Q7 j% F0 }9 _. X0 M $args = array(# ?. u3 i! \1 }" P1 I8 ]
'labels' => $labels,
3 U4 p8 T0 V3 m7 O( X2 R 'public' => true,2 e. w, d: H" N% }8 A
'has_archive' => true,' z2 c1 @1 g; S/ ~7 X- m0 G
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),0 N% Q, J& o# Z0 \
'taxonomies' => array('category', 'post_tag'),! F0 i) `# A$ n5 z
'menu_icon' => 'dashicons-megaphone',* X, U& ]" b' z6 T [1 I
'menu_position' => 5,
3 e* C- y X. }5 w O& q* g8 | 'rewrite' => array('slug' => 'site-wide-notices')( @8 ^, O o8 y+ s* {
);$ H/ g* X4 S9 t3 c/ V! P4 w
- r" n* o, }; R$ `/ a
register_post_type('site-wide-notices', $args);. g. N* I0 o. L% P7 h% O& W
}: Q \( b, p \. C7 V
```( b% G' s* d# u+ |4 A' L: L
& e# @2 Q$ p P G9 } F 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。0 o6 l7 f/ Q( a5 |/ Z, }/ X
! U8 T4 V4 J/ q2 Q1 c/ @3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
: q" a- p5 m; x6 c# T
% w- W+ p" k) C! k ```5 M" L* y; v% G2 y
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
) ]- l1 B- y" g4 M function add_site_wide_notices_boxes() {
) n; O3 d+ m2 p$ F add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
% _* Q2 ^# S$ p) m: z }( `1 s( e9 p- G1 [( P
* `! @/ H# U# N" x' B# h1 ] function notice_details_meta_box($post) {) i4 `: [/ m! S
wp_nonce_field(basename(__FILE__), 'notices_nonce');
$ Y4 t$ E* _& V d7 y$ g $notice_title = get_post_meta($post->ID, 'notice_title', true);
- K% Y0 K/ o! y$ M; q/ V- D/ c $notice_content = get_post_meta($post->ID, 'notice_content', true);
5 _7 s* `$ C% ?0 f6 m- g0 X ?>' |* X0 ^) r' E0 k, A/ u/ X: ^. }& G
<p>9 \3 `. V0 T3 Y& Y% D( K" C8 z* a' t
<label for="notice-title">Notice Title</label><br>' |: `0 `. e! c# e; W
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">, W8 R; P' l! b
</p>% Y; A: i. O4 _+ R6 `/ Z
<p>
, u6 C4 V+ a( q; A) E0 d2 r+ K- t <label for="notice-content">Notice Content</label><br>
( d" C4 n) c# L/ O+ Q2 \2 e <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>* H' Y: Q8 k; f4 s+ |1 L; a
</p>) E! p7 ^7 d; g2 q
<?php
/ @4 L; a) L8 ]: X) G }! Y* L* p3 @4 {. v( ?/ K, {
; ]) N, Q& ]0 X8 ~* _9 U# P- V1 Y add_action('save_post', 'save_site_wide_notice_meta_box');/ S* [8 [0 M; t# L3 s3 j
function save_site_wide_notice_meta_box($post_id) {
, Q7 D8 t0 F1 w4 z$ _" y if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
3 G+ ~. _; n6 N8 \* j return;
9 B8 J$ r: N. z0 T if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
( n4 W! k$ C8 V6 l return;
, \5 n3 K" `( i# Q6 C; n# \: x
, T2 U" S) P3 k if (isset($_POST['notice_title'])) {: ^! T' Q, E$ y- X
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
3 ]4 q$ n- c+ D; Q6 c }2 Z$ M4 C' h$ t( p
if (isset($_POST['notice_content'])) {
8 q B! h3 ~# {; `3 b' J( D update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));+ [, Q: n! o- {9 F, n7 \
}) h; R- A0 p+ q# |2 q' U3 W
}! D7 M" C! r, F; Q6 H
```
( s) z/ k4 F2 B' a- m; q" O* l4 J6 W3 M- @$ h& G% h
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
# Z9 W/ E3 F2 m. a2 m3 V7 |+ O2 S F% z- V
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
) w1 f0 U4 `8 V" O* ~. M+ }% M1 P4 l {+ ~3 l
```
8 n: x& N6 G+ c' G6 x $args = array(2 m5 D I+ s( W% D7 L9 }3 d7 v
'post_type' => 'site-wide-notices',
' R- C+ N- {$ H: W+ V0 V$ C 'posts_per_page' => 3,
% C! I1 y: f9 s# w; T 'order' => 'DESC',$ `, K7 X4 L5 s6 r
'orderby' => 'date'8 p, t& f$ J0 C+ G# l% y8 u4 u" m
);
. x3 q- C5 ~! |* r6 a) F $query = new WP_Query($args);
* A* q) f! ]( _) z F; B if ($query->have_posts()) :
- S; u3 D& f& Y# j4 [ while ($query->have_posts()) : $query->the_post(); ?>
3 C3 [! K. O! D' T: J <div class="notice">% m' Q" T$ U, I, s1 E) J* l
<h3><?php the_title(); ?></h3>% n7 _/ |+ I8 J" J/ M z* I
<div class="notice-content"><?php the_content(); ?></div>: \ b) E2 z% `0 N- \& D
</div>3 L+ n3 y7 M' H7 U
<?php endwhile;
5 \7 j& ?5 N- l wp_reset_postdata();" j& ~) i% r$ \; _7 W) A; E) A; u
endif;: k; W S! g- _7 R8 M
```# t% }) x; `- R6 D
3 B6 ^# K3 z- _1 \ ~9 i
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|