|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
/ T$ F$ `2 F# u0 M1 o! b4 b$ _- J% d9 k/ b s+ Q- \% i
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。0 R* q$ S2 l% N) }; R/ `
7 ]: @; @7 r$ y0 T以下是创建自定义插件的步骤:. x/ y7 G% K4 W. N# V
4 s" L. c c3 w! y, m& z1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
. {9 E4 c! M. `4 X; ?/ o; E' T9 X$ b+ m1 t5 ^
```8 A4 y. W4 o; `" O& Y
<?php
: g% a6 \) d; l$ n8 [. z9 B /* E% J, f5 I3 ?- P% G0 }
Plugin Name: Site Wide Notices Plugin
$ s0 ]/ R; ], a! |& K, N" D- v, U Description: Adds a new custom post type for site-wide notices.
5 K9 Q/ J( j( q; R2 r; y* H Version: 1.0
" P3 h7 K; d; F T Author: Your Name" c. A/ o2 E5 O- }- N
Author URI: http://example.com
0 U0 I) \# m2 ^3 P ] */
$ A" E$ w& M4 m0 [( c: u. h
. M4 a: u) W, J% |3 B* C // Add plugin code here...8 l2 ` T( S* h8 m$ O- a
```: N" i& D- P: ?. L3 `0 h
& r& S" x( S: z: M4 [ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。! \9 N' R. Z" \& e6 P9 o4 W* h
/ x: L2 y' |/ ^9 P, e6 b% J
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:" m: d, @4 v6 r/ b: s
! Z+ n U" H9 F# r3 }9 y' ^% X+ u6 l V
```' u1 ^9 T }, [' O8 f
add_action('init', 'create_custom_post_type');% q4 M- ]9 h$ m5 \0 s2 W
function create_custom_post_type() {, y+ A; V4 S1 d) N% G
$labels = array(
# n, c, @; I+ K! B2 Q; ~* o 'name' => 'Site Wide Notices',/ Y6 ^ b. ^5 _, `- W+ w
'singular_name' => 'Site Wide Notice',
1 P0 }" b7 G0 z9 i) Y6 M) H9 l 'add_new' => 'Add New',
! t% I! Q: }: s% B1 w- C 'add_new_item' => 'Add New Site Wide Notice',
& [3 H( a) U h8 E% S9 L7 s2 y 'edit_item' => 'Edit Site Wide Notice',, F( m: E6 ]7 x7 o" k X
'new_item' => 'New Site Wide Notice',
7 D" D1 M/ z$ @% v; ~8 U 'view_item' => 'View Site Wide Notice',
4 a/ U5 M9 |& k4 u; z7 \) f7 F 'search_items' => 'Search Site Wide Notices',' h4 v; [: s! u
'not_found' => 'No site-wide notices found',# \/ F* p3 }, ]/ Z& u0 J& D8 G
'not_found_in_trash' => 'No site-wide notices found in trash'6 X2 a5 o; _+ R" R% V3 r) m
);( `0 p- `8 v9 T7 A- `
; c/ L: |% c+ I( n0 k! G/ ]6 T $args = array(, w6 [/ T# Z, v9 f; {* T
'labels' => $labels,
% i. U( l: W* F& J4 e& c4 A 'public' => true,
* b/ q8 m1 Y0 E0 Q 'has_archive' => true,9 V3 Q0 g' }+ ]" x
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),- F4 D9 H: F; r2 Q7 E
'taxonomies' => array('category', 'post_tag'),1 y' L7 V( s/ c) C2 C6 `- T
'menu_icon' => 'dashicons-megaphone',6 t* ~0 N3 j. \: K
'menu_position' => 5,5 s3 z) P D4 p0 `
'rewrite' => array('slug' => 'site-wide-notices')+ u( \5 b4 p7 ?
);
; i1 W- O1 k0 G% \' t
/ l- L. a+ J- i/ b( P* I# q register_post_type('site-wide-notices', $args);# j' ~$ U' i, b8 A! C9 c4 B/ o1 L' Y0 X
}
7 L; A- P1 W" [& _ ```
5 A, u6 m! x( W: W3 z, d
- F/ x( j# l4 k$ M( R0 J 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。3 b& k" R. M: X t1 ~$ x0 Z% q
0 Z$ s7 Y8 K# Y0 }( F3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:: @5 t0 V a8 F& L- ~; C
0 X! B. X% a/ K7 l$ V8 ]* C& f ```
9 t$ k5 Z; @5 f( I7 d add_action('add_meta_boxes', 'add_site_wide_notices_boxes');1 i# }. W1 `; b p/ I
function add_site_wide_notices_boxes() {
7 K+ |6 G" f* ^& E& M1 H8 P add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');+ ]" D* ~+ A$ p! l# I# U( M7 L
}
3 a8 s Q/ L- W6 D
4 y/ |3 g3 y" J function notice_details_meta_box($post) {4 }9 |. h* ` h* R* ?5 k
wp_nonce_field(basename(__FILE__), 'notices_nonce');
9 `8 q; O$ x* X/ f6 p% c $notice_title = get_post_meta($post->ID, 'notice_title', true);9 N( k% q4 L- t$ I2 w2 I( K9 [: p+ O0 F
$notice_content = get_post_meta($post->ID, 'notice_content', true);
4 ~& E2 o4 P; b: Y: q( E ?>" v7 {" Q' C" K! J0 E# f) ~
<p>) z* H& ^" ^7 V$ i) w% W5 `1 U
<label for="notice-title">Notice Title</label><br>
0 V) K% \% ?+ d' u9 Z# p <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 ^6 u1 R" e0 K* u$ t! _
</p>
r% \* N* d* R. ]' { <p>$ q- k* M1 Y" o8 N2 t
<label for="notice-content">Notice Content</label><br>
7 Q$ a. e7 q) S; c <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! C! I1 @3 Q& H0 ^
</p>
% ^! n- [. s$ [0 y5 p4 N <?php
6 ~3 r5 d( @2 V }' A- v. w+ x; Q; o( B! I( f
3 u( B1 J, q( F# R! d
add_action('save_post', 'save_site_wide_notice_meta_box');
4 p1 O0 O5 ^9 d Q ~8 E; a function save_site_wide_notice_meta_box($post_id) {. P+ }- m: _& C$ d
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))), C* S6 d: b. P/ U( k
return;
; M9 b M8 n" ]( S6 @, m if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)9 q5 Z1 D) n5 W W
return;
8 ^* m: Q1 B0 I3 K" w1 i& |# ?; C/ M! X
0 X* n: |. w7 W1 I/ y% A if (isset($_POST['notice_title'])) {* L1 g. N1 b( |
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));6 H( D$ n: X3 M+ [8 u& U. d9 k; w
}
, _+ n4 \) @: K8 {- L4 l: S if (isset($_POST['notice_content'])) {+ S7 {: v |7 _: ^
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
- J% ]7 o4 p( Q! y- l" @- C }- G: o+ m Y& q3 X1 Z
}' Q# ]+ [! z; V' Y, S$ | D* p
```
/ D( v7 |6 E% {2 {
. L, X: a: m" Y& S2 u 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
: F) U% X4 I+ M" A& v6 s
$ r! f0 L* A6 p4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% @' ?/ f; }; q# n2 Q
# I1 F; q1 g4 @( h ```
* X& h$ M' O7 m: J$ U) l9 m $args = array(
$ x' C( O1 I4 m c: G 'post_type' => 'site-wide-notices',
5 V9 ^9 \/ {( Y* l) N* `3 e 'posts_per_page' => 3,3 _; k3 c3 _. h8 d, F
'order' => 'DESC',
4 W+ Q. e) b8 R 'orderby' => 'date'8 [9 _' ^% e/ G3 w
); J& M' p+ U& f6 X+ O" M1 n4 {& [ [
$query = new WP_Query($args);
5 B1 `/ x \! t6 f" y( g0 V if ($query->have_posts()) :" r% d# R' X% N' F. _2 R& w1 k* d
while ($query->have_posts()) : $query->the_post(); ?>4 e4 O. b) v) E" B
<div class="notice">( M3 U! ^. g i D1 U' r
<h3><?php the_title(); ?></h3>
. {9 M7 s# Z) p$ O1 o/ D <div class="notice-content"><?php the_content(); ?></div>
7 _) K3 F; r) ?- z3 Z% X </div>8 O8 ?( ? h q. M z! t. ~- ?2 ~
<?php endwhile;: P9 [% z+ u; R2 W# M3 R$ {0 n
wp_reset_postdata();
D% w5 r$ O M2 X6 u8 {. P4 c endif;
; w: h( ^. ]% h ```
9 \- K+ t5 ^3 I w8 z! L5 t2 I) e& @
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|