|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?0 s X3 F. {- C# V
" A2 P1 Y) v5 Z2 o2 I# T如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
) O/ ?3 c5 k& n1 ?$ a3 f* e, i
# F1 g* {5 g5 y7 j" V4 P2 j5 h以下是创建自定义插件的步骤: r; b$ i4 S9 i! K! J( ~- b6 ?" n
7 i0 a, O% F) n5 o. O# E
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
: x1 E6 @7 [! a w3 z) e" T
8 @; g& ]5 g. V/ D# k ```
+ j, J3 N$ t0 V; z- O2 j# O <?php3 U2 J6 L! F0 I' H
/*$ U2 g9 @! u9 K8 j( M! H& g+ j8 A
Plugin Name: Site Wide Notices Plugin) T: s) V; D6 t: S
Description: Adds a new custom post type for site-wide notices." k8 G4 J( t8 c! G
Version: 1.0 Z! Y9 K* E- P) q, u6 e
Author: Your Name5 x' g3 m$ T8 e2 \
Author URI: http://example.com
l& ]2 w. x' r5 a; J */ t1 M9 U% n. U8 q
7 |! @5 ]% ]! y7 r
// Add plugin code here..., D; S2 J+ |8 h0 y7 H% G+ ^2 R
```
; f% ]5 K1 s: ~; ]9 ?
" o0 Z% @- Z7 @8 T' l9 t 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 B, U' R. Z+ R+ E8 `$ h1 P% e T+ K6 r+ S, L
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:1 I. Y @- ^/ l# V
9 B! j0 w: a4 _/ H# F" Z. j( T7 | ```3 W# _4 I# l$ D [) ^; N' f2 P
add_action('init', 'create_custom_post_type'); |2 U" ]5 s1 @) }% r/ z$ Z% |2 Z! }
function create_custom_post_type() {
7 D% b8 E- }4 |9 n" s/ x. [) ] $labels = array(
, q' p) C, k" U4 s5 N; Z 'name' => 'Site Wide Notices',& M2 K( ^/ t# p) `- ?- i' s; j
'singular_name' => 'Site Wide Notice',
& @1 B5 e4 f e" w2 u 'add_new' => 'Add New',
* N, N/ L: a0 x1 H g 'add_new_item' => 'Add New Site Wide Notice',
0 u& B2 z H- S4 \9 }+ u 'edit_item' => 'Edit Site Wide Notice',8 b- [' K4 X" ~* F7 i; E! }
'new_item' => 'New Site Wide Notice',
; {+ {/ @' k2 H/ i1 x! b) ` 'view_item' => 'View Site Wide Notice',
) x+ n; Y% A# \5 D 'search_items' => 'Search Site Wide Notices',
$ C6 f$ @ q- ?' L9 d2 k9 W$ ^; Q 'not_found' => 'No site-wide notices found',. b# ]& |* n% ]
'not_found_in_trash' => 'No site-wide notices found in trash'
) } a! |# L4 c" E2 e );
# ]. }1 c# a& @" M( M# Z$ m* n: ~" {6 s) m7 s, ~
$args = array(
1 _4 t: G/ X \+ w, n1 K" N 'labels' => $labels,4 V$ w' p4 @: \$ u" I7 e Q& A
'public' => true,
* o9 ?/ S8 @& m5 ^5 F2 Z; @) p8 | i 'has_archive' => true,/ k9 A) R' x- P
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),% M5 Y9 h0 }: m. c z8 w
'taxonomies' => array('category', 'post_tag'),3 c8 r1 M5 Y. q1 q( v, y/ z" s
'menu_icon' => 'dashicons-megaphone',+ u6 w6 u: N$ {0 h0 b3 J# h, @: d l
'menu_position' => 5,
9 [2 d% `& g L% ]. V! B 'rewrite' => array('slug' => 'site-wide-notices')
- d7 I+ Q- u2 V2 a) [. A );. E% j4 T7 Y* G9 H/ p
" b; w8 y1 l# w; D; _% ]
register_post_type('site-wide-notices', $args);
* z4 y! \& t* w5 n. S+ U }8 ~+ G4 a8 L/ | O. A. z
```
; Z- {; P0 U7 d7 u
1 o# |3 g2 T- B! G 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
1 Q& l h. v% y( O# f y; r3 |( W$ E4 S% W2 Q# h% a
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:$ R. e6 r ^3 m& ]5 e9 K2 Y
5 c6 g! `# T: b n ```
5 x0 e: O; M7 I% [* `+ d add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
; G! s1 o9 }: v5 Y7 R! _ function add_site_wide_notices_boxes() {
~( y; D) \: h# E' n9 z add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' \1 j% J, C3 W1 u }+ D1 i0 H3 r4 s+ z: x5 _
' z) N$ @& c" O t
function notice_details_meta_box($post) {
# e# n; B5 u$ c6 q! G, q wp_nonce_field(basename(__FILE__), 'notices_nonce');8 [* f* e1 H* k7 T( Q9 Y: x
$notice_title = get_post_meta($post->ID, 'notice_title', true); t$ s5 f Y" C z! e8 P4 O
$notice_content = get_post_meta($post->ID, 'notice_content', true);8 w( X2 J& r8 P& A, \2 M y3 @
?>+ C; [0 u4 d$ I4 Z. Z2 o
<p>+ N' C8 G+ r9 c1 h; _+ a
<label for="notice-title">Notice Title</label><br>
$ U- e4 a5 ~, k5 } <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">9 S0 K" S5 `$ b4 ~1 U0 m
</p>
4 z7 X9 p3 W" b2 F& G0 i7 j <p>
& ?3 l! |6 y$ R2 D <label for="notice-content">Notice Content</label><br> a, [1 O }9 l( H- u
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>. n- [( S3 }2 \* c" J5 i% F# ~! z
</p>0 a; Y/ F; Y; M; f' b$ e0 S
<?php
+ H; K& h$ L0 [& h9 V }. ]) z4 W( a7 v; r% y
B$ G/ t0 f" v5 D; `( O
add_action('save_post', 'save_site_wide_notice_meta_box');
0 m$ o- b! G) ^: o function save_site_wide_notice_meta_box($post_id) {
8 a, ~3 H! H; ?/ W4 j6 n if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
. l" w3 J+ F# N- k9 q- n return;. s' H! {' ^! s! B* e& P* J& U
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
1 ?* [ M4 X2 y, {4 m return;. P: {) \3 F3 W( `7 n+ i
: O' E9 V' Y' X5 Y1 i, d if (isset($_POST['notice_title'])) {
& @$ ~# J+ ` \ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));' t' m1 n) A, h% C, x X4 K4 M
}) h. X$ w: Q8 K/ v9 H3 W* r" m
if (isset($_POST['notice_content'])) {4 A% A0 G( ?& K* y# K Q
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));; a) f' I/ V2 P* c: G
}
+ M1 _+ M) Z0 F& E6 | }
1 g( y, L7 g# J# r$ c% y' w ```2 I0 s; V7 A: r' @1 i0 t9 }
" x# h3 i: D" d$ k0 |' U1 A. D! W
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
- A- r# e6 e. }3 T4 I$ X( @0 M4 k& U- d3 q9 j+ q# V# `
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
/ H L, y; x9 ?7 h# O
5 ?, P+ Z* T8 Z- e3 j: @ ```& C5 j/ p, ?3 {
$args = array(
# R) J& r, U, L: @7 X5 N* ^# C% M 'post_type' => 'site-wide-notices',
( E7 v! e3 B9 _" m9 B 'posts_per_page' => 3,
' k$ b% T, |4 C* H 'order' => 'DESC',, I. k/ I. }" {$ m- }- v2 }: q: A
'orderby' => 'date'" g L; R& P5 q# b: |% R/ {, P
);
/ w. V( v9 v* X( G; y S, t $query = new WP_Query($args);
/ s1 m# }& {# ^1 Y. j" C7 d3 u if ($query->have_posts()) :: E) l% S9 N( n0 ~8 z7 Y+ K
while ($query->have_posts()) : $query->the_post(); ?>( ^: ^, ^6 m. `! M' K o
<div class="notice">$ V5 ]: w4 \; F( v1 _5 O
<h3><?php the_title(); ?></h3>
2 l. \6 U* p- E0 n9 y <div class="notice-content"><?php the_content(); ?></div>& l8 \3 C" x( S, P2 Z
</div>
( q) G: H$ f6 F. }5 Z V <?php endwhile;
# w- H8 K- E* W/ R3 i; l' N wp_reset_postdata();: D/ v* O' _& }1 \1 c5 W- \! t
endif;
9 `. V+ i1 E( j/ `$ e9 M) E ```
; [) [0 r% n; W5 L( }7 O6 }) d; S e$ x
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|