|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
$ ~8 O: E; k$ e7 H @8 V: n/ K
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
. w+ G9 A: d) y# {' X
4 k1 _9 z4 ^7 f, b, m9 }3 c以下是创建自定义插件的步骤:
! \1 X7 _/ c' B4 }. @% D1 ^( C7 v7 r, ^. _" N% I$ |
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:* k. g3 u2 W1 B) `# |; \5 |! ]
8 s5 t r V7 x2 {% U5 I ```% D. M6 O1 _6 e4 E5 V* m' q* K
<?php
3 I, L) I* y% ?3 P) m /*
$ b, b* [9 Y5 X% l. K Plugin Name: Site Wide Notices Plugin% m$ s, M7 H9 {! s1 } O
Description: Adds a new custom post type for site-wide notices.
# v6 s( {- ]/ k. H/ J. [ ` Version: 1.0& ~" ` f! @, l% N( N6 F1 y
Author: Your Name
v& k9 A* }' c7 Y$ ~ Author URI: http://example.com% }# p& r& r% I( g1 ~( A
*/
5 c" W7 g. w2 W) y, j3 B5 v- q4 u5 W9 S" V$ \: J
// Add plugin code here...) `* T+ u( D- M i) z. n# s
```
7 E# R( A4 k2 F' T0 V) j5 K8 [9 N2 y0 c
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
/ A! S) @$ h1 l: G& r- f; q5 k7 {& I; h: n3 V
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:' s( z R% i- P7 Y [* t
2 |" g0 h' Y0 c& k% i
```
2 c/ |+ h _! ^" b% f add_action('init', 'create_custom_post_type');
: c9 F( a. t6 n6 K( M" u function create_custom_post_type() {5 Z l7 U' i( b' t) O: ?
$labels = array(
8 s) O# }9 g3 S$ M( V/ t! M 'name' => 'Site Wide Notices',% k: f* E* E: A/ x+ @- ]
'singular_name' => 'Site Wide Notice',
3 e( |+ Y: u4 e/ ] 'add_new' => 'Add New',7 t, p, `# y3 f$ q
'add_new_item' => 'Add New Site Wide Notice',% S$ O1 X/ p8 w3 B' _/ e
'edit_item' => 'Edit Site Wide Notice',
) r6 \% n: Z( _& h4 Q& B 'new_item' => 'New Site Wide Notice',! z( X0 A: O8 t& |' \0 W2 E% i
'view_item' => 'View Site Wide Notice',- B4 s8 B8 G7 e* i4 e' y$ Y; T
'search_items' => 'Search Site Wide Notices',
& i; O8 u0 ]) V! R 'not_found' => 'No site-wide notices found',
$ K* c. k$ D2 N& } 'not_found_in_trash' => 'No site-wide notices found in trash'
9 O9 Z. @! [: @) ~4 g$ c r );8 K E+ R, S$ |
: h" d) _/ p" [& \7 m' f $args = array(
& o% _9 q: ` T' G 'labels' => $labels,9 f8 }4 m9 Q* I. |
'public' => true,& R) p8 C% S6 T( F) P
'has_archive' => true,
! v# }3 A' G" f* @ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
$ w7 B* b$ w' ?4 y9 p/ C) M9 [' E 'taxonomies' => array('category', 'post_tag'),1 c; i3 {9 L7 F, z/ o
'menu_icon' => 'dashicons-megaphone',
( o" |; M( K" f3 |* ~" E# a8 b. F) }8 Z+ B 'menu_position' => 5,, A" ~1 y# b& p6 z& E: f" D6 {
'rewrite' => array('slug' => 'site-wide-notices')- P5 p4 o9 N- A, s; i
);
/ q: _; h; E3 G% l9 f! E, `+ s3 C; v: J0 w! d
register_post_type('site-wide-notices', $args);
( |) v% t* D5 |4 a& |. u" L }
W v) Z, F6 u ```
{; ~( y) M9 S$ i3 X: b6 |- M4 y+ L/ w' t/ O
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 o% f- N- |! Q' G
! C6 v( k: g7 M5 O" o3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:2 }2 f; W6 v1 B, U P
) A! ]( L3 W+ e( M8 h5 ^$ |. O8 x ```# W, f9 T; {. j4 D
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
5 I: e7 ^3 Q. E3 O5 d5 @8 v function add_site_wide_notices_boxes() {0 t' d: O7 \# J3 a- e4 A! ~
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
- u- f0 b* H3 l6 Y/ C5 }6 a H }
1 x, [- M& }8 b' }( B/ ^5 b3 J. N* U/ V- S% q# _: b* D
function notice_details_meta_box($post) {
& P9 c' o/ _% e wp_nonce_field(basename(__FILE__), 'notices_nonce');0 M1 a8 A) o9 a9 M/ Y5 w5 z
$notice_title = get_post_meta($post->ID, 'notice_title', true);
+ j& ]) B9 S( I* U8 m2 I- A7 O8 j' P $notice_content = get_post_meta($post->ID, 'notice_content', true);
: z m) O* ^# `/ e ?>
# {3 V1 [0 S' o# n4 i3 ~ <p>
2 V, M$ z0 c* `9 M <label for="notice-title">Notice Title</label><br>
: D: e$ E. Z( H# t+ @ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
" i) P+ x% y0 ~# I/ p </p>% o: }: Y2 x: I& L
<p>, W5 j- m0 w( N3 _. O
<label for="notice-content">Notice Content</label><br>7 `, z6 r; b# v, q$ R, F
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
/ z# X3 D( [" S$ { </p>5 O! k- _% X; v" s
<?php' `: U' n- K; X, F! _1 p8 U# l
}2 g0 T4 [4 f4 N$ @6 {) A
/ z/ Y: g0 s/ C add_action('save_post', 'save_site_wide_notice_meta_box');, c3 g/ i8 }0 R6 j1 h4 d7 m! ]
function save_site_wide_notice_meta_box($post_id) {, k s1 N& n2 u# N5 z5 v
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
) r) ?3 t' b# `0 q5 [$ n! P return;% U4 `& U4 y6 J
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) o: y5 S1 c' s
return;/ Z7 k& [$ ^( w3 m
. R2 |# N+ m* d6 j$ L if (isset($_POST['notice_title'])) {8 G P3 G- m" D; K
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ c" v! @) A& ^4 f
}4 X9 `9 e9 Q3 {! g
if (isset($_POST['notice_content'])) {. I* F6 B: U* B3 w2 C! q7 E4 ~
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));" A" S9 ~+ \/ s: D
}- q" |- Y0 |7 Y% T. H4 {" X
}
; T% q+ y E& P- }5 | ```
3 ?8 v) Q! h, U8 ?9 u, R9 G1 T1 l0 Y+ X8 F
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
3 M: e4 L4 {' y4 u$ p Q
5 ^5 e# ?& ]! M' e3 G L4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
, v$ q4 L% ^6 b( v" L# d: ~5 k( ^+ P8 C0 X, }/ S- D3 Q
```
' h* [/ E# Q7 C9 I: J. A# [' { $args = array(
8 f7 C, B% o% s4 n5 ?! {- R 'post_type' => 'site-wide-notices',
w, N2 E: g( |6 u# [$ J 'posts_per_page' => 3," \9 k" s: |3 n% E1 q) c+ _9 f2 W/ Z( E
'order' => 'DESC',5 y8 A. J7 m# B/ H4 f7 w
'orderby' => 'date'
7 a; O- h' ?- `; L );
- m+ T ^7 n6 l6 n3 D7 k5 c $query = new WP_Query($args);
8 a: z( F* {# `4 C if ($query->have_posts()) :/ C j/ c6 J9 s5 f* e$ _0 _" u
while ($query->have_posts()) : $query->the_post(); ?>
$ y: Y7 s1 h1 ^9 V4 O <div class="notice">
+ {/ v' f. J1 r% r <h3><?php the_title(); ?></h3>4 @$ h8 a9 J2 g) @8 j
<div class="notice-content"><?php the_content(); ?></div>
# n6 u# Q/ |4 B9 Y </div>' I2 E* q' a- x; b: Z7 @5 j* O
<?php endwhile;
$ ?# G9 u) T2 r4 T; H ^1 I3 W wp_reset_postdata();: [. b& z& {0 y
endif;
+ O+ u% o+ f8 s+ I$ \ ```# {; D' ~5 E/ A4 I; W3 a# O
2 H+ o5 S# X; ^* F2 j 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|