|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?! t i. j. [: K: O
8 |" C O; b& C) v8 p3 i+ i" q( r如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
4 R- ?* @% f% x, G
. i& W7 ~5 f2 ^# P5 {- y' }以下是创建自定义插件的步骤:
8 c! s4 A( E0 p4 F- S& X: s! s; u6 Z0 S& |/ ?5 t a# i
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
0 U- e) ]6 G$ M6 V+ h9 i
2 m) b/ j# o$ f6 ~ ```+ x' B0 E% G* C4 K+ |
<?php
O ?5 d, H& c /*' b2 C0 h4 H. ]7 X$ R
Plugin Name: Site Wide Notices Plugin/ K; Q# k# _' Y9 \2 m8 {7 g2 K8 r
Description: Adds a new custom post type for site-wide notices.. ~! [) s0 Q5 g
Version: 1.0
6 E e, t( o. _/ _( V- p Author: Your Name
4 G! M; |! j9 k5 x1 g( M Author URI: http://example.com
. n# c; X2 |5 h; X3 h */
% L( c5 b' K5 Y1 K" ^8 }
5 o, G: U2 k% w // Add plugin code here...
5 P) B% X3 f- N$ G P; J ```
8 Z M3 Z. I) q* L' E. o1 K0 m+ L P/ j2 W- O) Y+ _7 C
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- O& ?9 S: s8 Y; L' E* n9 M+ d/ Y4 Q
# l% `3 ]+ k: \/ h8 b2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
7 M8 w- R. V- F% u5 [" i
- w: N8 U& H4 X7 O5 m9 @ ```
& d$ C- t9 ~: w- Q add_action('init', 'create_custom_post_type');
+ Y5 j1 o( d [: p% S2 G function create_custom_post_type() {
3 z& ^- U( P+ Z# D T/ L9 ]$ u7 R $labels = array(
. R( N/ M" R; U* z 'name' => 'Site Wide Notices',, o& s4 g* I6 X+ D3 H+ J% N
'singular_name' => 'Site Wide Notice',
4 ` E. A; {4 A5 p3 u. x) ^ 'add_new' => 'Add New',
7 v- v3 F" }4 _ 'add_new_item' => 'Add New Site Wide Notice',# } m- s/ B, D8 V2 o* G5 e
'edit_item' => 'Edit Site Wide Notice',0 q! f, A ?0 j7 S6 {
'new_item' => 'New Site Wide Notice',
! r, W) n& j8 {1 O/ e 'view_item' => 'View Site Wide Notice',
# Y+ {: g+ \2 L# W 'search_items' => 'Search Site Wide Notices',+ n' y! X& R, U/ r
'not_found' => 'No site-wide notices found',$ ~- t$ p: F7 u, J! n
'not_found_in_trash' => 'No site-wide notices found in trash'! K( E5 J* n: C# F1 U
);
6 ` `$ J, _' a5 E: _, }2 w
# m4 p o1 [& X: @3 \) h; g $args = array(- _2 B( ^7 p( S1 p- H) ~! x. c" D+ g
'labels' => $labels,) l- _4 O; N) b0 E2 N: `. m
'public' => true,1 q) T9 B* U+ Z9 [3 B. M+ t
'has_archive' => true,
! ]9 `6 ]/ c. q: \. ^ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),% d: Q7 ?, T- L/ \
'taxonomies' => array('category', 'post_tag'),
' F' x+ X0 h% G 'menu_icon' => 'dashicons-megaphone',
7 V- X$ w; }/ K4 A7 Z7 J5 b 'menu_position' => 5,6 o9 Q+ d: h( ~+ b7 S4 z
'rewrite' => array('slug' => 'site-wide-notices'). u5 S( b2 t1 l1 p5 Q/ \( X$ A
);2 p! z$ g" a/ ]: ~+ V
2 U) Y2 W% g9 R- v# h7 E( K register_post_type('site-wide-notices', $args);
. x2 y& o: q% N" r }; V. A8 g. W1 C1 j$ [
```
9 d( g. p: C1 [7 h4 _
% i3 o0 Q, ?5 W; _. W8 Z 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。! K$ K7 s F* l( n$ u
2 P H: x/ p. r K
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
( s* s! w8 q5 s4 l. m$ Z
. ?6 V& W# S/ F# K: z3 N# } ```2 Z7 @ C: m+ r; W" U
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');9 r) o, O0 S$ T% i. `! O
function add_site_wide_notices_boxes() {
* P0 l3 i1 u2 Z! z2 g add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
5 ]/ T+ D* A8 C$ V }) P: w' X9 R* _* {7 o4 Z2 N
; A- |# U+ E& l" x5 t4 E8 r
function notice_details_meta_box($post) {
1 x' P2 e: ?6 X, ]3 Q wp_nonce_field(basename(__FILE__), 'notices_nonce');" _ U* }1 j7 n7 B
$notice_title = get_post_meta($post->ID, 'notice_title', true);% J3 W: d0 N' u: G( m- W
$notice_content = get_post_meta($post->ID, 'notice_content', true);1 _8 H0 s) ~0 U6 P! R- o
?>
0 o9 o- f# {. l, F <p>
$ U3 b2 l$ ?, c- l" T' G <label for="notice-title">Notice Title</label><br>
+ s7 S1 Q. h& ]* U <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
& @7 s7 ]9 Q& b# d( h& a </p>: I% v( f, Q( |$ d7 t
<p>
: ^& |8 X9 W/ \- W- |; }0 k' w <label for="notice-content">Notice Content</label><br>
: b* a% w7 H+ N; |" R7 J9 h% {/ R <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?># M- i4 y3 d3 V3 G4 d: I( k. _ t9 L& [
</p>* h$ C7 P' P* v7 D2 }! p3 n
<?php
$ a: }' E4 p, o+ ]1 A2 h }3 L, J9 _. P* {8 Q4 L9 o" f: w: z
* g! T& Q$ l% X* M( ^/ s) A# k. Y add_action('save_post', 'save_site_wide_notice_meta_box');
/ o3 E5 Z( C- k+ ~- f9 k# V* K function save_site_wide_notice_meta_box($post_id) {
6 q- v7 ?3 y0 \9 Y: U! B; G4 K' p if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
' C9 e0 _" R+ [0 f return;0 a' Y8 p* H) }9 z
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)8 w/ O! H( J" A6 {* _
return;( X$ z* g9 l. @' p
% \" J6 F) V- n/ v [' T/ i6 b1 s
if (isset($_POST['notice_title'])) {
' n1 B+ i& n2 ?/ A. j4 U update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
5 w: X' e" k& l5 A s# E }
/ g% N1 ^ E# N% \ if (isset($_POST['notice_content'])) {; C! k9 D C' U! h2 o/ S3 u# d- c4 E9 l
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
% s: D. R9 C; v- v }
9 Z* Y( Y* ^4 g" c* O3 ?9 L }$ ~" S1 T, f$ ^ \9 J' a7 [
```
# U) R( ?: j, C+ m+ R
' P9 S5 @* P- K 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
% t: {- t; l: S7 |. {
8 u8 s4 x0 Y3 j! i4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
. `3 _0 ~$ V/ n
* g. ^) x9 x9 R; a) \& F" f2 w ```
' { F6 I" x8 g; Q $args = array(5 I- S$ ~* t3 E" g. o
'post_type' => 'site-wide-notices',
% g) }. k- |1 y$ H1 |- t 'posts_per_page' => 3,
( |8 k" i1 K$ c! r' t: y0 Z" i 'order' => 'DESC',/ F$ ^+ J7 w" T% s+ |6 a$ n
'orderby' => 'date'* }" l; _/ ~& V9 r* ]+ ~
);$ L3 H# d \6 ?) O( W* n3 h
$query = new WP_Query($args);
! H, k8 D1 T6 ]6 V: R if ($query->have_posts()) :0 F# u0 I$ [1 ~) v# c. P
while ($query->have_posts()) : $query->the_post(); ?>
6 C3 @7 b+ H& g! Q, y <div class="notice">" l9 k4 U0 V& P0 ^- n* w& Z
<h3><?php the_title(); ?></h3>
$ Z+ _/ r5 m7 v. ` <div class="notice-content"><?php the_content(); ?></div>' [! Z9 R% e7 ~. i' m: F5 _" T
</div># E) o: `1 j& B/ p
<?php endwhile;/ u" M( [/ V6 x' s& @1 U$ e" A
wp_reset_postdata();
1 L/ U" c! U& \( |- P endif;0 f$ D b; E* H6 d* s
```
, f) Q6 u0 e" S$ K9 C! f
% X% [" \# N. S& E0 i& v 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|