|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?, n6 u& ?/ Y+ L' z
: G& w5 m: J7 P( N如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。2 b# w6 {; ]6 w" M# y- C. S3 L I
: t4 A% g9 s& E4 ^7 Y6 _以下是创建自定义插件的步骤:2 }- K. o0 n5 M* n0 B) [
5 X& J7 l. \5 N- Q/ Z# C5 L
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:4 f$ [+ j, s0 _2 x! }+ L: H. T
w `% X1 R% }5 n' [, {4 E
```) |; j/ H# X. d5 p4 y# \6 @8 R `
<?php) ?) c# {+ S5 W1 K/ u H& H
/*
5 @1 m( @" X# }% E0 U+ {% ~ Plugin Name: Site Wide Notices Plugin
- w: G0 r( W t$ N6 n Description: Adds a new custom post type for site-wide notices.
, H2 L- S' A4 f* T% d Version: 1.0
- G. ]: A7 u& P1 n& d Author: Your Name# w: D4 J, Z- k- i
Author URI: http://example.com
- u8 t, h* b* A Y9 r7 W5 D */( B; {5 T% w/ t8 M4 a) @$ ~5 Q
0 c$ X. P6 x0 r4 g5 x6 B" ` p( A; \
// Add plugin code here...
! u# I) q7 q7 ~2 E: \6 X9 i ```% |; T" L# _: ~: X# c% @5 t
( m4 ?3 d f7 U% B+ E$ y. J( h
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。8 U* L) O$ ` _
. [# e) g. l* g) i+ {
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:# B3 D- p/ v5 J0 o8 J
; j' g7 V6 q* S3 O% A; Z
```
+ U" o/ }$ ~. o. P; L) @" H add_action('init', 'create_custom_post_type');
0 W3 I4 i( j$ z$ J function create_custom_post_type() {: W0 F" `. S, }+ h
$labels = array( r" v: F$ ?4 l! g* f. ^# G$ F
'name' => 'Site Wide Notices',1 j% D4 d5 C: c
'singular_name' => 'Site Wide Notice',
" J* [" n7 F% Z, f( Y0 N 'add_new' => 'Add New',; `6 i. v3 C: J
'add_new_item' => 'Add New Site Wide Notice',
3 _" a0 [- c9 |9 d% B 'edit_item' => 'Edit Site Wide Notice',
0 _) v a$ {$ F7 S! M 'new_item' => 'New Site Wide Notice',! o/ a, i- B9 ?+ [# J B& S
'view_item' => 'View Site Wide Notice',
4 x8 o/ q. c% d: b. x 'search_items' => 'Search Site Wide Notices',
$ r' H- x* O0 ]5 y 'not_found' => 'No site-wide notices found'," S6 m. N! s# D( O0 J2 ]
'not_found_in_trash' => 'No site-wide notices found in trash'
* N0 v$ f& u4 Y* N9 V# G0 q+ a );
; c+ U& w+ c! J9 U4 f' A
! B7 K2 r( j# S3 w) _# D, @ $args = array(
7 ~* {6 A q2 D' K1 w9 P; ^0 q 'labels' => $labels,
$ E, K8 v" v- R 'public' => true, k: K$ a/ S% U5 n0 A7 @0 X: A
'has_archive' => true,/ ]5 L! v+ e2 A: u
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( g* s; X2 D" A9 s! H
'taxonomies' => array('category', 'post_tag'),
' s, B$ m \/ A2 L$ A 'menu_icon' => 'dashicons-megaphone',
& M' C7 ~ s% C, i7 g: H4 b; b( p- j' L, k 'menu_position' => 5,3 W2 r+ ?) [! H
'rewrite' => array('slug' => 'site-wide-notices')
8 _/ v8 g3 }5 O0 w" l7 O5 { );
8 n( T, L% n1 i4 M1 |4 V
A M. L1 a5 V @ G, z" y( Q |% H register_post_type('site-wide-notices', $args);+ d0 t! T3 G0 E2 @9 Q6 ^
}
7 r: P+ e6 |. }% ^ ```0 O( c' B" ~1 _- [, e& |8 m3 o( ~5 w
8 G9 Y0 _, e" O, Z; z/ v x8 r 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。+ j; v- l$ n% {% T" x
% H6 @" g. s9 W3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:/ q0 R+ g$ t+ y. ]; Q0 A
$ _5 v0 J* \( v+ x9 U
```( ^% C9 M1 o# t9 ]& B" G0 H
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% [% k$ ~ R) g
function add_site_wide_notices_boxes() {
B8 V4 M, k3 ]0 W. T add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
& K+ D, E: _; v. a: ~ }
% X) i+ |" t+ H9 |9 _. {* ^% \1 Q& c) p% W( n
function notice_details_meta_box($post) {# H$ e0 q0 a+ f/ f: \2 \3 `0 _2 U3 z; b
wp_nonce_field(basename(__FILE__), 'notices_nonce');+ l1 S% @5 c- Q0 H7 i
$notice_title = get_post_meta($post->ID, 'notice_title', true);
) ]. w) |% J2 f) F4 z" z $notice_content = get_post_meta($post->ID, 'notice_content', true);2 F( ^0 L* p7 F/ [
?>
7 P, @7 X! S4 I* J I: _% z1 x+ T <p>
6 M5 c0 b# E' m% }6 H. j <label for="notice-title">Notice Title</label><br>7 e& F3 Z2 I9 s4 o. J5 U
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">7 p t3 I; F9 w3 n9 \6 l( T4 ?5 m* z
</p>4 l" X/ A9 G! u0 S& V/ u
<p>+ u4 p, X) K. c- E8 O @) b+ K' m9 E
<label for="notice-content">Notice Content</label><br>+ [2 z6 D0 S% V- N- r$ [- \
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>- w" n& y6 k! [( x
</p>/ Q) x) T W4 e1 i
<?php! z2 k9 a7 s9 @/ t4 u. p
}
9 ^# i) S& m; l0 L+ ]- A, j& g1 p+ I& b5 Z, K0 q7 }: M5 Z' l
add_action('save_post', 'save_site_wide_notice_meta_box');
6 s$ Z7 O! |: d1 p7 J G9 _ function save_site_wide_notice_meta_box($post_id) {9 m: q: G0 z, a1 A- w. L& \
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
. u- l4 q) u7 f; |% {' K5 s- `$ W# v return;
& Q$ Q# k0 k$ d0 b' p8 } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
. n: }' h' E n6 ~ return;
+ q" F7 c5 s7 W, ~3 u7 [! ^7 ]" p9 h6 i7 k$ e
if (isset($_POST['notice_title'])) {) e( P% S* [" i+ R! C" ?
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" }! y% R: X9 Z( z) h
}3 ^% J$ Q( C3 k4 t
if (isset($_POST['notice_content'])) {
$ n, z4 V/ j, v. H/ k update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));) Q- J* g$ e; r s/ F
}
2 X& x0 c R- y; p7 J9 G }
0 [. y2 B; P& O) \& I ```- A3 b4 _8 @+ O+ d) ^/ P
/ R* o E H$ o1 d
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
: o, a( M: A( G! `9 A6 n: L# ~
# X W. u% f" W b5 `# [4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:( J C$ N4 F0 T" K$ G2 K
, Q; J3 x! z; ^, v' f" ^- W; w- d7 [. `* J ```$ v& k! c. q$ P. q
$args = array(: l, P2 `3 ~* ~5 k" y: |
'post_type' => 'site-wide-notices',
. w% n. S- L& q7 S1 a( w9 E 'posts_per_page' => 3,& X! M8 K7 K# g5 m% f! a
'order' => 'DESC',
k: o, V+ ~+ z8 d1 P0 E 'orderby' => 'date'
* v& y" X1 M3 a+ y6 J* C );
4 H( \/ i$ x6 B( G- a $query = new WP_Query($args);
/ `. L3 e# p" I6 i% m& _8 p3 N if ($query->have_posts()) :
3 m4 Z2 r7 z4 E' g4 Q' n! N; ? while ($query->have_posts()) : $query->the_post(); ?>7 l5 K2 H" {/ [: c. b3 F
<div class="notice">
2 d' H+ `" b3 M$ G4 t+ H <h3><?php the_title(); ?></h3>
& L& L! A) l! c8 e6 T1 [# k5 o <div class="notice-content"><?php the_content(); ?></div>
3 H8 I' i; K4 T+ T, p9 \ </div>) ^# w% C* L4 P) J7 V! X7 G) w" ?
<?php endwhile;) U2 Y* \/ r1 H% a
wp_reset_postdata();
: B$ i: Z+ W9 e% Y endif;) p; q/ Z! Q/ j! C& x1 R
```
# \ |. v# A! N& W, W8 M0 ~3 j
3 H( D. W! i5 @" M. @ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|