|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
( b+ B$ f. l3 }
0 }) s) ?; }! C; T) Z" [* O. D如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。9 {3 J5 m7 o8 V. } @
7 ^9 n' x+ Y: X7 Y# l以下是创建自定义插件的步骤:" ?3 C# P5 ~# R2 `+ I. _- U
, @! B ~) b! [6 ^& h1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:& h. e8 K( g& l9 U g! @
+ e1 O0 m0 q; r6 F8 @; S0 K8 f ```
% E! Y1 G }8 y, H* v& @ <?php
, i3 z" Y+ i0 u1 P" | /*
, [" o; D/ |4 A# T, h. |" w Plugin Name: Site Wide Notices Plugin8 }8 H3 F7 r& H* X2 H
Description: Adds a new custom post type for site-wide notices.+ ^/ M+ F" J7 ` b1 [# W* s
Version: 1.0! h( ~+ M. h1 c6 d' {
Author: Your Name n6 J4 ^( b( Q* C
Author URI: http://example.com
! X9 A* r2 W* S1 C4 O */
" N1 z- d1 H. K9 b) A) Q' a+ G! J/ {( {' c% Z+ x/ V Q
// Add plugin code here...
& G9 o N& x1 M/ C) g9 i3 e4 r ```
7 I$ y# ^$ D& S8 Q6 X7 u3 ]1 D2 i* L3 n* x& P# P- _
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。9 t7 T @5 \: U% A
6 {) e. _ E2 L- u. G, r( f, p4 v2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:# ~+ [3 H. r! I" g5 f
R& ~" e) n m+ `
```+ F# E* d9 ~6 x, e$ A
add_action('init', 'create_custom_post_type');
' [' Q: ]5 P! v# x0 T' \ function create_custom_post_type() {
' C7 _' |9 W# c9 _2 u v+ V8 { $labels = array(
) a5 X |' r. W$ D5 k 'name' => 'Site Wide Notices',
7 L4 |+ w2 b' Y5 o7 U2 d 'singular_name' => 'Site Wide Notice',+ W" L# V0 A- A+ n$ x4 |
'add_new' => 'Add New',
' P3 V, {; S' R& _* i 'add_new_item' => 'Add New Site Wide Notice',# Z `$ }3 |2 t+ S. t
'edit_item' => 'Edit Site Wide Notice',
2 i0 K' H8 B" G! @' @' O 'new_item' => 'New Site Wide Notice',
3 ], X. d5 x% ?, O- v 'view_item' => 'View Site Wide Notice',$ S' C# e- b/ |+ ?& d
'search_items' => 'Search Site Wide Notices',: T9 F0 h' D" L* L0 `
'not_found' => 'No site-wide notices found',
. y* F$ o4 f$ h; I5 j! f& t* [ 'not_found_in_trash' => 'No site-wide notices found in trash'
, K7 s0 m! [/ U8 j! R0 X3 ? );
1 g* e1 d! z( D% v7 `- i' P! k* j) k( q) C" O
$args = array(2 ^+ z0 w* s& n0 w
'labels' => $labels,- _3 M, t; B6 a, I, j/ T3 m- ]
'public' => true,- T% G. [' [; J
'has_archive' => true,
& ^) v! M. k. D. _( H5 W) e9 P 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'), J. e* O$ q' V* t9 i; k1 w
'taxonomies' => array('category', 'post_tag'),! E1 Q/ \9 u! B6 p
'menu_icon' => 'dashicons-megaphone',
1 Z) F2 @# W) X 'menu_position' => 5,
, p, J c0 P/ e9 L7 @ 'rewrite' => array('slug' => 'site-wide-notices')* _. T; R" R) S8 W& f( |1 D
);
3 d; s Y6 B( V( P; c+ t% K1 \
* G7 u7 J# j* k) E" k1 P register_post_type('site-wide-notices', $args);$ _5 P2 Y1 W5 i- M1 T. ~
}3 @' r1 o. `; O% E7 H+ S
```
- p) R3 ^- _4 y# Q3 P! h3 l0 k- t# s
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
0 A4 {' [' C" M9 \3 \, d
- r- R- V9 W: E% t3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:7 w5 X1 ?: E3 o
" f8 `+ q5 B: I1 l6 k: p ```- H& B. k! X9 U# b% ~
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
% q' o0 f& z2 y7 x# x function add_site_wide_notices_boxes() {* [) O7 C1 ~, h
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
6 T% d9 N5 N* P: p* r }
9 T `% h. E+ A. g' ? ?# ]8 B9 @. E' Y9 w. N. |9 W. v: F& M
function notice_details_meta_box($post) {8 O, X% Y7 P' N% p# b1 B
wp_nonce_field(basename(__FILE__), 'notices_nonce');# v9 h) l% w7 c! a$ }+ g
$notice_title = get_post_meta($post->ID, 'notice_title', true);; A+ W8 }! E" i1 q( i
$notice_content = get_post_meta($post->ID, 'notice_content', true);
# D- i4 y3 J' n% @ ?>% W$ `7 z3 T# L* C+ \9 N P3 W
<p>
, n2 ]( V$ U: o8 i! h5 r3 E; H <label for="notice-title">Notice Title</label><br>
/ O. g0 h* Q+ h/ ]9 f; J <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">0 P0 z$ w. J) ~ t. \5 y
</p>' {; q( a1 H& A- W* K
<p>
1 V% i j' h: `- q9 o V( T <label for="notice-content">Notice Content</label><br>
) p A( }. V' _# i) J7 O$ g* c Q6 j <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>) e- O, |9 u7 r4 X L( f
</p>
3 s% e2 \1 a$ ~) N e5 E* d% h <?php+ E% u/ n8 G* p) {9 h/ M4 G K
}
+ t- ~% o1 J; a" c9 e( X
" \+ E( d$ S2 e9 W add_action('save_post', 'save_site_wide_notice_meta_box');# n6 B9 l+ v: \& u
function save_site_wide_notice_meta_box($post_id) {4 T2 V+ P) P5 u4 C
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))+ l. j8 z0 u& z/ c8 P
return;
9 Z5 \, c/ |. x3 {7 c9 R if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
* ]9 ]+ i$ _) T; D return;
& v4 [: q3 e/ ^+ P6 t
/ J( K6 a( _3 Y# ?% |) b, B z if (isset($_POST['notice_title'])) {
4 s/ N) [3 o7 J, p% u update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));* u+ u. g# r/ S* k/ ?- H* K
}: _( }8 d8 ]. U. [
if (isset($_POST['notice_content'])) {
- {( Y6 d% P) ] update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));: i! }" u" u# G- D. h. ^
}' F% b( D3 N5 J
}% L3 B) Z( g; B) e
```
0 E/ N' d" [4 h O" {/ e6 W3 W% Z' S; E$ W
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
* z- G |% f* N3 u/ h3 n3 }6 x/ C0 J$ W" ]/ x7 B
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
D) p+ Q Z" F4 f' f8 z, l9 w4 x" `" k& e7 t$ W
```
1 i3 S O- G) ?; s0 |$ \ $args = array(
3 I! G/ m7 B8 C7 e 'post_type' => 'site-wide-notices',
5 c( j, w- s3 p% J) `4 k 'posts_per_page' => 3,+ Y& Y0 Q9 B2 c
'order' => 'DESC',
5 Y* ^! l. A' t, W, i 'orderby' => 'date'3 y4 O. Q/ b/ @; Y! g7 B
);) k6 O) K- ~* e: t
$query = new WP_Query($args);3 E+ {& V9 O; o% r9 b+ z4 H$ V z" g
if ($query->have_posts()) :
, I' ~/ X) w n5 C5 Q( ]7 W u9 i while ($query->have_posts()) : $query->the_post(); ?>1 B- Y5 X% S* K; M; `" j
<div class="notice">
% y! X ?7 W' Y% X) Q2 U8 N <h3><?php the_title(); ?></h3>
: U/ F0 K9 m! d- k, j5 K% u$ X <div class="notice-content"><?php the_content(); ?></div>
3 o( ]) u" a: q; b* k0 L </div>/ _& I' W; B9 O# C
<?php endwhile;' v5 u f# Z. j: e8 M
wp_reset_postdata();
* ?. B7 ^! E* E7 [ endif;1 A6 f! I- ^) P3 q
```* S# H' q) Z' S3 ^& ~ G1 L
# D7 [ a* ^! d+ g: z/ U" g
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|