|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
6 f% [$ g5 v: ~, V# A: V6 ^0 j- l5 v! D: @: J* `! i$ c) M
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。$ D; ~; Q$ A: e/ I2 x M) z2 e
# C) o( m- q1 U" l以下是创建自定义插件的步骤:
9 C/ e6 T( P/ q h$ {7 q, A9 w6 B! j8 ]) ` K# \+ l
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:$ Q2 q# z+ ^" r2 B, C9 p
% c, m% q% |! {! D P ```! ~% V0 e) b1 b/ k9 X2 r1 g( R
<?php6 v2 K3 s5 x9 w/ M' W. f! Y
/*
: Z3 Z0 p' ]) r/ g( X1 R Plugin Name: Site Wide Notices Plugin- u3 B8 ~! C0 ]+ G. ]
Description: Adds a new custom post type for site-wide notices.
( ^4 d5 _/ H7 l3 |% _ Version: 1.0
% h9 e4 r" z" [% s @/ e Author: Your Name
( @+ p; B6 n. F$ [8 d/ \ Author URI: http://example.com
, m5 B" |3 D9 k */- Q! r0 ^6 C% \" u' V5 X: W
& C. r6 K9 J: P' _7 Y // Add plugin code here...
6 P: q# Q- l4 X. \9 y ```
* A) z: v( e$ @# o, C5 h6 l, Q- H3 c) V! b- s4 U" p4 ^$ n7 W/ e
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。" h0 C( j' z, l9 P' y
: Z7 s! }( }- q, L' I
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
1 x& i! Y" W1 B7 @' E* ?5 m. m6 {2 w. q W* X; t3 T7 i
```
4 [. X/ n! d9 f add_action('init', 'create_custom_post_type');9 Z4 f# B. Z i. M2 V" s
function create_custom_post_type() {
+ J" J9 c- t; h2 H2 K( ~4 @4 f% o $labels = array( {( ]# S% M' M0 a0 l, d0 @' {
'name' => 'Site Wide Notices',$ \8 c& d+ @$ ^4 \/ Y; \
'singular_name' => 'Site Wide Notice',8 w: P; m' ~3 v8 z3 p* k
'add_new' => 'Add New',
( I# d% W' o1 u, Q 'add_new_item' => 'Add New Site Wide Notice',
1 e( S3 a: k& |# i" R 'edit_item' => 'Edit Site Wide Notice',
2 a+ a& J0 A, [ y 'new_item' => 'New Site Wide Notice',
: h1 K2 ~6 u/ p4 i. P9 V 'view_item' => 'View Site Wide Notice',
0 I! Z+ Y2 [1 @& m9 W8 M8 c# t 'search_items' => 'Search Site Wide Notices',( d& r6 P9 c. d: G' o. x3 s
'not_found' => 'No site-wide notices found',: n% _% ~* T- E- D8 Y
'not_found_in_trash' => 'No site-wide notices found in trash' N! U* W3 B" T$ |5 b
);
& F3 @, o3 H$ o' d* A
) L* \/ ^, I# ` $args = array(
- E# B# n5 `. ?5 {4 B 'labels' => $labels,
5 r O) y1 o& [ |. r 'public' => true,
c3 S5 I' q H6 X1 u+ @ 'has_archive' => true,5 e; {# q% P$ D U( [" H
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( l: J4 Q8 G4 B
'taxonomies' => array('category', 'post_tag'),
) e; Z A) L1 U# L7 W) r 'menu_icon' => 'dashicons-megaphone',
3 p4 I+ ]1 j+ o, N% c O: T1 F 'menu_position' => 5,- M4 O: ?( F! `$ o* A
'rewrite' => array('slug' => 'site-wide-notices')
% K5 N* V$ y& a; M );5 Q3 w/ F: S- w% p3 f" c
+ f$ w+ k( {+ `1 |, t$ Z4 m7 x$ y
register_post_type('site-wide-notices', $args);, g; p2 z! V2 E
}
5 Q1 S+ q% Z# }$ O ```
9 D$ z4 }% L; ]; y. L7 k7 ]: b9 f5 `* j o4 x* [
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。/ M& ~- `+ F l& F) ^
! L" B! Q- S, _5 J# ~3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ _: r H- E4 h! R+ f! O3 A
6 H5 y9 A- J. ], V
```% e% x9 k* K. l% e3 }# G7 I/ q7 \3 r
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
& P0 ^! h, L @& C7 ]/ x. D& T/ ~! Q function add_site_wide_notices_boxes() {5 q* Z: ~) r1 t$ A# ]; ^* D
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');4 U" ?* S5 k/ ^) W
}5 V' [( Q: D4 H) [2 B
! O/ J: C9 a3 ?7 M6 J8 H' `5 @
function notice_details_meta_box($post) {
$ e# E. P- w# `$ J$ i; y1 _% t wp_nonce_field(basename(__FILE__), 'notices_nonce');, M! W+ v: |; N3 @
$notice_title = get_post_meta($post->ID, 'notice_title', true);
& ~& w" m. B5 d; q& G) T4 i $notice_content = get_post_meta($post->ID, 'notice_content', true);
$ ~4 \( X) N! l& ?4 o8 ^ ?>4 G) P/ Y0 T$ s: d' _0 u
<p>/ ~, G& h; E4 m/ w' m) t& f
<label for="notice-title">Notice Title</label><br>
4 W$ V. j( u% v8 {, G n8 q <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
F& A9 U& K( R$ _( a# U H </p>
2 D4 c% s2 N0 m$ V5 P <p>
8 l( R+ ~4 y0 C- x7 x; _ <label for="notice-content">Notice Content</label><br>
8 n3 O% X9 n7 b8 j <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>- M* Y$ V1 S0 v% O; `
</p>/ F2 V9 A1 X3 I% P9 ^0 x
<?php$ ?% Y P% z' B* p
}6 u) ~7 K- r1 o7 R/ {( G7 ]
, _4 D" W: ], O! j" N9 Q
add_action('save_post', 'save_site_wide_notice_meta_box');
* B0 \7 r& s- K. |; U function save_site_wide_notice_meta_box($post_id) {$ I7 V0 d' z6 y) b( c5 a
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
# n# C, u3 Q) E return;: Y3 H* e( M+ X) z7 E) Z
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
% I7 p$ p$ F3 @5 n return;, e; Y, Y4 _7 _2 o/ J
( k% @- N8 q# N% ^
if (isset($_POST['notice_title'])) {
8 d, a+ d+ b; H2 M update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title'])); W0 {( a: @7 P3 S; }2 w
}0 b( J, E" \0 I: x! ?
if (isset($_POST['notice_content'])) { t3 K0 K, h$ Y( {" T# W/ Z
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) N$ e* l2 \: `' M1 ]% h6 I6 ~9 M }
. b) T2 K6 \' l8 P8 J$ Q8 ^' O }0 K4 p4 y$ X# L9 Y
```$ m5 Q1 U1 h7 H% E
- _% U) R% n! R0 u3 F* B* e
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" |* Z" v2 _# C* N! J# `" @$ g- i3 S4 l2 m& {5 M# B
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
) o. ~2 i0 C3 |/ c2 x4 Y
% J& N8 Q, G+ _5 S ```
7 }. e" g6 o+ S# }! h4 | $args = array(
# S5 D2 k! H. P7 u( h. u 'post_type' => 'site-wide-notices',
4 s$ X6 t- d- E7 i. m1 r. ?8 b! O 'posts_per_page' => 3,
& c0 l- @) @, I n) k: M 'order' => 'DESC',1 N& n5 W* V3 z
'orderby' => 'date'% k; ^/ i3 K' g1 n/ B; s
);: D/ P. @2 `8 U% p# r5 Y) ~7 a
$query = new WP_Query($args);! D( o* m6 B. {! m9 Q4 x( g/ Z
if ($query->have_posts()) :
/ h# m- E' k; b2 | while ($query->have_posts()) : $query->the_post(); ?>
! m- E4 Y. I# t! w4 C4 r8 E <div class="notice">- i7 @3 y& ]) J- N; x
<h3><?php the_title(); ?></h3>
+ S( U& R |6 q3 l$ s6 O1 Y <div class="notice-content"><?php the_content(); ?></div>5 k; p+ g6 L# ^! P* W: A1 A
</div>: ~! k8 l9 D! |0 f9 Y
<?php endwhile;) m. m: [ v( E& t! K
wp_reset_postdata();
/ p' R5 M- A( T7 i+ s) s endif;* I! U1 }" h1 P! m
```* P4 O7 N' x( ~0 ?
. e5 x" g N* a, Z+ t; ^8 i# \/ `
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|