|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
9 k$ g4 D% s+ T7 T" V. h; ~2 G7 s" {8 R# g; I
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
+ V* \) J# i5 p; V1 V# {
* i+ ]4 h; e5 h( B5 L4 o以下是创建自定义插件的步骤:
) t& Q6 J+ \7 N7 v8 p: l6 u+ b$ }1 d' Y- |
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:% @) Q+ O1 o/ T3 Z0 w
, v% h* M; W: s( A+ P+ m ```7 p- |, ]2 {9 a' [( T
<?php, r8 S6 F0 U! b+ U
/*' Z* \# X- c( d3 A
Plugin Name: Site Wide Notices Plugin
8 l2 Z! N2 l! R0 U4 U& ] G5 s8 R Description: Adds a new custom post type for site-wide notices.
. ~) l$ Z ?1 m3 ^( {# n0 I Version: 1.0
; i) C$ q( x- z6 ^; Z" I Author: Your Name
( N/ N/ T. U) ]8 E& g- X Author URI: http://example.com
, t# n2 C- @5 n6 q */' e3 U/ x! X7 l5 J8 o2 r
, @ W* \9 l# m8 g // Add plugin code here...3 Y5 n" o* T' K" R. Q' U8 O8 L
```
4 T* \9 G2 b# G4 v+ `) L+ n: z4 f
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
+ f- W f D' P5 q# Q" e+ B$ }* r3 N& b; X, A* I
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:5 |4 x4 c) B3 s* C3 U( v
6 K6 D" ]* Y: ?" k3 P p8 w
```. f) G! T( Y/ q
add_action('init', 'create_custom_post_type');
* n" ~! a$ d) Y' x function create_custom_post_type() {
6 p3 d r$ k l0 M6 w$ x' C $labels = array(
% b6 R! ]% @/ w7 `3 z) I' A: l& ]; L 'name' => 'Site Wide Notices',
% h+ c9 s0 `% o 'singular_name' => 'Site Wide Notice',' I6 N: Z: r7 s i- w, n
'add_new' => 'Add New',
0 d/ o6 } K& i9 r% U& H( Z$ `3 W 'add_new_item' => 'Add New Site Wide Notice',$ j! \# h$ w$ E7 b; b
'edit_item' => 'Edit Site Wide Notice',! M: [! G' O9 I; R. @
'new_item' => 'New Site Wide Notice',
. a$ H5 l1 T+ z! u: i- |# @& E 'view_item' => 'View Site Wide Notice',3 l' z+ B5 \# N+ a
'search_items' => 'Search Site Wide Notices',/ e! l; Q' K. |
'not_found' => 'No site-wide notices found',
0 _" f x/ b& J$ s 'not_found_in_trash' => 'No site-wide notices found in trash'
; O' W. O' ~1 s( A# f );5 ?' G) g+ u5 e+ z. f
4 V, a+ W* c, |% Y% v
$args = array(
2 [- ~* q/ L$ @, _5 V 'labels' => $labels,; s' D: e3 r& H- t6 \
'public' => true,$ a' R0 d5 }/ ]+ @; D' c X: ? a: S4 K
'has_archive' => true,$ w9 h- {* D. a) o
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),9 C% U% s$ E) M9 `! v% p3 l
'taxonomies' => array('category', 'post_tag'),
+ M' Z$ Z! G* u1 F, l. E. C# r 'menu_icon' => 'dashicons-megaphone',
1 l( P' n0 P, {; Z/ e9 o7 I! B 'menu_position' => 5,/ U V% J* m5 m3 o) e% G9 c
'rewrite' => array('slug' => 'site-wide-notices')2 M: d4 x# R# p, J
);. ]! W* G, V3 Q5 ]( P1 _4 d
7 R% M' N$ P; u# M
register_post_type('site-wide-notices', $args);3 U' z- G' j! I+ g7 l5 ]: q
}3 R% c; L7 Q6 Q1 q: m. r; f
```
: d( M% N: \# m! i
' Q1 d3 i( p$ j3 @$ u5 i 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。& i' g ^! E: ^: }4 e8 j7 X
Q- H0 I$ ` Q* G* I3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
( l% _; E8 F9 L, ~7 n/ T1 D* j2 w3 `) @6 Z9 S
```
% p' Y/ ?! H8 w5 C; n# \4 n add_action('add_meta_boxes', 'add_site_wide_notices_boxes');- ]! H2 s. L" |7 S2 v k, X
function add_site_wide_notices_boxes() {( f, ^ L6 a3 g8 E* k5 ?
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');. Q, {3 r0 K2 o; o
}
$ I- I) c: N) I9 p6 U4 _
% r% ]- i$ G, s$ p' Y& s: Y' O/ D function notice_details_meta_box($post) {
" |" h, r( `! M' E) o wp_nonce_field(basename(__FILE__), 'notices_nonce');
& F0 Z) A5 s2 h) l$ z! _( O $notice_title = get_post_meta($post->ID, 'notice_title', true);
0 l: M a) I' d8 ? $notice_content = get_post_meta($post->ID, 'notice_content', true);
. k. y- W! Z8 O0 d1 l ?>$ n O' e4 `+ _4 \4 s4 [1 b
<p>' {) L4 ~* R+ }3 z1 k
<label for="notice-title">Notice Title</label><br>
" J0 M0 E5 e: a k3 V* f& r <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
5 u, @8 M. t( x& ` </p>5 j, l* K% L9 P1 X! b+ `
<p>
( I2 Z, v/ f- R) p <label for="notice-content">Notice Content</label><br>6 R; ?6 G K" G
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
5 j8 v4 U+ }# B9 W" _ </p>
1 E( E7 J J6 d( [# z& M <?php3 R4 f- p: `7 j5 X5 K" D' T; `
} n, U! O' C" V. E* \" ]6 {5 V
- H3 `2 B4 Y: j* n* U: ]! e
add_action('save_post', 'save_site_wide_notice_meta_box');
. N4 k2 t$ A. }) d9 | function save_site_wide_notice_meta_box($post_id) {
) F* n7 r7 |3 p+ v if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
" ^' A* x+ C9 W2 \ h* G0 T a4 `* k return;
7 m# {9 N( X3 P+ l if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)- T9 t$ d( l+ C5 G' Z# O" a
return;2 i/ Y- J5 B: m3 V5 x( h
) o- v& K: u5 d' G L9 ^
if (isset($_POST['notice_title'])) {
+ f% q' L# E% }1 M update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
2 ]) T+ ~0 u( u2 q/ T }9 A( q5 R, k& h8 i% Z# ^
if (isset($_POST['notice_content'])) {* a$ J+ J; v0 K0 c9 L- P
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! U9 @" p! X7 M3 [ }- k+ E9 D9 E8 ^4 l, ?; U! ~
}
& v1 b3 a) L2 U6 q( a) u( ^( I, ^( w ```% n0 A) X, T; T' {2 T' h3 n
7 T0 k, o% F/ H- h, @
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。4 w3 k/ v- l) I) d8 g9 M
: E6 u" `# _4 s X4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:/ `8 c1 \& H5 s f/ w) ~; Q/ f
: D2 }' t( v8 H1 Y/ J! u5 h
```+ `9 w+ O' S" q* L& H3 C
$args = array(
$ r5 N; _1 t8 j" N8 E7 Q0 L7 ?$ i 'post_type' => 'site-wide-notices'," I4 P* L! B6 M9 l1 f1 m: Y
'posts_per_page' => 3,
; C4 w Y+ c2 a8 B) h' h" ]4 J% o 'order' => 'DESC',$ o2 h6 C1 a, K
'orderby' => 'date'
5 L) K2 w4 }% p# ^: G- U1 a );
6 ^' | G6 F5 L$ J- _ $query = new WP_Query($args);
9 p, e$ A3 W# ?4 J7 b# E: w if ($query->have_posts()) :1 I. t0 x; y9 U# w& h/ G K9 v- a
while ($query->have_posts()) : $query->the_post(); ?>
+ B3 \/ a4 t$ G( J. n* P <div class="notice">- |! O A' U9 j. O- f
<h3><?php the_title(); ?></h3>
3 |7 }1 B" m' E9 N/ Z; \# J% B <div class="notice-content"><?php the_content(); ?></div>+ L y( M/ _: t5 W7 a" _
</div>
& {0 i5 x+ Y* \4 l <?php endwhile;: b b4 E# F' P8 u
wp_reset_postdata();) n: ]: R, n* z0 f; t2 s
endif;
8 y. ~: v; m% @8 l* k9 B; Y) t ```& }$ v! h3 F: p* }
' J" b& b" Q" `" q
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|