|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
) g+ M. P2 } ?9 Z! ?, x9 W" p# l& ^7 ]! y( y6 H3 |
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# c- y; ]6 B( t( b$ d- h; X
0 K2 q' L! V, z! f8 `
以下是创建自定义插件的步骤:5 ~# [1 I" c- n; n. `- e
; s8 i) V' o: ^! }2 \
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
/ @& w$ Z e8 @9 @- y4 e6 K, D4 ^' g& P1 u, t% a" {8 B" [, u
```
" m5 n4 A( N; L7 p! T <?php
2 A! M7 x( D7 M; K* `$ B9 K /*2 y# N6 d: U2 L3 R
Plugin Name: Site Wide Notices Plugin
3 n- m: m# O, E' U8 E: a& v Description: Adds a new custom post type for site-wide notices." k! U. V' ~0 q( ?. N: s6 N
Version: 1.0
4 W! d5 m# E/ a+ ~5 A Author: Your Name
3 P& a4 D# {3 L Author URI: http://example.com
2 _8 J, F9 q1 t- g' g */: Z ]# p7 j- @. C
8 \% `' Y7 g9 }) `' l, ^* v // Add plugin code here...+ H3 f# d; D: n0 G8 B" f' g7 }
```/ c; u; B, Z/ L: U
; R) X' a3 {) z- A- e9 }
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。: l; L8 Y+ ?2 B4 I
" D7 K6 ^0 w. _" P+ C
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
* A: { x+ i! O% T+ w+ Y" I/ ^2 R/ K. o( [0 x8 N/ R
```
9 p; g3 T* x( y add_action('init', 'create_custom_post_type');
# d9 I! I5 [: q$ g6 S! f function create_custom_post_type() {
0 x8 }: c) o4 K8 r1 Z6 _5 t3 ^ $labels = array(- k h4 N* j+ w1 w
'name' => 'Site Wide Notices',
* O/ c9 R0 B( K# a: A! G. @ 'singular_name' => 'Site Wide Notice',0 [( q; L$ M1 @( V! l. X
'add_new' => 'Add New',
" ^8 G! C# U1 d0 P( f1 N6 H9 t 'add_new_item' => 'Add New Site Wide Notice',1 @7 s i' y. O( ^" Y* S) ^
'edit_item' => 'Edit Site Wide Notice',0 l1 d) G' `& A1 ?6 t( s; t6 k: W
'new_item' => 'New Site Wide Notice',4 U. G5 C4 Z: Q9 e& ^, d% B% K9 G% H
'view_item' => 'View Site Wide Notice',! V( `. K0 c" [0 q# j
'search_items' => 'Search Site Wide Notices',) }: F! v- b& q* f
'not_found' => 'No site-wide notices found',' Z1 e+ }0 k9 ~5 G! E' K/ q
'not_found_in_trash' => 'No site-wide notices found in trash'
4 t0 B* f. d, |" @& B" M* D );
7 k/ g4 L) j5 |# M. g5 Q9 j
, U7 T9 |% g) X8 s2 t $args = array(
- Z) R+ C- h$ D$ {8 z 'labels' => $labels,. o4 l0 H0 N ^( s* z
'public' => true,1 `% Q2 @% }3 t1 `% q* X) l2 \
'has_archive' => true,) j, a. q9 ^0 B
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),9 F0 y; `! Q' \, l' T" y- k2 {2 p0 d
'taxonomies' => array('category', 'post_tag'),1 I& ?: ]) \: _, S
'menu_icon' => 'dashicons-megaphone',
( b; g# c' J' @5 {# @ 'menu_position' => 5, o9 [4 a9 g, _4 X) A- V- m$ a
'rewrite' => array('slug' => 'site-wide-notices') E1 H" M) L! n4 t% }3 @
);* `4 H- c! U' B
' i1 t$ m$ a0 `
register_post_type('site-wide-notices', $args);( @- k. O( I# h- @4 z K
}" }! m' p# S, \: J8 E$ H$ }
```
' q( }& h+ k4 B7 U6 @; [7 I+ v }& I) l5 T
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。% @4 @7 `( n M+ A( b1 H( j
f% i( s# m/ o. U3 z5 ]
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
- ?& O" r4 k6 A( s: B. n
' i4 X, |7 q# n3 D! O. } ```' _2 _' v( z; K
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
/ {$ t7 g9 L( m: A function add_site_wide_notices_boxes() {& S* S( [) O+ t" E( F
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');; i: W6 A6 U0 n) E/ a: N; w* u
}
. U6 C6 A# m' K, O4 E& r5 t+ f% Y6 w7 s& r, a
function notice_details_meta_box($post) {
1 y7 h+ G. R! q2 B# T& q. @ wp_nonce_field(basename(__FILE__), 'notices_nonce');
# H& X0 @- k# u $notice_title = get_post_meta($post->ID, 'notice_title', true);3 ` R( X$ K) _; S
$notice_content = get_post_meta($post->ID, 'notice_content', true);
8 o9 q# R% ~- S/ C( I* P! z- ` ?>$ H* n" r* h7 G2 r
<p>: A/ A/ l, B' n, M
<label for="notice-title">Notice Title</label><br>8 X5 p |/ ^/ g6 ~/ k5 B
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">" ?$ g0 n0 O9 p% c N0 s
</p>* [- B2 K }9 d+ w/ O3 w
<p>
8 ~: f7 B$ \, u& W& r* v# o <label for="notice-content">Notice Content</label><br>6 } ]) B4 x9 t/ |/ `$ B: b
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>1 b5 x3 ?8 ?' d: P+ q
</p> F- f% j) |! l, }! I& o
<?php
2 O: |' R; s/ h! t* k3 v0 T }8 p0 y& H. Y6 R4 \" ~4 Z, s
. m j. ~4 ]7 P
add_action('save_post', 'save_site_wide_notice_meta_box');& t/ f( `6 w+ P) H6 M4 L* y
function save_site_wide_notice_meta_box($post_id) {
5 Z) M/ L C/ B$ i# |. j if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
- h4 p% E, f& ] b n return;' E6 ^$ d3 L# [# j0 H2 _0 P
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
9 s: P9 M9 X" [: e' k return;
e& J2 v; M, y: W8 p' i% F& t$ J/ j8 l2 W) ]1 d+ f' O
if (isset($_POST['notice_title'])) {8 k- m9 }: J" X E
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ d! ~) i, c) V4 i5 W' O. ~
}, C7 j0 C6 E+ h9 H$ b
if (isset($_POST['notice_content'])) {
0 K" J6 i$ T$ w& S update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));% m# I/ s$ q! g, O+ y# f7 q( r
}
% c8 s$ J7 W* s: w; @( g }4 {6 G( t: c# Q& J$ H! l! K' F
```1 z) [# x, ^# r$ O u
5 l2 r3 f' ?! ]. W% t T' M
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
- B3 k. f2 k, K6 D' s% b
9 l& V" C4 @3 d. L; w4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
8 _) C/ o! f8 O# h) _
6 b* w9 F' F/ w# n; D& B ```1 Z$ o1 ^3 p$ i" X' R9 {. i
$args = array(8 }& n; p0 h+ [& P9 O, l
'post_type' => 'site-wide-notices',( J; o* J, F/ C# O3 o) i+ y
'posts_per_page' => 3,, I5 i# C* J7 r! m& n5 m
'order' => 'DESC',
, A+ J/ ] e& B8 R. z! r 'orderby' => 'date') @" C0 G$ }. a4 W% m! Y( A
);
: l3 _) L) {/ @- B $query = new WP_Query($args);# t }- [- W- F8 A0 D) j
if ($query->have_posts()) :
% l5 w0 r6 ?( r9 P0 r1 g while ($query->have_posts()) : $query->the_post(); ?>
0 P+ v1 |; a' | <div class="notice">
) K* ^4 N' U+ B <h3><?php the_title(); ?></h3>: W" y* o3 `) S$ e9 @% ?" E
<div class="notice-content"><?php the_content(); ?></div>6 O. D% |3 O% H! M
</div>
9 T6 }7 R8 n9 X% B <?php endwhile;# F, v# w: T" o0 W0 D( b4 p1 P% G& W
wp_reset_postdata(); P, y- s( i! T' e' W3 E
endif;4 X3 { X P3 B4 r$ Z. N6 ]
```
8 D7 v: Z7 u) J8 X
+ T: H6 j& w |+ i$ q' q# K 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|