|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
/ l2 ~7 E) w" ]4 P! o C
: Y6 j2 M4 m G6 ~! U如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; L; x8 v3 m) Z# r O6 j' J* {
# z+ n4 {. a: G3 p1 t以下是创建自定义插件的步骤:
* O6 C& l4 L+ K; e G* {
$ v H6 U4 m! x1 M1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
1 L* w9 O" w1 V: d- T1 Y) h
# q( U- O; J+ W/ p& R ```% ^2 i# [4 Z8 U; ]+ o; R
<?php: r) L, H4 q+ D
/*
) y- B X/ u* L2 Z Plugin Name: Site Wide Notices Plugin |# A- K% [# s7 c8 }
Description: Adds a new custom post type for site-wide notices.
, Y0 Z) p6 I& q3 X+ [ Version: 1.09 @* B: i; g$ L, P6 ^: }' @3 d
Author: Your Name H3 q" D; g ~/ O, R
Author URI: http://example.com
, Z9 P3 w% V: n [; x/ x */
$ c8 @9 R3 J3 ]' G6 I" ^, B; W8 r7 J" o7 T
// Add plugin code here...
- T0 i* t* T5 O* o% s# ~+ ~; [( [ ```
. F) H: F0 {& ~- ]" R9 m9 j# { U3 |) R
' U; R+ W% W6 \/ \6 e( } 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
1 ~9 s; Q7 w3 ~- U
4 b$ G! |5 ]; C( m! B1 Z9 r7 p2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
+ N4 w- f. d) r D2 y; O$ [
1 M5 I9 b% K: a ```
) `! e( a! Y+ Y+ @/ H add_action('init', 'create_custom_post_type');
+ y; f+ D) Y5 C U" [9 v function create_custom_post_type() {& S8 D0 f& a8 w: H
$labels = array( ?6 T: k+ f9 P, C/ }
'name' => 'Site Wide Notices',
/ E$ T8 Z& ]" g1 y$ O 'singular_name' => 'Site Wide Notice',
/ k, J4 h. x/ w% F5 m6 y3 M 'add_new' => 'Add New'," K! l1 t# `4 u! U1 _( U! B" S
'add_new_item' => 'Add New Site Wide Notice',
3 s+ T; A- M8 _( w/ r0 G: B 'edit_item' => 'Edit Site Wide Notice',6 ]2 U8 d; d+ V" q/ ~) d
'new_item' => 'New Site Wide Notice',; J5 D. e$ |/ x% ?3 `
'view_item' => 'View Site Wide Notice',
; k9 I0 N( ?$ S: C y 'search_items' => 'Search Site Wide Notices',( G2 \& v0 a4 C- P/ E* O: M
'not_found' => 'No site-wide notices found',! |) m% q6 g( g# y3 y, v
'not_found_in_trash' => 'No site-wide notices found in trash'
% _! j; z: H( D1 s h/ A );
6 v3 l2 w8 _4 ]3 X6 S* n8 ~1 d- [) g- {$ s1 i' H) [
$args = array(1 g/ p; m. V4 F, m
'labels' => $labels,* h5 k0 I$ v# G5 ?# K9 I t) d
'public' => true,! J* m! C6 a9 H5 q' S
'has_archive' => true,
% W9 F) N0 {/ m. |6 t, v3 x. ^! S 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),6 l4 g; A' u- ~
'taxonomies' => array('category', 'post_tag'), t/ q! |3 m8 P% S
'menu_icon' => 'dashicons-megaphone',9 s. s" i: Q4 k/ [4 E
'menu_position' => 5,& `% s2 A$ Y) \. P
'rewrite' => array('slug' => 'site-wide-notices')
5 C5 U4 S- c! j% L; u% x );
- T6 Q( \1 R% R( z @( Y& B9 S* q
, E% U6 e" A% ]$ [2 e& S register_post_type('site-wide-notices', $args);% d0 |4 Z9 \' U7 `0 u8 N, ~
}+ i2 F5 l7 d) J4 k% M, d" @" H0 ?
```" ~/ m' |" _3 A
8 O$ R" o* G' k' D# c/ z! S8 O
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。1 ] x; `; s8 Q9 g2 S1 ^2 ?
8 L' M# j% g* T) k/ ?6 C' k7 U3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:! v/ d- x) M* {: L% S5 g" x2 I
]; b3 w; H0 v; q2 ]6 ~
```9 ]( Y; y, Z. ]7 S7 w
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');+ c" @* @$ I7 W: d
function add_site_wide_notices_boxes() {
5 \& s5 z( Z# P! S; x. o1 D add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
# a) x7 t) t; V* P' T0 p: \ }+ o: B3 h2 l' Q6 j0 s6 ]
5 N5 Y& H* i/ \* J; y, j
function notice_details_meta_box($post) {- k# |% ?1 X- E4 ?
wp_nonce_field(basename(__FILE__), 'notices_nonce');
% v/ {2 M" k: o1 B- s' N $notice_title = get_post_meta($post->ID, 'notice_title', true);. x& L, D, n4 V, k& N1 f3 S5 _/ G
$notice_content = get_post_meta($post->ID, 'notice_content', true);# }/ u' x& }" ^* a1 S0 K
?>
- P3 _* a+ n" m" {; K! h <p>
3 p5 m2 F) ?1 i1 P <label for="notice-title">Notice Title</label><br>
, k8 n; ?) J6 f/ W' f <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">& W- t3 ^5 W' h5 ]+ P
</p>
# A* V) I7 L. K' n* r( D* K <p>
$ d$ s$ _ @, B- x <label for="notice-content">Notice Content</label><br>2 d/ S3 K' k/ m4 k! \
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>- X9 x2 p: S1 Q) W
</p>; w4 d( r P) f* D3 N7 F1 ^- q
<?php0 ]; i0 z9 m1 o
}- I0 ]8 s: k8 }9 ~
1 S& i& I# B% c+ C" U
add_action('save_post', 'save_site_wide_notice_meta_box');
& ] X8 f) y7 i& _$ v( g function save_site_wide_notice_meta_box($post_id) {
- d+ e K+ _# Z; ?+ `/ b if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
; J' f7 l8 e; ~4 O6 q return;4 ~; `5 K/ [% d, g) q V, E
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)4 [( i3 ]/ L" g O7 t
return;
" u2 T; }9 B4 y* V% Y/ N3 ` |. O0 z
0 X% P' _9 j0 p% w9 _* Y* Z+ ]* G if (isset($_POST['notice_title'])) {
9 f: s+ d7 M6 y' `* J- g* q, b update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ c3 n9 P) S# Z2 @
}
5 X3 v. j0 A4 m' V9 i! P if (isset($_POST['notice_content'])) {
# B8 i9 L3 y) ~$ D. W update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
- i8 a; G, I# R }4 k5 L/ a7 m& H+ Y+ ^% i
}
, s6 ^* Y1 }4 s+ g: k) g$ M6 p" ^+ x ```
. Y$ T! a C, n: N" N6 V4 D9 j2 X$ N4 A; s m7 s0 K; G2 j# J$ F
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
. \2 f3 A0 T6 y1 ]5 b m& s9 t; L) U9 F0 \
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:# I( h7 k8 o. R7 D' e, J
& \# a& L8 G/ B2 O
```* h0 F4 F! q/ B: J( J
$args = array(
# f* J+ r$ s0 S1 E& K- q 'post_type' => 'site-wide-notices',
* S# v# o4 b1 `6 ~ 'posts_per_page' => 3,- e! o; p* J- N4 @2 Z
'order' => 'DESC',; ?5 Z+ s% I& G; B0 w' T/ m
'orderby' => 'date'/ y5 k9 k9 |% `4 K( i& Y
);
8 ?/ g& q9 u! W5 g7 d5 a$ M2 I9 P, W $query = new WP_Query($args);( d4 L, x- B2 ^" s. L! p+ z/ [
if ($query->have_posts()) :
% ~9 X$ D5 g# I8 \7 V+ i while ($query->have_posts()) : $query->the_post(); ?>
' ~: G! K3 T/ J9 f! j <div class="notice">
' a, v8 H" E6 u6 ^/ }' e5 Z <h3><?php the_title(); ?></h3>
- z9 B, K5 G5 |0 ^" m) k- G( R <div class="notice-content"><?php the_content(); ?></div>
1 t( }; W, s* z; y9 T </div>+ Z) H$ E$ `, t+ X4 K
<?php endwhile;8 V% R) X7 |+ f0 B4 K
wp_reset_postdata();
' k( x; v$ X) R/ i endif;
/ M4 z7 t- d0 B) F ```
6 F" ]* u* q0 d9 i
# Y/ M# Z$ l, Q6 z 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|