|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
- ^. _' B8 E- c3 _$ g; z: L
3 w( b$ c! _& i如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
8 _ c; `& j; t7 i
, N9 h' A" c5 A' K- p/ H# p/ ?0 c/ T3 z- P以下是创建自定义插件的步骤:
; o' w, I- J" e8 F* v8 m [
4 k2 \7 b$ o) V) A+ A1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:/ H9 B7 s% a# y) s8 p* g5 ^
7 z% D- K0 j8 @' a) S
```7 P3 v: U8 U! Z8 S
<?php& j/ F/ c5 p% c7 M \: o+ }) ^! i8 }) E' V! P
/*
1 x6 k: Q% T9 F2 c" b4 ^( B Plugin Name: Site Wide Notices Plugin/ `0 [4 e3 C4 w9 ~
Description: Adds a new custom post type for site-wide notices.
5 }, h& @6 h! Z+ E: L Version: 1.0* J& W- H. K! A
Author: Your Name% t5 O0 a$ A# m6 @/ l
Author URI: http://example.com- ^3 I0 s$ C8 X8 P) Y
*/+ }! C- Q% @8 H% w0 I/ l
3 V: w+ s/ @4 P7 _ // Add plugin code here...
/ F7 @3 \! w* p5 U; t" N ```
1 ]7 T$ [* Y( J2 _ r. f5 @
6 R* R, ^$ u% m: w% L. m 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
+ @- n' @6 W/ D ~
! ?3 ^3 a$ ^# g: T; L+ C2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
: i) I% ], x+ p1 O4 Q4 w* M
, N, O4 p8 ~: ^0 t4 \2 D. P ```
' a0 Z, K$ A9 e7 P' Z) r( ~ add_action('init', 'create_custom_post_type');: v8 \5 |4 Q& U
function create_custom_post_type() {9 f W' n' S$ o1 C P4 ~( H# ^
$labels = array(! v" F2 B2 J5 ]6 a9 Y( P
'name' => 'Site Wide Notices',; K& z! b& x0 J6 a: w
'singular_name' => 'Site Wide Notice',) _# t: u, g# n+ f3 Y# z
'add_new' => 'Add New', ^- u" C0 d9 V2 P/ R
'add_new_item' => 'Add New Site Wide Notice',
. `, r; ]' E8 }( s 'edit_item' => 'Edit Site Wide Notice',
$ ]! [4 R6 Y; k 'new_item' => 'New Site Wide Notice',$ z5 }% |6 i( ^+ M0 n3 Q
'view_item' => 'View Site Wide Notice',* `* B8 }. U- _) G# h: f( q* t
'search_items' => 'Search Site Wide Notices',
8 Y2 H2 f2 i7 \+ n* ]) Q 'not_found' => 'No site-wide notices found',! m5 D+ W$ E6 p! p
'not_found_in_trash' => 'No site-wide notices found in trash'7 w8 Y% u1 \1 q/ g- N/ z
);
9 G9 |' X& h, r7 C) W; V7 {
! V: S, V, |. D! C; Y $args = array(! u4 G. ^/ V+ ]$ \& J' }5 [( ^
'labels' => $labels,
# `( E, P. `# Z k& m- p; q 'public' => true,3 \) J+ `0 m* D$ m, N
'has_archive' => true,! ?% c$ L# d8 P, C/ o; @' C% Y
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
! i, ]: @- K4 ^ e 'taxonomies' => array('category', 'post_tag'),! q4 ^% N1 ?1 ^4 A' Q% S; E
'menu_icon' => 'dashicons-megaphone',
3 c5 P" N, a* \& l) e( ` 'menu_position' => 5,, y' y4 L9 W- ]8 R/ y5 B! S8 o
'rewrite' => array('slug' => 'site-wide-notices')
2 E1 ~+ |5 J) t& r7 V );1 M# A \* p0 h2 [8 s$ s
, [; I [2 [7 [1 {0 L1 ]+ `3 Y register_post_type('site-wide-notices', $args);% s0 e9 @- ? \ c1 V9 @5 e" v
}
% a, `/ R& R) j8 b4 M/ Q" T2 f ```
5 w7 u5 D0 g# r
/ H1 e; I( @& b: C) K' c- ` 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。/ U1 R( @- j) W4 T: D. ^
+ o6 [2 ?/ b4 D% l+ M& u3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ X+ @- p/ [/ S# @! ], A
- {5 [8 B( q0 W( J ```# @0 S1 ^5 X5 O4 Q% }) Y7 b+ X! w
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
( G0 m% T3 j; J/ Y0 v# L function add_site_wide_notices_boxes() {
9 {* ~1 R+ @# f7 h& l( ?8 `0 H2 g- ` add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
1 Q' r- z* ^; v# T# V- U }
# |+ w) B. O+ V% U3 l. f! @3 @, M/ {% t# Z4 v! X
function notice_details_meta_box($post) {* M# R. l4 @* c( c; W! f" b
wp_nonce_field(basename(__FILE__), 'notices_nonce');
3 Y- U- b9 {. g7 z: G4 g3 ` $notice_title = get_post_meta($post->ID, 'notice_title', true);9 u) c( r# L& O
$notice_content = get_post_meta($post->ID, 'notice_content', true);# e0 k3 N. T* q' R4 S0 Z. P5 d
?>* Y7 J( K4 l+ ~; L1 K h
<p>2 E; B: o/ A, x1 q1 K# H8 X' \8 p
<label for="notice-title">Notice Title</label><br>
, \8 T9 [" T5 {# s% q# W <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">' A9 u: r- r& R% Q, V, L
</p>3 W$ c/ k1 F4 V) A9 ?, |! i
<p>" y) `, z! t! l0 D8 Q- v
<label for="notice-content">Notice Content</label><br>$ R' B, K- p# o5 h
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
' P2 t4 }: W7 I: v) C G8 S </p>
" a D/ Z" d5 a6 M- u. _/ e <?php
( }! x6 s4 j* [) a' F8 h }2 {7 e) K! e! j) U6 J: I
# Y+ U7 s+ \% S
add_action('save_post', 'save_site_wide_notice_meta_box');5 h$ f7 b) M: T1 |7 Z; t
function save_site_wide_notice_meta_box($post_id) {$ O5 B* T7 T# [2 v r. g
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))" u. L7 U2 @: Y3 [+ l
return;
* ?$ F; X2 A7 V+ T% w if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)3 A% ~0 h- o- I8 N! A" X
return;/ p4 Z7 M7 ^3 F8 |0 q, S% N
3 W* ?+ x8 Q ?9 K* W% `
if (isset($_POST['notice_title'])) {
' K2 |7 Y( u' I- f( J update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
( C: P, l; A' {6 Y& \ }
/ M' n# N& ]& q& I if (isset($_POST['notice_content'])) {
$ f4 t# F4 d0 h update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) ~& B9 w' D' X: }" J }
5 Q/ i8 r, B' C; q }
5 i2 ^% |5 [0 @5 w+ p ```
! s% a% h. W; M9 n. g" O1 F0 A, h& {" x G. }# E+ p7 F l
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。4 F S2 N% w) `! a9 m
9 g g) w8 a& Y( l `2 n' h
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
& q. U. {8 F6 f% h; ]& K( L4 L2 K: n6 m
```0 h4 T R: N( p: }
$args = array(
/ I* L# R6 m4 h! E# w" K! `' T 'post_type' => 'site-wide-notices',
+ K* R5 l- \, S 'posts_per_page' => 3, l% S6 y- ~6 g+ j9 v/ Q( |
'order' => 'DESC',
1 V i3 y, O8 D! m3 @" u! t 'orderby' => 'date'
2 s+ g, f9 {# g7 h) x9 T( } );
R. M$ U4 E8 j! @/ M+ L $query = new WP_Query($args);7 Z1 N* q/ R0 A% {
if ($query->have_posts()) :
2 W2 B' f) a1 I3 d: q while ($query->have_posts()) : $query->the_post(); ?>
- a' x: n: C- _ <div class="notice">
8 ], t0 o, R- r <h3><?php the_title(); ?></h3>3 \: d6 z& i4 m) E% T
<div class="notice-content"><?php the_content(); ?></div>
, i) |# b B2 W% G </div>
- w* Y5 [ d% B- U <?php endwhile;" [1 j/ F+ J4 q( S3 }5 |% [8 Q0 E& t
wp_reset_postdata();
1 [, I% f5 ]4 M6 h+ _+ ^! | endif;
9 _) f5 d/ D& l4 n$ T9 Y7 b ```4 u$ B7 R) A2 l' ~
% C+ M3 |' ^5 L3 c; e# G' S x6 m 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|