|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?; L/ P4 T2 z* f/ i2 D- C- w5 t# l
5 Y/ ~5 \" N7 z8 I9 _
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
" _3 T: k0 G2 ~ w/ x, q* }% c& l7 E6 m+ u; m) }! ~4 l
以下是创建自定义插件的步骤:; `* p, W. U( \2 v2 ?
f# b8 N# K, a8 \+ B
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如: ~' [3 m* |; K8 l4 T
8 B3 S7 E# u+ U+ Y
```
7 o+ d Q- {' A1 a1 j2 y <?php7 c6 `% k" y2 @; S$ S9 K
/*
; D: ~& V* @4 v6 O6 q( n9 z Plugin Name: Site Wide Notices Plugin
( ^; U2 x4 U. G0 v1 S Description: Adds a new custom post type for site-wide notices." _" n7 M* l1 `" T4 M4 a B! \9 g
Version: 1.0
f, o2 H% d9 A# Y$ M+ j' i( q B Author: Your Name
! P% B9 ?6 @/ r4 _+ |, m, ?/ y6 d% ~" a Author URI: http://example.com1 S5 ?# e& i$ E% A1 v- v. F
*/
2 W$ f( {+ |$ x$ S: y" C- u
/ w: g% R9 |+ j8 [ // Add plugin code here...: _0 I7 D+ \0 i& u
```" ^) \& b! w L/ `
# ?$ a8 H1 F$ l
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。) Z; \2 X4 H9 W. ~" Z/ a
, F$ ]1 B0 f& J2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:: |, M: h. U7 p$ h' r# l
4 @# P9 S5 d6 C2 F& d! a& N ```
, R8 A1 b7 Y4 y add_action('init', 'create_custom_post_type');+ d) L$ q( g @* }' L
function create_custom_post_type() {
5 R, ~1 F5 ~! w& l $labels = array(
6 l4 z7 B) j- V6 ~ 'name' => 'Site Wide Notices',: ?1 \: C# V+ ^) n( s5 _- i
'singular_name' => 'Site Wide Notice',
9 F2 z- v. w! e, E7 y: V 'add_new' => 'Add New',
* Q W. V2 ^7 l+ {4 \0 j5 F 'add_new_item' => 'Add New Site Wide Notice',! q- M/ u. U1 s, H" E$ n& p: p" l% L
'edit_item' => 'Edit Site Wide Notice',& N0 S9 _$ L+ J" Y
'new_item' => 'New Site Wide Notice',
# j+ O1 e \0 _; {# P 'view_item' => 'View Site Wide Notice',- l9 N' R/ @2 }4 z! Y
'search_items' => 'Search Site Wide Notices',! f8 Z& r+ J+ K+ s3 T) A5 C. E
'not_found' => 'No site-wide notices found',
7 I* r6 P5 f$ h% n 'not_found_in_trash' => 'No site-wide notices found in trash'
6 R- _5 E5 |. H8 Z; i2 B: [( U. q );
5 S% [ u0 b1 k! I0 [9 T0 L0 ^9 H/ m7 ?
$args = array(
9 i! o$ T1 M* S7 [/ x 'labels' => $labels,
7 y0 {' x9 K0 ] F1 o 'public' => true,
, x+ Q* E+ K/ p0 X 'has_archive' => true,3 U. m- z$ r+ I8 u0 B
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),2 B+ D7 z9 h- z# q) i
'taxonomies' => array('category', 'post_tag'),
) H9 H- U0 y5 [! h9 {6 K9 x) Z# e 'menu_icon' => 'dashicons-megaphone',
" m$ o, u {+ x8 q, z C1 I8 }# y 'menu_position' => 5,
) X5 ~1 `% q# k; c5 |# [! { 'rewrite' => array('slug' => 'site-wide-notices')) | v! D' e% A" \3 @+ {
);! |: `* D, z) i" V8 ^9 |/ o
% R; J8 h( E# P4 h5 n, w2 s7 b- L
register_post_type('site-wide-notices', $args);* P9 V3 {& D, n0 B
}
* S: ~/ ^7 f, l ```$ @* r( u4 y0 X, i; h$ u1 c( @
( W6 m- p4 n3 J& K9 P& Y
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
0 b) N8 E( j/ K+ a6 Q% Q2 E5 X9 B' J# h1 C
5 O2 Q. Q) h5 `9 W3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:3 t2 ~3 e& G- T6 p& Q3 ~
/ J; @$ `$ q. P4 ]" n* C ```' j* M8 D4 E! B) Z
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
. z( W4 z' [/ d5 }! U function add_site_wide_notices_boxes() {. }% p% f2 E' i& b( C2 J- f# h8 h( Q
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high'); C8 O4 t4 ^ V0 W9 }
}
$ g3 B6 \; [8 @0 D; t. p4 o9 M( [3 b
function notice_details_meta_box($post) {4 x! p# _# D" C9 r I! H0 R' }7 j/ R
wp_nonce_field(basename(__FILE__), 'notices_nonce');
5 P5 f7 X2 V Y; m $notice_title = get_post_meta($post->ID, 'notice_title', true);
) A8 ]8 ~ ~! X $notice_content = get_post_meta($post->ID, 'notice_content', true);' P% n! t ?9 o# T0 G6 y/ I7 v
?>
3 r; P& i7 ?' Q% W; n' p' S% {& | <p>
1 J+ _% s) O u) s& V% ~ <label for="notice-title">Notice Title</label><br>
( J' ]' R5 f* o" _5 a, y2 Z <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">* _6 k) v- O% x+ R9 a
</p>
. Q( g. k6 Y: k <p>
5 P( D. y1 k1 u! }$ O C9 F: u <label for="notice-content">Notice Content</label><br>" A% d& w% J4 o8 y8 G; k
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>7 S4 r- ]! x' }- e3 u: d7 N
</p>
. c% F8 n& Z8 \: _. W% P4 ]: h <?php. z1 ]/ \$ @7 L h) M- |3 R
}
; ?' s9 m- h$ Q4 B* i: R: \9 k
/ c- D' {0 Q% z8 `+ J5 S! K1 f: a8 s add_action('save_post', 'save_site_wide_notice_meta_box');/ M- [0 t1 O& }. D) Q% r; b' F
function save_site_wide_notice_meta_box($post_id) {
! ~( K0 Q# b' e0 M o if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
. o$ e0 q( ^" b2 o5 Y) c7 S7 g& ]. v8 z return;
5 z5 a6 r8 R5 t, [- u if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE): p- l) e. U) m! @ \' O
return;
$ w. g2 E* b4 D5 W2 o
3 m2 u) x# k; @( I- o if (isset($_POST['notice_title'])) {3 Z) `1 g; ^) h& R5 {/ e! J
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ W' [. a6 Y3 t, \ C
}
2 Y- L, a: k/ F' y if (isset($_POST['notice_content'])) {
; @. a T5 N3 R6 q& x update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
6 p2 s% x8 o0 w! ~ }4 V M9 I" o: R
}- A% c* d5 E$ R F
```( i2 K- Q" h5 R8 O# H- \
% Y8 f3 V% {# I# p P% i" j 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
7 f, x4 O3 R3 q" d5 w; V0 t- p5 o. l ?2 r' X
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
: E& o. F1 s1 Z1 M; j1 N/ Y5 r/ ?9 `! a6 D
```
* A6 j; w, v4 D% z6 b $args = array(
& Q3 L' W8 _+ [. l 'post_type' => 'site-wide-notices',+ t) U4 W+ `0 Y2 [) q0 o0 W
'posts_per_page' => 3,6 t9 y- I' {" {; o7 X" [
'order' => 'DESC',
, K4 t$ e* T6 O& c3 o' n/ c0 c 'orderby' => 'date'
) X2 b* O# j+ B+ ]4 F4 `2 z );0 r: t& J. t$ T0 B* L' [" z
$query = new WP_Query($args);4 g) L* ]' X' P% h5 h/ ^1 E2 F7 X8 Q
if ($query->have_posts()) :
" C" o' e$ \/ b( t$ F while ($query->have_posts()) : $query->the_post(); ?>1 F! E! f& V7 p. K2 T! h) t
<div class="notice">, b7 L: x. L/ h2 L7 A
<h3><?php the_title(); ?></h3>/ _6 |: Z5 B3 n" X, f* a7 l2 K" p$ c
<div class="notice-content"><?php the_content(); ?></div>
) R, V3 m8 F/ u' C; |0 e+ T: ?% Q. X1 M H </div>
+ q5 Q/ {! p9 P8 y <?php endwhile;
0 k0 }3 O8 z W7 N/ z9 K3 ^ wp_reset_postdata();5 X6 [8 w( \3 z
endif;+ M5 E$ Q- c. S2 g4 T* e
```3 @6 z% O/ e& J1 U
7 G7 F9 _/ Y6 n% Q1 c r! S 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|