|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
5 A8 x* y- N2 f# N9 e5 D
3 G7 c, b3 ]/ P如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。! G* {8 Z' E# k; r, k/ A
# s% O( X8 V- _. y: K
以下是创建自定义插件的步骤:
O% W; s! q6 q+ }
4 K: r# C& D( d) W+ c+ u# [3 O+ w1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:5 J7 v! C0 _+ _% B/ p& ]& x, s
# {% V$ N- F7 \ ```
$ D. E2 I& L) d9 P$ k8 R8 t <?php
u( m5 V! d- v' D5 [* n" [6 a /*
1 @2 _$ M9 i% _! C% q Plugin Name: Site Wide Notices Plugin3 a6 ?! s* p" T
Description: Adds a new custom post type for site-wide notices.: S# M3 q2 R3 D$ c1 }7 _, [4 ~
Version: 1.0
5 z: r' r- L7 f& Q Author: Your Name
& {) {3 N8 ?& e, _ Author URI: http://example.com* r9 }+ x8 h# @7 D$ f1 M! N* n F- J
*/
- X l8 o" t7 x* I
3 H9 j- b; o' ] // Add plugin code here...8 e. g- @; w& P# V& Z7 n! I4 s
```
9 ^% [, V1 B3 y9 p
$ H) Q, z' @" ]5 J& v) q4 r 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。 {* }! o$ v! n' `
+ [0 C8 y. l j3 G
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
0 N. U- I5 U0 V7 Z: b" Q, k3 m% N4 K& Y1 d
```! P3 N5 b# Z1 X4 O2 ~3 t0 J
add_action('init', 'create_custom_post_type');3 |: Q7 p. H# [
function create_custom_post_type() {
& ]) E! U! v5 k& } $labels = array(
' m$ ?+ n; q6 t) Z+ V: T 'name' => 'Site Wide Notices',
( f8 c- a; W$ S1 d5 \1 n 'singular_name' => 'Site Wide Notice',
5 f6 d" N9 j: E" N5 `1 p% B. I 'add_new' => 'Add New',: l3 W' t. D: H9 R4 h' ^! r0 ?
'add_new_item' => 'Add New Site Wide Notice',1 [5 ? N- r3 g1 d( r5 l
'edit_item' => 'Edit Site Wide Notice',6 L1 t* `% j" m q7 U
'new_item' => 'New Site Wide Notice',& w' R/ J# u3 y* I" W! E/ |; V) M" Q
'view_item' => 'View Site Wide Notice',
1 Y& ] j' J& T 'search_items' => 'Search Site Wide Notices',
" Y* H3 U0 K# G5 y 'not_found' => 'No site-wide notices found',+ m7 k% P$ N* U- z. X T: ^$ D
'not_found_in_trash' => 'No site-wide notices found in trash'
% x3 R7 ~& G: J1 f& z/ Q- {- r7 } );
/ a% [7 E' B# V. ^/ N+ }% v- f+ P- f
$args = array(. Q2 A- V) e/ n9 e+ s" g* L& Y
'labels' => $labels,0 c% Y7 x- {: m, z
'public' => true,' w( B+ R1 [# y
'has_archive' => true,$ [* a1 `6 [; f
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
8 a4 w; m f8 }7 ~5 J 'taxonomies' => array('category', 'post_tag'),, G% G7 j+ O5 k
'menu_icon' => 'dashicons-megaphone',
) Y. T& c' Y( o 'menu_position' => 5," l8 U& S8 b7 y6 T6 }# u
'rewrite' => array('slug' => 'site-wide-notices')) V/ { l W$ d! C
);
0 z& w& X+ l8 N; l& W" N$ o. c
$ |; G/ w% W* @: ^3 A register_post_type('site-wide-notices', $args);. X: K7 i+ P. H$ C, @: ^
}' J2 H9 @, L+ n1 N" j: \
```
8 f0 }5 W# A' U3 U7 X9 X8 W& ^: ?4 l: ^3 o2 L2 F
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
8 Z% |8 u7 i, X; N* j6 v& H8 {* K5 I! g
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
( b6 s0 s! \* G9 J7 a& |9 [+ ^: I+ h6 A7 e& m
```
: U* w# X4 c8 x4 n6 `/ Q0 T! V' Q4 r add_action('add_meta_boxes', 'add_site_wide_notices_boxes');& y- }. g3 N5 N7 G% ~( l X8 W
function add_site_wide_notices_boxes() {
' H/ Q0 Q# a6 j! X$ i3 s4 I add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');, Q! T2 Q( X' q) H) J f. u( j8 O
}
; G4 ~$ Q) t' x6 u! o3 g3 F0 c6 ?7 ~" }7 _5 F6 h8 O
function notice_details_meta_box($post) {
4 t5 |6 i r; D* c wp_nonce_field(basename(__FILE__), 'notices_nonce');
2 {/ C5 U+ I+ ~3 ? $notice_title = get_post_meta($post->ID, 'notice_title', true);
% g- i5 H5 j5 H5 E+ d! e# |+ F4 T( {1 n $notice_content = get_post_meta($post->ID, 'notice_content', true);. _" `; L! v# n/ p9 D: o
?>, C4 q' Y# e* M6 \! F% S1 v
<p>( T! h+ i) M2 r/ \
<label for="notice-title">Notice Title</label><br>. s& ~0 x3 I, `8 g9 E: g8 C- f
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
W y; v d! T/ ]$ r" E: b; X) {2 | </p>
; h" H9 T+ p; g$ u0 L" y <p>
5 u0 y( d: S, h" |7 ~& f <label for="notice-content">Notice Content</label><br>
& ], [8 c7 U8 P% ] <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>$ n7 P% n8 ?( v/ t# I7 H
</p>
* @* P0 e8 y6 U o _0 G8 O3 v" b <?php5 j2 y5 P" F0 G, Y2 x
}
5 @( a) N6 A) r, Z3 c; U# X3 n" a! B0 x
add_action('save_post', 'save_site_wide_notice_meta_box');
6 y U) X# w% d; w) D function save_site_wide_notice_meta_box($post_id) {
' a/ @$ e" {: T- b& c if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
6 L6 \. Y# D: r' z5 F$ C3 k return;
; ^, B6 D* s8 o if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
4 G- \3 r& t* {8 m* b+ l6 b return;
) O7 z* }/ p3 f2 Q% B0 z6 P# i1 z/ c- d
if (isset($_POST['notice_title'])) {
0 v* E, M, q2 \& m# {3 t update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));: e; `4 Q( S6 \' z. r
}
2 g" K! u. l2 @' t8 Z; A if (isset($_POST['notice_content'])) {: n6 D, u9 D9 Z, }
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));( E$ ?/ }8 W* b; U: x
}
2 a9 f$ a8 G! V/ n f }0 {7 Q1 |2 E7 q* C6 i7 O
```
: v7 |. E( A8 x# R/ G* V# i& a* E( a, l& b; B. f8 S1 {
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。% ] l; f$ H" ~( ?# _
* Y9 r8 x. T3 u: F5 \# ^
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
" ~3 b7 ^2 H! R( L+ {! b- t9 O9 x0 a" X
```
2 N) ]9 W' n# U9 D- v& b $args = array(
d" F; R" _! F: R 'post_type' => 'site-wide-notices',
( W7 d6 f4 `: j" C5 t 'posts_per_page' => 3,5 {# L* _1 n7 ?
'order' => 'DESC',
% r4 z) ]2 s& V. t4 w J$ ] 'orderby' => 'date'
5 U& e3 D- A) M! e! _8 T );4 _ m5 z! K3 Q0 ~. c2 s+ q+ x
$query = new WP_Query($args);9 @" x0 U2 W7 i) s. V1 ~0 z
if ($query->have_posts()) :) V. k$ B9 q' @1 S0 o) b* E
while ($query->have_posts()) : $query->the_post(); ?>; w- n! Y1 C$ `2 v! Z
<div class="notice">
2 S: o, l; V# q) B" Y <h3><?php the_title(); ?></h3>
7 p! C4 W; R* @8 o, x& r <div class="notice-content"><?php the_content(); ?></div>
- A* `) ~* q. }# } </div>7 S! A# y0 q; v
<?php endwhile;
; L) D" x1 r% ] wp_reset_postdata();
5 H2 l1 \9 t1 l" _; z( z G! C0 Z+ M endif;
7 [, _) P& V0 p$ l8 ^7 N) | ```8 R( k! ~% O! y" ^- w# D7 x
; Q0 k% h1 p5 Y! w1 X7 |% l 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|