|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
: Z# |6 g6 J/ I
+ F" e# H3 E% t: g' L0 x: ?如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
R, ~' ^8 |" Q) i6 |9 @$ g( |9 _
以下是创建自定义插件的步骤:
& b' }( z0 e) Z5 G% Z2 \6 s( B+ ?. r8 `. E# Y7 w; Y" G
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
9 r" B; \2 }2 x& C9 F! S; N! F/ r+ c
```: L1 |* d+ l2 L ?/ \
<?php
9 A+ Q5 K9 I9 {: @2 T/ g /*) k9 w6 F. f1 B) ^) g
Plugin Name: Site Wide Notices Plugin7 |% K$ a' Z- n( |% ^2 w4 @$ U# L# D4 e
Description: Adds a new custom post type for site-wide notices.1 H! L# ~) |/ Q/ r. j
Version: 1.0, ?& k v) d! z; T
Author: Your Name& [" R4 Y3 d; } I& p) W
Author URI: http://example.com/ S6 f0 t. E& R9 S! n0 V/ S% h
*/
# p, Q8 ^& w4 I& `
; T3 D, R! u- y) X9 Y // Add plugin code here...
8 R* o6 y r2 A& ^! x ```
6 u3 I) v# e# Z1 v+ x# k- w" E' N
/ y" D; ]7 v+ E2 B3 L: K; Q2 n 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。% {5 l! R/ P* V6 y7 A" @
% l5 E* d5 c S/ q2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:/ ^ V+ L- z( Q5 `$ ~& p
, c0 L+ G1 O, C" y2 H5 c+ [
```
# w: y/ G8 h2 C/ V3 O add_action('init', 'create_custom_post_type');
. X7 J* v$ \# l) d function create_custom_post_type() {
+ J& u; A! ~. P. M, G $labels = array() n% B* n' F7 x+ y0 O
'name' => 'Site Wide Notices',
3 X4 N4 x; K% d2 A+ z 'singular_name' => 'Site Wide Notice',7 O; j2 ]# u- K
'add_new' => 'Add New', ?( E( H; L! l. L8 x6 H z! S
'add_new_item' => 'Add New Site Wide Notice',' x+ l @' S0 W) M G
'edit_item' => 'Edit Site Wide Notice',
! K. T: l( T# K1 D 'new_item' => 'New Site Wide Notice',9 z: B& G$ `; p" D5 M
'view_item' => 'View Site Wide Notice',
2 ?7 M1 G1 x+ N/ U 'search_items' => 'Search Site Wide Notices',
! W3 E: [6 w& n @/ ~ 'not_found' => 'No site-wide notices found',5 k( g. t% Z5 {* b9 t8 R8 G
'not_found_in_trash' => 'No site-wide notices found in trash'
* U$ }0 J3 ^& L% r* w );
5 p3 m( o" F+ D, }. q7 t! l! }3 \ R) ]4 u
$args = array(& m6 h" }; O* z! W! Q# ?" o
'labels' => $labels,6 }% W4 \) ?+ _$ [' I
'public' => true,
4 o0 s2 d! n6 c& X! r3 a 'has_archive' => true,* B$ s3 x* A: V: u0 K4 V- M8 @- q
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
7 h& M) z% P! y' P4 m 'taxonomies' => array('category', 'post_tag'),
- e' X% [ n! Q2 b, \4 e 'menu_icon' => 'dashicons-megaphone', ~/ V2 M" q* k) T, g- d. j- V
'menu_position' => 5,. F- p% T& M5 j0 S& u N9 x( o, d
'rewrite' => array('slug' => 'site-wide-notices')3 O( |& j% a. x& u, _0 N b
);
+ T7 U6 ~! e& p5 p; B0 `/ x7 `: P" w. |4 b n
register_post_type('site-wide-notices', $args);# ?8 [$ ^' ]0 t+ N2 u
}+ c b5 {8 [ {2 K
```
% ]* C. H2 x- _* z5 J, {
( `6 {1 r" c- c) N6 {7 K2 @) r8 Q 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。4 H+ l* L1 R4 U6 m: ^; G3 ? M+ l
# ~! S4 n* M$ T' O1 F6 J- u+ O$ `3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
4 d' F) P0 T% i( n+ ~& X# d1 y5 h7 |- h8 O# Z
```# I/ ]" N% q( S/ S3 w9 x0 x3 x9 ]3 V
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
6 z- W+ ^% v9 X* |: L function add_site_wide_notices_boxes() {: Z- _5 \1 C7 Z/ C% r( ~; w
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');# U8 N) w3 U1 w. [
}
/ }# B& ]6 Z; S4 q5 @" Y2 z
- I2 J) `3 R' ^% I' U0 p0 b function notice_details_meta_box($post) {3 H# b5 a- K; t* U& P6 Y* m1 X
wp_nonce_field(basename(__FILE__), 'notices_nonce');
) a+ w) k/ p3 ~: i $notice_title = get_post_meta($post->ID, 'notice_title', true);; S# o$ E, _0 [) y* s
$notice_content = get_post_meta($post->ID, 'notice_content', true);
$ m# H+ o- l4 v4 w* \! v0 \ ?>
5 O& a) R7 |: ^, K <p>; M2 m5 }' E w0 U
<label for="notice-title">Notice Title</label><br>
E9 y+ `1 P( @% L+ [. b <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
, t! ]8 H, O0 x" I: y </p>
" Q- j6 {/ J$ }% o/ K/ u' S" P <p>/ |0 c$ O- Z3 E2 G
<label for="notice-content">Notice Content</label><br> X- O) l2 f: X4 n
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
9 b+ W6 l4 M& p T+ H </p>4 }- _- i3 q+ _0 j. H
<?php7 [8 _4 c/ L' o4 K
}% l$ C( N1 S# x- t5 M
3 Z# I; i: K2 D' R) f$ s7 V
add_action('save_post', 'save_site_wide_notice_meta_box');
" G4 T, o2 A0 i& I function save_site_wide_notice_meta_box($post_id) {
5 K; S- |, J; O" ?; ?+ T2 R/ n if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))& {" O7 I l& d6 f: \% }! S3 d0 B
return;3 H7 s2 o; X F1 c0 e+ z
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)( E6 V- [8 @% x0 ]
return;
9 b* w( R9 T8 S; \ r w* O0 n+ B, h) I9 N
if (isset($_POST['notice_title'])) {9 b2 R- W" n/ [
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
- p" O3 Y/ \( k1 J5 { }
; k( F0 q/ q* ]7 b' M! A if (isset($_POST['notice_content'])) {3 _7 m! o3 \- U, D$ U
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
" Q2 ]- f/ G! C0 n# w9 ?+ E. V }1 }+ h" L: }9 s
}# ]. c X! D- g! U
```
- k4 f* }! E; \ O
+ ~+ X# u9 y: B* |* I; s: U 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
% n; O6 X: P8 ~9 v0 d- B
" f+ U3 I$ c4 E4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
: J0 q3 p7 W6 j& W& p- x! Z! e" r7 E$ r; V( u) M, @$ V5 G( Y
```) k$ i# K/ v! V5 g+ e; }" W* V
$args = array(- W; @+ A$ n! F! ?
'post_type' => 'site-wide-notices',) Y1 u. Y) g; w# J" r6 ] s
'posts_per_page' => 3,
0 }( H" ?/ y) U/ Y1 W; d& G 'order' => 'DESC',% ~, R& o" }, ?5 w1 @# J/ Q5 g
'orderby' => 'date'
) _% K8 L( R$ z );
9 o/ [6 W0 Q5 @6 V, k' R( X $query = new WP_Query($args);9 k4 n+ X0 S. S+ c
if ($query->have_posts()) :
, v4 c( _- ^' z: l/ N9 B8 z while ($query->have_posts()) : $query->the_post(); ?>
+ T* R Q. _7 r8 y3 L) {1 ^ <div class="notice">
% W4 J$ r& H/ U; m2 c' }' T4 ~ <h3><?php the_title(); ?></h3>7 F& R0 {0 [& c) L
<div class="notice-content"><?php the_content(); ?></div> [/ k- r }$ d/ k2 ?7 h- u g A
</div>
: M1 F6 v1 K" a: H( r# | <?php endwhile;
2 D( y+ @( k2 C' N5 {2 z* P wp_reset_postdata();3 |' w" H* d0 E2 i. \2 h; e1 Z
endif;
% N* w4 G- R3 o# Z% d6 { ```
3 d0 a2 }- s" t6 L! }
7 G0 r# ^/ {1 y 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|