|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?) M$ S# x' N$ Q6 c
. ]' H9 X' c( P% E1 P
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。2 g* L, W7 z+ O
% Z: @- Y# k# q$ Z0 \% A
以下是创建自定义插件的步骤:3 h6 o+ @& q& ^( U4 _
5 B& @, H, D. G5 _! v6 V. N
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:0 a: q. C6 p H7 l3 C
3 @2 K$ C$ V# ~5 I
```) I$ O5 v. O) j( o' i$ d) I8 K
<?php
a7 K9 [ u( U3 e& m2 H3 }; P e; R /*8 k& }3 w' p, k
Plugin Name: Site Wide Notices Plugin3 v$ L0 E( K& ?5 R! r
Description: Adds a new custom post type for site-wide notices.
' z5 m9 K/ t) I/ K5 t) l- _ @' H Version: 1.0
) `" a0 o# n& x- q" {8 m Author: Your Name5 J, [/ T" d0 n2 U. s8 n
Author URI: http://example.com
5 Y( ~9 i% L/ Z( v */. \9 {4 {% _: {. O! \1 B% p
$ }. c5 g6 Z* T5 a& _) x
// Add plugin code here...# c. K5 x% Y" }; v4 t
```& J. t/ q0 C8 M, N2 y
) V* T% z0 @& J' v% ^. d
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
) P' W# @2 T$ K, @6 X' ^& t" ^! f4 C1 \& M- v
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
4 }; Z# |1 E+ ^/ `$ c( y
) F3 Y a/ p$ M' X ```1 [. V4 q2 g9 C" G
add_action('init', 'create_custom_post_type');
$ C1 K( A# e8 O4 ~ function create_custom_post_type() {
8 W3 k' f% \. ]. i $labels = array(( v. P8 c7 _& m+ V# A
'name' => 'Site Wide Notices',
& P# X( T8 ]3 d9 }1 A2 T 'singular_name' => 'Site Wide Notice',
^" T, d" a$ F: e0 X; y& S$ M 'add_new' => 'Add New',- h4 T$ f* R5 g& \9 s
'add_new_item' => 'Add New Site Wide Notice',
# m1 z1 v' w9 [ 'edit_item' => 'Edit Site Wide Notice',
8 O% s: M+ P* k4 k7 _ 'new_item' => 'New Site Wide Notice',
" p# {' }! e k' M1 W/ L 'view_item' => 'View Site Wide Notice',
) s( c! N! k7 [9 _; h2 b1 q' H 'search_items' => 'Search Site Wide Notices',7 s5 L0 T( p- w1 r8 B( v
'not_found' => 'No site-wide notices found',
7 \* ?3 X# z3 s- ], G8 l/ l7 [ 'not_found_in_trash' => 'No site-wide notices found in trash'
% Y. Z- u. d& s );4 j# P) S1 o9 w# L6 t# K# |
$ }; \8 L7 Z! G/ }# ~! {
$args = array(' S6 g* C6 }# f+ b0 X
'labels' => $labels,
- b8 _* k, u4 f; f0 W! I& O 'public' => true,$ s2 Z2 V$ M* D
'has_archive' => true,4 M1 `. F* }; z
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'), u; i- `% o# A/ R6 F0 u6 C' [
'taxonomies' => array('category', 'post_tag'),- ^- t e1 G# d' M1 V
'menu_icon' => 'dashicons-megaphone',
$ F! C4 [9 S9 P7 } 'menu_position' => 5,8 l) v/ X3 S& M6 E
'rewrite' => array('slug' => 'site-wide-notices')$ u. `( L, q- D9 Z% ?$ k% h; @
);
; W; l( N4 `. Y% E' U3 Q
" p" S% \8 ~4 K1 b7 i register_post_type('site-wide-notices', $args);# C& w* A; E6 ~3 O4 x
}2 D, f. i' N3 f/ ]8 a
```2 a' {' |; C' K6 P
7 l0 i; L. ]' E% h! c$ `" v 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。$ {/ K! y$ W. g# u
3 }: Q9 S+ C# o5 v3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:/ Q# K) L- E; _: {' N
" w3 g. y1 Z Q- Z) h: ~# F: J5 B
```- T. I8 w1 L( h% K) _9 b
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
0 O+ `5 {" O% u- F+ L function add_site_wide_notices_boxes() {3 k: W' p+ W# P. [! l. X
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
7 I; z9 r/ f$ j# v }6 O* N# b0 h+ ]( o
+ Z( ], J( c: A! Z- H$ F1 M function notice_details_meta_box($post) {* f, n& U% h+ Y9 ]6 V
wp_nonce_field(basename(__FILE__), 'notices_nonce'); _- e& l% q# u; k5 n
$notice_title = get_post_meta($post->ID, 'notice_title', true);
! N+ q( E9 ]6 L+ N $notice_content = get_post_meta($post->ID, 'notice_content', true);% c* _! Q1 K" @5 Y; T& d
?>
0 p( D6 \' g$ ^ <p>
" F# C0 a8 v* h4 E) i, ~# E2 m <label for="notice-title">Notice Title</label><br>1 v+ I/ z% H# J
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
) V* |; k; j4 E0 _0 B: U </p>1 o8 p0 h( a0 A& B- y
<p>
2 c" Z) s4 L; [2 w6 P" b$ |6 L+ D <label for="notice-content">Notice Content</label><br>* {; H6 s% n# O$ |/ m. b
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>- c* O+ T% n/ h" E6 t
</p>
: U6 n- u5 v9 @2 h <?php' a3 c# r8 t( E! O+ \" d" _0 U( G
}
+ b8 [, l% { I
) k6 d; v; k5 w! \% x$ h) h3 l add_action('save_post', 'save_site_wide_notice_meta_box');
4 v2 m( p: w$ U- S2 H! p function save_site_wide_notice_meta_box($post_id) {
, k2 R' {# O4 F% `9 r) p0 k if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))0 ~; N- _& g e" h' N
return;
; R5 B# x& o: S! z7 a) | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)+ m& R( t: p/ N
return;
D2 x- M4 t! h4 }; e/ `: x/ ~6 x ^; g$ S# W
if (isset($_POST['notice_title'])) {
; D M' N. S) g s update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
( v2 L P- @0 ~& E& e% A: F }
+ X9 w( t' I8 N3 J5 n% J if (isset($_POST['notice_content'])) {; f, h# X. }) |+ ~% S9 z
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
. Q/ n/ S$ z5 j8 z ~: f }
' l/ _* h& p. d* T2 M }
; n! H; ~1 I5 R" K) N, `" ` ```( X3 c' i, C8 S. b5 R( O6 q
2 W2 b8 g a5 R7 U: C* F% ^ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。" L* T5 d6 r! Y5 e n
4 z8 o" i2 d4 A+ _3 c- N: S& k+ T4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:4 \' }2 ~0 e2 V2 h. S. f. W
& i7 I: b' } Q, n m T0 e ```
6 M3 S6 @5 x. ~' S! ^" I% ? $args = array(6 x, {9 Y F% ] q
'post_type' => 'site-wide-notices',3 N) q. f1 c* Y) A% d
'posts_per_page' => 3,
; |5 P" P, Y% p- v 'order' => 'DESC',
% V$ H4 y/ c# ` 'orderby' => 'date'
# w! g" B' i6 {( M );
0 G/ ^- i3 ~5 \2 w $query = new WP_Query($args);: U. `3 G! O" d" r1 I
if ($query->have_posts()) :
, G: o8 @( Y1 F6 k( H' m U6 e while ($query->have_posts()) : $query->the_post(); ?>- D/ c) q5 s# O2 r4 T8 l
<div class="notice">
, V% o* V3 S9 X9 F <h3><?php the_title(); ?></h3>1 v) @/ r; Q6 W3 v) \9 o1 R8 ]
<div class="notice-content"><?php the_content(); ?></div>) n" ]1 i- x( p) x
</div>
0 A& G5 p5 [- J* [/ o @5 J% C <?php endwhile;6 E, n. v1 j1 p4 F6 [4 P6 V, W
wp_reset_postdata();5 E* x3 y. Y. q% o
endif;
9 J8 W$ t; o, C$ e6 P ```
' _% }$ U C7 z6 t9 @" M4 @: K$ ^& S$ w, Y1 g! ]/ _
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|