|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?& L$ Z' \$ w* X) F. Z- ]
u: J& ?- N! q如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
* p5 N& a( S) ?% G" N5 }8 T3 h N- q% o d( w. o4 @; Z
以下是创建自定义插件的步骤:
. g+ ?* f9 R9 J( ~; u
' G# a4 \9 b& m1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:" E; p+ q ~8 C E; I$ t! E+ Z
( T8 e4 ~; X; Q2 O- @( U ```
6 j4 R. \8 q: L( t, C1 Y <?php9 |0 s. J/ E7 C$ v' Q
/*( z: k" @- |2 W; x7 t' w/ {( W+ X; R
Plugin Name: Site Wide Notices Plugin) d: E- M5 R0 R( C0 {7 U- r: q
Description: Adds a new custom post type for site-wide notices." u1 ` U a) ~( D( O/ ^2 \9 V
Version: 1.0' }! T8 D) u1 s
Author: Your Name' H+ o7 r7 X$ \: ]
Author URI: http://example.com
7 l, x D- u6 d. l' i */
0 N) {% W# [, B: j# c# |. }3 {, L
// Add plugin code here...
& z1 `8 K- M/ T3 z. V* ~ ```
6 }: w. I8 [) A$ c
9 H7 [! h: w( X8 k- u; Q 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。/ |. F2 k$ B6 V$ S* }2 b; G" z! `
) X5 I) r, ^6 x* n) |, t
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:7 u3 u$ ~# x. B
, f0 i* k4 z9 K6 } ```
2 H( ^0 g4 f) ^% t a% `# _" i add_action('init', 'create_custom_post_type');) \+ R3 M. @$ A! x% Q' h
function create_custom_post_type() {; u" \6 h( f- b# O- Y
$labels = array(
1 Y7 i) O7 N3 }) [ 'name' => 'Site Wide Notices',
, o4 Q, w! d/ U) L- q: [+ z, @- G7 x 'singular_name' => 'Site Wide Notice',: T% O. {7 ~2 N
'add_new' => 'Add New',
2 M7 m/ F. s: m b& v2 q/ B+ x 'add_new_item' => 'Add New Site Wide Notice',
+ `; m: p' Z6 c5 O3 x. { 'edit_item' => 'Edit Site Wide Notice',3 v( K. w" I, d0 {$ O" C6 N
'new_item' => 'New Site Wide Notice',
6 _+ e8 i& }8 g V* H 'view_item' => 'View Site Wide Notice',
$ O. Q% h6 R$ n4 v. f9 p7 c+ C# i 'search_items' => 'Search Site Wide Notices',
d3 p- F; C' Z 'not_found' => 'No site-wide notices found',
8 s/ [! P0 T# e: G% l 'not_found_in_trash' => 'No site-wide notices found in trash'
4 \, U' ~% f0 ], d );
; p. Z w" H2 y+ Z% E+ |; s! l( L6 {8 V: w: a
$args = array(
$ Z! q( }4 k/ {: i 'labels' => $labels,. z8 \# e$ d, q
'public' => true,) i* e; o. T- C
'has_archive' => true,
5 g% x' Z! v+ w5 P 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 G$ l8 B3 ^7 V$ `% H0 ] K
'taxonomies' => array('category', 'post_tag'),
9 u- O. H( X9 {8 V2 f1 y 'menu_icon' => 'dashicons-megaphone',, v# i2 ?+ b- _6 j0 q# k! Z+ h0 f
'menu_position' => 5,% q: F5 }% O1 m( K8 @ `
'rewrite' => array('slug' => 'site-wide-notices')# j- H% T! u! h4 g; T9 @
);$ ]% }* |' M/ j
2 [5 I7 F9 l1 B. S+ i
register_post_type('site-wide-notices', $args);
+ K. c- e4 h# s) P4 E }
$ R8 O7 f( T- c' | ```
- `8 Q; m& }6 K# T3 p( Q4 ^3 L/ {% D+ K, \' e5 y
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
T- e) L: L9 E! B7 Z+ B9 e
4 |& {; Z" |! k" J2 d1 {3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:# _6 {2 g, g6 d7 K3 H; Z
0 l$ h) R9 I- n. _; M$ H8 K
```/ P$ C3 o o9 H- A8 H/ o
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');. z1 z7 \1 R% G
function add_site_wide_notices_boxes() {- d! c& z5 [, d$ D2 S/ B
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');* \! y# d% V) V9 |0 x2 R4 H
}
! b0 q6 A# p- w/ d6 x2 w; Y9 ~
7 L. [" E" m, ^; ~" ~ function notice_details_meta_box($post) {
' @9 T/ C& q* I) w: k- B wp_nonce_field(basename(__FILE__), 'notices_nonce');9 V( I3 n- m* z
$notice_title = get_post_meta($post->ID, 'notice_title', true);
$ X2 J4 `! M+ t' D r) w6 @ $notice_content = get_post_meta($post->ID, 'notice_content', true);: i; H T4 ?3 d4 x
?>
1 F+ ]2 `4 o& f; ^. T <p>/ ?3 i$ ]& Y' Q) f! C. S6 Y$ X; ]
<label for="notice-title">Notice Title</label><br>
z W6 g6 f" m/ s <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">5 r% R( |+ b' t' W: Q. L# E
</p>* }( N v5 ]+ D: G
<p>2 ] i x4 N5 S& K
<label for="notice-content">Notice Content</label><br>3 Y( a1 h$ S: h% r1 g7 c! A
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! F k$ s* `0 l6 A; i4 S+ {8 k5 S
</p>) R- s5 n* T$ v+ H0 a# T
<?php+ d3 C* E. q( i9 Y$ Y
}
4 W, i, G$ z5 p! c j7 ]9 J
, k5 J( X1 ^. v) m( T2 L6 T4 Z3 d add_action('save_post', 'save_site_wide_notice_meta_box');2 ]2 N+ T5 J/ J* k& y) z
function save_site_wide_notice_meta_box($post_id) {
, k" f* v4 p/ } if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
! O8 N/ Z9 p P' t" ?) ` return;* F1 [$ s3 a a
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
$ r. C( U% S/ q" Q return;
1 O& C0 T5 R4 ]9 p9 s# k% O% h6 T0 Q# }$ G6 F7 @% W
if (isset($_POST['notice_title'])) {+ a( e1 { v& B5 p2 Y0 s
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));& a) f$ s2 ]7 W7 T
}
) C6 h3 V: o: w }2 q% a if (isset($_POST['notice_content'])) {( H: G$ N1 g: t( f- P
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
7 m& S9 Z+ _. U4 m' r }
4 t9 t3 C7 F# ]4 F" F8 f }
M* e$ u4 p' ~5 e1 i ```5 P n0 ?. D6 H$ {, W$ J
2 S( m0 ]- b3 ~2 _' X& |4 Z- a
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。) ?% p3 C" }& M! n( U3 f5 V
5 {8 ?0 M% E! V a. R4 c4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:' m2 H8 ]6 j) g8 Y4 c8 d% C# Z
1 m; W5 n3 B% I ```
6 l2 c9 @# e0 Z5 h ~$ h6 K. j $args = array(
- E7 K, I5 d: M# a) i7 Q 'post_type' => 'site-wide-notices',
4 M: D, u7 F0 k* W( D 'posts_per_page' => 3,
% _' l0 w G' [ 'order' => 'DESC',
$ M# B; x8 r6 v3 k% I, b 'orderby' => 'date') f, Y! I' `- Z4 A( X2 D8 p
);: M; l: _5 N/ x7 e" u9 V
$query = new WP_Query($args);* [0 L+ k7 g; @5 F# J* y6 P0 \
if ($query->have_posts()) :
8 |) T- m! r9 J while ($query->have_posts()) : $query->the_post(); ?>; @9 S. e7 H8 w1 s4 K
<div class="notice">
! W' y9 D/ a4 t9 ^% O* A k <h3><?php the_title(); ?></h3>) g- C1 w: B2 H. t1 ]! v8 W8 G
<div class="notice-content"><?php the_content(); ?></div>
C) N8 \# d; P+ r) Z V </div>
. R9 W2 \. V2 ~. c' { <?php endwhile;
1 D1 ?3 Z5 s6 S# \: q/ P wp_reset_postdata();& W+ p4 m" M, [) s% J, J* C
endif;
$ D5 V$ }! R3 J7 @2 u$ w ```) U+ S; m3 T7 H
) J+ Y4 b+ J! r6 C' A y 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|