|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?( U9 G& s& e1 ]
/ W# w0 L, I+ d W3 \如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
/ ~1 W- S& p' |& k/ N/ W! l/ N& Y7 e- y8 E
以下是创建自定义插件的步骤:2 `' I0 H/ p/ q& V: V
8 e, i6 K& J+ r' }# I
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
5 t% L! J! n$ D; w3 T- }0 B$ t9 s+ q- P. Y
```
! H6 h4 R, H% h; A2 X <?php: b: G1 n' r& X; T+ Y
/*1 @+ {- Z6 k# T
Plugin Name: Site Wide Notices Plugin
5 @0 x1 u- g# C# ? Description: Adds a new custom post type for site-wide notices.
$ v4 w4 \, ?4 L) g+ q/ m Version: 1.0
8 R \# y. x# |0 y& ^6 s! J3 v Author: Your Name$ }, |4 \1 `/ q& D
Author URI: http://example.com7 @# |/ c" v! _
*/1 R8 y0 K# v- h2 _# w" O
( K+ a6 M( H8 i // Add plugin code here...9 _! c& p, s \ E. }4 p& ?
```
! ^1 i/ Z. y0 W# o6 `- n" h4 f, j3 P Z0 _7 ^
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。. x3 ^ n O3 Q. g
4 p" n5 ? ?+ N+ m5 f) i1 D2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
5 c1 p5 H3 B$ Q! {! ]! X
0 o+ ]: Q2 d! V, @ ```4 `- C v( A6 }! _
add_action('init', 'create_custom_post_type');
1 \2 M2 U5 ^ q1 t* o+ f function create_custom_post_type() {3 j% Y3 t$ R0 G2 D! n9 t! H/ K
$labels = array($ F9 F- Q# o5 O: d+ x4 @
'name' => 'Site Wide Notices',
$ ]4 u% q+ [3 y, H 'singular_name' => 'Site Wide Notice',
/ K) W% N% ]" d 'add_new' => 'Add New',) r& `/ B; u. m' n1 w9 I1 y2 r- W/ B
'add_new_item' => 'Add New Site Wide Notice',
" c9 r+ o z+ D. s) T! l, S 'edit_item' => 'Edit Site Wide Notice',
& i5 e4 a0 K8 C# ^, \' T: G I 'new_item' => 'New Site Wide Notice',: G# U7 X( k. q% l2 ^8 R, E$ n
'view_item' => 'View Site Wide Notice',# O; g) X: [/ v. q' O C
'search_items' => 'Search Site Wide Notices',2 V8 P2 ]" \; K; E5 j. v5 x$ x7 ~
'not_found' => 'No site-wide notices found',
% x/ ?: I( p3 d! }3 U 'not_found_in_trash' => 'No site-wide notices found in trash'" S! x3 l h5 @
);
# X& @* r8 _0 Z1 E6 x3 o( ~( L4 p% ?3 Y H* | s/ r; X! a
$args = array(
* E, p; y! j9 D% B+ K 'labels' => $labels,
, x y& N, t- d3 K( H! a) k 'public' => true,+ j6 _) E9 q V, k+ x+ b4 x) a
'has_archive' => true,9 v/ H; ~0 ~2 J$ T2 r" T2 A% B
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),! s. v2 w8 Z% k Y3 X
'taxonomies' => array('category', 'post_tag'),
3 D' c! o" W; T4 s3 N( B 'menu_icon' => 'dashicons-megaphone',( J" S [! `* ~5 J$ ]( U0 i% D W) j
'menu_position' => 5,7 P6 M2 p; s+ f c1 I2 K; X
'rewrite' => array('slug' => 'site-wide-notices')
/ L) t9 H& K1 |" b" x" X );& k2 q* F7 V6 N+ h. m/ P/ w5 F& O
# U; q, x) t2 t: t
register_post_type('site-wide-notices', $args);
$ R& V; t' {6 d: R; t8 d }" r6 i9 j- d! Q9 s
```
0 |+ m1 e, t; o+ R/ M. `6 }( Q: ]( v, `9 }+ N; z8 b
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, I5 |7 u/ t/ A, J+ T
/ w/ V, T+ \% w' A2 m$ Z
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
6 w, `, u1 G* j# w: U; E
' ]4 h5 r B t: v% n8 q& B ```
; X% H" }9 s2 x9 [% M! \6 w add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
! ?7 p- \3 N7 _2 a function add_site_wide_notices_boxes() {1 d& ^2 X1 {7 v% J) l, N
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
, z9 u1 C' e% u! c9 } }
) h$ k) h- N% @6 U: F" a4 N$ H2 X# T0 B1 N+ n h f& n P
function notice_details_meta_box($post) {8 t# {% ]/ G' q5 L/ l+ w" x0 r: \( b
wp_nonce_field(basename(__FILE__), 'notices_nonce');
, T- P, D* U% T $notice_title = get_post_meta($post->ID, 'notice_title', true);
$ |4 [2 H; M8 J- X) J $notice_content = get_post_meta($post->ID, 'notice_content', true);
5 P) ]9 p/ I2 h7 R/ V) A ?>
2 s" M ?- @6 l& a& [# f <p>/ y1 X+ ~, b. {0 V
<label for="notice-title">Notice Title</label><br>: l- F4 C, q/ X
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">% m" \3 w! X' k6 M
</p>
. |3 P5 B' Q! `" ]7 U: |! ` <p>& |3 c9 Z4 f0 ]& L2 G
<label for="notice-content">Notice Content</label><br>
F# u `* L" F! \- G <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>1 w! c# T0 {" X% M
</p>+ I0 a+ P1 u& I
<?php
9 } D" z8 M3 _ V8 {3 Z }
{3 X" Z6 b' \$ F; A y' S7 b! t: m8 N5 D& o, G- P% X( s8 ?2 Z! C
add_action('save_post', 'save_site_wide_notice_meta_box');; {0 D" v- C& `% i, M6 o
function save_site_wide_notice_meta_box($post_id) {' `, f9 k* o) I2 q9 M* w) x2 t
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
3 a6 s1 n$ {* [; `1 ^( ` return;+ e c- l6 B2 f- A
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
8 N8 _! m) ?; k: ~! S* } return;3 R6 g' k5 }& s" r3 b- i6 F
! w3 B$ G, h1 _4 ?5 x0 `# M if (isset($_POST['notice_title'])) {1 ^6 d& Q0 a2 h5 ?
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));1 j8 i1 F- g5 u# J$ L W7 M0 b
}3 w& D5 h# F" a3 X
if (isset($_POST['notice_content'])) {" s; v' p) O c* j* B
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));8 C0 l. X: D+ ^! W$ d% Q
}
; n; w7 v5 C0 h* t }6 B4 g2 `6 @ j+ @8 L
```
5 i0 Y* K* R3 `8 \( ^- I& z2 A- O6 U& N" h! ]3 D7 |7 h2 r* M
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。 x* G+ A9 y( C# L( C
) ?7 ~& x* C9 R; n2 O+ W
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 O3 i" V9 f7 p; j+ ^6 C6 A- m
) v* C ]- @- Q; `$ I) f7 ?! } ```6 J8 J5 m5 _- \. x* H8 H+ W! u
$args = array(' {' r3 M- z3 G8 p6 m# K
'post_type' => 'site-wide-notices',
. Z( _( H" B( d7 q) K$ d5 a, ?! u$ x0 z 'posts_per_page' => 3,! B+ f' Y3 w3 J% {5 _0 t' g
'order' => 'DESC',7 z/ ^6 T, j/ B* I* ]. }
'orderby' => 'date'
P! O/ p# R) X3 }( B1 r E6 O );
1 K" I1 b0 v/ t' g $query = new WP_Query($args);
F6 r0 x3 N ]- [6 R if ($query->have_posts()) :6 r" C( f; Q: W! c: k
while ($query->have_posts()) : $query->the_post(); ?>
, ?" g; c! e9 G; G# R0 ^/ j$ M5 v <div class="notice">+ k/ A8 | g% D& p# H: w1 J& g' u: u+ F
<h3><?php the_title(); ?></h3>$ O9 t. X$ h, b* a* T* s5 O E
<div class="notice-content"><?php the_content(); ?></div>
* T0 y; m. R; ?; P% ? </div>8 {- F A' R7 `& w, {! i: K
<?php endwhile;# M+ N! o: b1 R# F. }
wp_reset_postdata();
% f* u8 k5 c1 V8 z) Y5 P7 q, J% m% g endif;# X3 U( W# m& Q7 ?) X; r) N
```
' j7 ]1 O5 S J3 W6 N8 S
) U2 p3 i1 M. v0 [ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|