|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
) h! T! c! D! d2 o; e2 ~8 J! X' V3 X4 R% |+ x: B6 i _7 \
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
/ ]+ F. e* n2 K* ?: e5 Z& x% M- j" {5 x' ]
以下是创建自定义插件的步骤:( S8 H1 ]% |- p' N: w: s- \% b* t
3 J4 s" q9 r' y4 }! r1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
; U6 a- H6 Z( K* D2 J. J2 \& ~3 Y4 _, W" }2 N4 @, G/ [- y" l' G
```
8 v6 h$ Y% r R2 s" R <?php
' D6 I: m! ~% D. b5 W6 L0 c4 N7 ] /*( M ^6 @: [. _
Plugin Name: Site Wide Notices Plugin
( k- V# ?- z% Y! g0 P7 c Description: Adds a new custom post type for site-wide notices.
. c; P6 l. `, \" h Version: 1.0* |% w# {6 i. Z' m$ k6 j
Author: Your Name" w+ u1 J; s8 K2 q
Author URI: http://example.com7 t* o7 \ V( B+ C) k/ ~$ O4 R( Z
*/
" ~" j: [9 w* Q
& v5 L/ `- r7 X. I // Add plugin code here.... O9 z0 D9 m: R
```& G# _5 Q( ?9 d! j# g# h. u' Z
+ p% K+ v) `' c' P- h: I# ~
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
4 U" h4 c1 r3 S
3 T$ ]( H; k4 s# Z) Z+ I, a5 T2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
: d( E g' M" {4 Y$ ^4 R) |3 D7 c! J" y
```% u6 D5 L8 P/ R) D
add_action('init', 'create_custom_post_type');; Y' s! n; J! x' Q7 D& ?1 o! V* t
function create_custom_post_type() {" b" a/ H: c' }/ I/ t
$labels = array(
( p$ J" Z2 Q, X 'name' => 'Site Wide Notices',, d1 D, i+ e* j8 u. _
'singular_name' => 'Site Wide Notice',
4 I" i1 u/ V) U r# z 'add_new' => 'Add New',
' _: l+ H7 P9 ]# m 'add_new_item' => 'Add New Site Wide Notice',
, g3 v7 d+ P) y) ~% a t5 Z) z 'edit_item' => 'Edit Site Wide Notice',
( n( G% Q4 P3 p! C" ]. \7 m 'new_item' => 'New Site Wide Notice',, F9 f8 D! b2 z, L! R; f
'view_item' => 'View Site Wide Notice',
; I2 f4 {5 L: v- v2 Y8 r$ K# A. e 'search_items' => 'Search Site Wide Notices',
1 }9 Z8 e& G" G+ m* r 'not_found' => 'No site-wide notices found',* [& a5 G$ n8 N2 `! ~
'not_found_in_trash' => 'No site-wide notices found in trash'
7 \* m! t% B3 e* [! l: S, r );
; h* H/ j" K6 y; u7 @" r: O* }* r% {0 C2 q
$args = array(
2 U0 e. t) t/ t- ^3 j 'labels' => $labels,
: b8 V) D5 U7 A 'public' => true,
/ }5 V' P7 {2 [5 G& G, r 'has_archive' => true,' K, B, j- [% H4 s' C5 h
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
F+ m8 `% w8 I9 Z 'taxonomies' => array('category', 'post_tag'),
, S. U/ |' c4 n& E2 y$ u5 L5 C 'menu_icon' => 'dashicons-megaphone',2 z( `* `: y6 _( \7 V6 f
'menu_position' => 5,: m4 t% G4 w, J- [9 J4 S* P3 R
'rewrite' => array('slug' => 'site-wide-notices')/ f! |, d6 D+ r0 Y3 x+ ~
);
* ?) [2 {9 c. v
9 {+ H# g; V) p8 B7 }5 ] register_post_type('site-wide-notices', $args);
2 n# d/ a, i4 E8 H7 b5 [4 ]6 X6 r }' a( t9 q/ ~- W6 C3 S# [0 O
```/ E! @' ~; e- Q. n& _+ \9 {
( h: V9 u7 y, N, J, z- x9 _
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
* x8 u/ f- N# B1 G1 Q( C) V k" ?2 P I! v& @8 l
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:$ b0 X. B. q4 a/ a
4 W) W! x4 N- O2 Z6 h$ K. Q
```
: K; L* K) S/ @+ m0 w% a$ v add_action('add_meta_boxes', 'add_site_wide_notices_boxes'); C' a1 D+ R/ s
function add_site_wide_notices_boxes() {2 H$ ]9 V: K7 F: O
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');( ?/ R! Z4 K& E/ O& S
}5 A# v( W" o1 v. t
% e. }- e) y/ `$ p
function notice_details_meta_box($post) {( `& g" s- d) ], c8 }8 R5 J1 H
wp_nonce_field(basename(__FILE__), 'notices_nonce');
. L& m, T0 r6 _$ U3 k6 h+ J4 E $notice_title = get_post_meta($post->ID, 'notice_title', true);
1 X* }- u) d* m7 a- e% i $notice_content = get_post_meta($post->ID, 'notice_content', true);
/ ^+ S3 u2 Y9 ~: I ?>! P1 [4 X: F/ p& @$ G! j
<p>6 f. B" D j4 m a* x& C
<label for="notice-title">Notice Title</label><br>) R5 Z* O; y& G, ?, \
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
' ]9 M/ |7 E& | </p> H# F/ h9 E: |' z
<p>1 C" @+ r* d/ a
<label for="notice-content">Notice Content</label><br>; L& `5 L6 Z9 p. G! v# p
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?># ^' `& N: I0 ~9 H6 L7 p ?( r5 {, r8 e
</p>
- ] v+ P4 b; P6 [ <?php2 M) }. Q7 E& c( j. ~1 W
}
9 T+ U/ U0 f' c8 c
' r$ m1 b& r$ r; l7 Q7 @, A! v3 W add_action('save_post', 'save_site_wide_notice_meta_box');
6 o7 \2 v$ r7 Q. U; p9 l& L* e function save_site_wide_notice_meta_box($post_id) {$ P1 q: m R$ r# j$ s% k4 r3 J
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
, j5 ]$ v7 E+ `2 ?( g" o return;7 C5 Y+ Z; i% c2 t
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- v }7 B# y& V. h return;
) ]' ]: [: p4 l2 I8 K) H) H+ f0 y0 \7 I4 J
if (isset($_POST['notice_title'])) {0 @8 @% c/ t; q& T& ?9 W: e
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
5 ?! B6 d% }* N) e }
7 ?5 Q# ?9 J! P$ E% [ if (isset($_POST['notice_content'])) {
Q8 ~1 f: M! _0 n update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
4 S7 W- `! S( G. G& Z2 h }; T, r7 ]& T5 K8 P
}. ~: S4 E. D! K* Q6 l+ v
```8 V2 ]) o6 p: A- j1 `: g; }
. d" g% y) E2 f; s1 ?; Q
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。/ s" T4 z4 P& W! I* D x
; q; Y- |( N7 K* v. i
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
- b# M- K& N0 i1 g& T. _
0 c. |9 W: D% ^1 a1 \% k- F ```% L. J/ f/ M. Q2 N X/ o/ G
$args = array(
! Y4 p$ @+ H6 f2 m; }( C( p 'post_type' => 'site-wide-notices',! q0 n$ g+ I z7 @% @3 M, ~% j
'posts_per_page' => 3,
# S! a4 ^( T% U5 o- X# V1 H 'order' => 'DESC',. o- n- v8 t' V7 z0 U9 E; n
'orderby' => 'date'
1 h! T- t7 D3 [" a) {- \( Q; } );
8 ~7 O' B4 l; f& Z% X $query = new WP_Query($args);+ c" _' C2 {5 b i5 W h% P6 V
if ($query->have_posts()) :, K0 q2 H$ E8 q' G; { w& u, u
while ($query->have_posts()) : $query->the_post(); ?>
/ T% z" M, J9 V% u8 G1 M8 j( ^4 F0 U' s <div class="notice">
- C; ~1 L2 [& c' S <h3><?php the_title(); ?></h3>. y+ m; M3 P F1 d! A
<div class="notice-content"><?php the_content(); ?></div>
6 }8 Q* z ^/ J </div>, @( ^- p6 x( v) x; m+ F
<?php endwhile;% S3 f' L' l1 O) b8 M- B
wp_reset_postdata();( S. K8 G+ W1 A/ H
endif; {& m8 D t/ D1 G) @
```$ a/ i( K) k% h' p9 s. G" m
6 H9 W) y2 z! ` 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|