|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
2 K4 a8 @2 h( D0 S( k9 }" G% ?8 K" Z* M
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
- E( T/ h0 j9 a4 c) i$ W: `
2 ~* J# C5 X6 s以下是创建自定义插件的步骤:
T! j2 g& |! p( c% a3 U% Z/ V+ o! N T6 D0 i
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:- O% U$ z' X. K" L+ J* F9 G
+ e) A ^' R8 m* ?; m) x ```+ k/ k, Q1 K4 p- P6 ]( t" `# X) f* [
<?php! _/ K+ i- H5 b% s& Z
/* m9 \( j- Q& m: A9 j6 s
Plugin Name: Site Wide Notices Plugin
: G1 M" p5 t. q Description: Adds a new custom post type for site-wide notices.$ ?; I. P7 V& n1 Q3 i% T4 _7 {( Z
Version: 1.0( ]( ^# B& h0 M, i3 L& ?
Author: Your Name' V' v7 j# i* w; B6 X
Author URI: http://example.com. m1 c( \) c* P- D8 L/ A
*/. {" h$ x0 v f! v: B& ^
" _ [! I8 k7 o5 t: W5 e3 V // Add plugin code here...
, r0 h7 w7 w$ J/ f- [3 a! [ ```
% F4 u) [" y- X: O) p* \5 d( I& |9 Z7 ^% C" ^* r( x7 l
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
+ x' A/ f. K( n4 ^ c1 {
' ^1 @: E2 @4 W# z' |7 |2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:% v( V+ L, P7 G9 f( M
) m6 w* w/ k: ]& N
```
8 N7 E3 H+ X5 i0 ? add_action('init', 'create_custom_post_type');# K3 R4 P/ y% k0 W. |. g7 V
function create_custom_post_type() {6 K6 u" b) Z, A/ \
$labels = array(
+ d0 v) {$ u9 O2 z! t3 I 'name' => 'Site Wide Notices',5 |& U) U- y. |: `
'singular_name' => 'Site Wide Notice',' q ?6 J. v# G! z4 x8 @
'add_new' => 'Add New',
6 c7 y0 x- D# m6 s5 E 'add_new_item' => 'Add New Site Wide Notice',
( ^1 b% a9 S O O! _ 'edit_item' => 'Edit Site Wide Notice',6 ]& l! H' i/ u/ s7 j3 y3 g
'new_item' => 'New Site Wide Notice',9 `+ x$ n) r1 `
'view_item' => 'View Site Wide Notice',
* U* J" x R8 W2 K. O& { 'search_items' => 'Search Site Wide Notices',
2 X( c( y, q& Z' O% }# D 'not_found' => 'No site-wide notices found',
0 `$ T' D1 \# S3 z4 F0 l: s, \/ M 'not_found_in_trash' => 'No site-wide notices found in trash'
# C2 L1 D5 m/ O8 U! U' K% ` );; T( N" ~) n3 h" p, n
: I/ I5 r' P; q4 f6 ~ $args = array(
5 _ x$ K# [8 ]" o1 [7 |$ k7 i 'labels' => $labels,4 u/ j1 Y$ ~) {0 ~- C3 @
'public' => true,
' K, i/ @) _) ~9 C8 K9 ] 'has_archive' => true,0 N8 N: `; X0 F) f# t8 H
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
. G! ]2 P' Z* L0 H/ @% N" ]0 @9 H: }* n 'taxonomies' => array('category', 'post_tag'),
2 Q* n8 w5 S" [; j( ]$ A& M 'menu_icon' => 'dashicons-megaphone',% w1 |; Z; V& `# a
'menu_position' => 5,
2 Y- @$ w$ E7 n* e 'rewrite' => array('slug' => 'site-wide-notices')
# V' N0 C$ `" a. F6 j );
+ b8 Y. P! u0 B( ]
+ ?4 o3 h( L! W register_post_type('site-wide-notices', $args);+ Q1 p. p. t; R* M' \. h( J: D
}
/ o( e0 _5 Q+ R3 C3 T, d- O ```
" u8 f4 s! S- ~$ r0 m( Q3 v9 `8 C% D& P- }- f) k4 m7 p; E) g2 ]9 h" N* m Q
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。$ Q+ U* B/ r% s- I3 P3 M
; [: r3 {/ p& b
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
' T' N$ U6 ~8 G4 d1 r! ^2 Y3 L9 X
[3 P& m2 }" q u: t; N$ X ```, Q4 G* ]( E" L5 n
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
* S5 q3 J: u/ W function add_site_wide_notices_boxes() {" k8 [3 B* k+ W' \0 n2 K2 r
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');- G/ [7 |9 E) L R: D7 s2 G
}1 v- v( R# A- H
. ^# p8 a. f$ L \- G
function notice_details_meta_box($post) {8 \# R1 S6 ]6 `" [' I3 E
wp_nonce_field(basename(__FILE__), 'notices_nonce');
# Z) x! `* o9 j+ ~" U; L } u $notice_title = get_post_meta($post->ID, 'notice_title', true);
6 H" o9 q+ m$ ]' k, F" [ $notice_content = get_post_meta($post->ID, 'notice_content', true);
! c7 I$ ]' w: ?0 N% } ?>
7 C$ \: Z7 J. s4 x <p># g: l8 P" M, I' F4 y) N# L
<label for="notice-title">Notice Title</label><br>6 Y5 R6 m! {- N7 X7 i e& E) J
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">" o( f$ k: k; m) m% y
</p>
+ y' `$ Y2 w- R5 x! ?: A' P$ s& g <p>
: A8 C3 f. ^5 ~7 x, ~! `/ ^: m <label for="notice-content">Notice Content</label><br>
* h% }$ |! b* b& c V( t <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
& A7 Z9 Z1 X0 X/ c1 n) w5 [* V% K </p>0 {3 C4 o) a/ ]0 i
<?php
6 c* u0 n: z# ~3 G }7 T( \8 y4 @' y Z" @ R+ K) `
: \: P% k# x6 w% y! _9 y/ h+ G, A add_action('save_post', 'save_site_wide_notice_meta_box');
8 p( k( Z2 ]$ l: |& ]* H function save_site_wide_notice_meta_box($post_id) {
- N7 o0 h D: N if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))( J: l" s. x4 d3 N7 c+ |
return;
( u- v$ n. Z- L" U9 P if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) ~# k- y, }. M) u" |+ `, z
return;# Y# G; H) l+ q" |( J" K6 E
0 v3 e% ~. r. K4 S) V+ P if (isset($_POST['notice_title'])) {
9 D5 k: x3 U3 P$ U- j# b8 v update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));5 q( a. U6 W2 ?# O
}: [. c/ F( N. O4 c$ _, t2 E) _
if (isset($_POST['notice_content'])) {
! ^1 P% Y9 y8 [, P. R/ \, j update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# j+ o1 G7 S4 N; D( R/ ]
}4 R! e" h: Z& D7 t: |
}0 _; i+ D* J. X" o, d
```
' n3 d/ n& z: l) b3 y( b: \/ z+ P$ T- Y9 C" x# |6 W+ t+ @7 R
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。8 `4 w4 T$ C6 F+ G
' W$ x8 h! d2 ?% c
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 ]% Z1 K2 j7 t! N
- G# i9 L, g- Y: \
```
' h" w+ [% Y9 q# S2 e6 Y8 L $args = array(4 {6 V( |+ R8 n
'post_type' => 'site-wide-notices',/ c) Y/ H5 V4 a% J5 B& g2 {8 O
'posts_per_page' => 3,! r, s. |/ I0 F! p& T
'order' => 'DESC',
" m4 q9 D2 ^) l6 U- Y9 |, u 'orderby' => 'date'
* g: t% A; P: f' W1 z );
$ |3 O! m8 t& ?: U$ D+ D$ X $query = new WP_Query($args);
1 K: _6 K. X* S" _! f" ? if ($query->have_posts()) :
( @) o, Y6 u. ]+ W' j2 t while ($query->have_posts()) : $query->the_post(); ?># w& r" _7 m$ n& H5 w" b- w1 n( f7 |
<div class="notice">
/ u2 Y l, ]4 ]7 \# u <h3><?php the_title(); ?></h3>
8 z2 O+ I$ v- r0 W9 U7 r <div class="notice-content"><?php the_content(); ?></div>
3 e6 r* y) W4 A7 K* v1 b </div>8 j/ x- r- Y9 u% E o* R8 {. C T
<?php endwhile;
* F! C6 _) }" a/ t3 S1 H/ q& I+ L wp_reset_postdata();
8 Y1 \6 k& i# S( Y( \2 y" f endif;4 a" P, B1 Q' q
```; Y$ o6 A1 L# e. R
& p6 J s" f3 g1 x7 f- I; Q* m 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|