|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?9 T* a7 t" y: y
" C! N. @2 i' W* m) a7 u5 \1 v如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。/ D7 {$ b2 G' G, |, X1 ^
) m) h/ G' N# o7 [+ e
以下是创建自定义插件的步骤:! y4 W& r2 O; W/ H" }
" Z# }2 G5 x$ v$ |- N: h0 N3 Y4 n1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:/ t# w- X& b @4 I) n/ Q) S
1 K+ L1 D x& u5 [- S ```& H; g' t6 R. o+ [( n( l8 ^ o4 M1 M
<?php
9 J$ o8 c- j5 ^ /*
- A0 f. ]* F# c& N1 i" B8 q* ^ Plugin Name: Site Wide Notices Plugin" E, [4 u f |8 {' L3 H" u3 x {5 x
Description: Adds a new custom post type for site-wide notices.$ N( t: z5 i. B# R4 \0 C
Version: 1.0
5 m* T$ \1 }7 a( Q9 e Author: Your Name% I% x3 Z. \: n# i! S* u$ ~, H/ N
Author URI: http://example.com
9 k4 V P7 u( L! ?5 @' C2 @ */2 S7 O5 v' H. g" ]/ v4 {
0 o G4 a& e4 S p# v- O( } // Add plugin code here...
/ ]1 w6 a: N h9 k8 ? ```# M' l% ]# b4 C7 s3 V
% M9 V1 ]- |# O. Z* V/ W 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。" g: x* u/ R# ]
) g- o/ E& e+ R, E. y! M# _
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:2 [1 t- h) R/ w
4 N: r. {8 d; Q0 [- B2 r: ` ```3 }8 B- }: F& f X8 p; Z
add_action('init', 'create_custom_post_type');* E/ a% w' j7 K9 U
function create_custom_post_type() {& {( C/ R- \1 e* v2 R% L
$labels = array(7 X" y( Y% H; d( q7 P# Z$ J- N
'name' => 'Site Wide Notices',1 p& P3 P( M0 P" z. r U
'singular_name' => 'Site Wide Notice',9 N& q% S' }+ |( s6 J
'add_new' => 'Add New',
- J6 x& E4 M( W3 ]5 `0 ^9 G: G 'add_new_item' => 'Add New Site Wide Notice',
( v" D( L5 I$ A3 l4 x) a 'edit_item' => 'Edit Site Wide Notice',
! E9 U' Q' I, a, W) O/ ] 'new_item' => 'New Site Wide Notice',
' ~) ]$ ^6 N4 s! v8 I. i 'view_item' => 'View Site Wide Notice',
/ u2 b6 h& P+ m0 [. g 'search_items' => 'Search Site Wide Notices',0 m# Q6 r* ^1 C" V3 t3 e
'not_found' => 'No site-wide notices found',
% q6 ^1 P8 B3 ^ J9 V 'not_found_in_trash' => 'No site-wide notices found in trash'2 e: Y7 F/ u* v$ f
); X5 C- z( {: U( S" M" ]
& q4 v0 m' v0 x, b1 [ $args = array(
* w" g1 j. S+ n$ E4 j _6 ~. G 'labels' => $labels,
$ H- Z: Z, \+ |( o9 ~, y 'public' => true,# T" W* v- @0 x! }/ c0 p$ U
'has_archive' => true,
2 j T, }: v4 y [$ z 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
- G& D1 o1 v" {4 M9 b8 ] 'taxonomies' => array('category', 'post_tag'),
U ~# V4 a$ H8 y: P! |- s 'menu_icon' => 'dashicons-megaphone',
8 a0 j4 ~- n1 h) T& x0 f* A, L) w 'menu_position' => 5,) t# q: R! _" z. X6 }6 M8 E4 Z
'rewrite' => array('slug' => 'site-wide-notices')! f, n9 J7 Y! y D$ }6 u
);
3 `. v7 G6 U# N- P" Z/ T6 n Q3 A8 y [) f9 }# M( k
register_post_type('site-wide-notices', $args);2 S6 l' N: d" ?
}
8 ~( K# k) J5 I1 m a! }9 d& A ```
8 g* c) ]/ A0 ~" B; a; u1 b+ r l1 Z* `* U( C
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
/ ?- E: {& V! `9 |" v" k- S n4 ^8 l( v& b- ]0 q' o6 l
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
/ |& u; T( O' m; K9 L5 ~# P' x+ w( W4 V2 X8 d
```8 ~$ _8 a7 G8 v; z1 J& k! H
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
& e% z/ w, O* u! F function add_site_wide_notices_boxes() {. z* q, k8 Y+ S* T/ N7 B$ m/ x
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');' @ b9 U' I) v
}. ^, w0 _4 |: X
- L# }. H; `! v+ k# X
function notice_details_meta_box($post) {
! A8 ]8 w/ |* \ wp_nonce_field(basename(__FILE__), 'notices_nonce');2 k7 {% e: G/ j! C- V7 r5 R
$notice_title = get_post_meta($post->ID, 'notice_title', true);
7 b9 |' g& M7 W" ?& Q $notice_content = get_post_meta($post->ID, 'notice_content', true);
9 F/ `- a. a# `* h! E# e ?>/ y9 A5 h7 [, d4 |4 H* I6 F# M, z
<p>
8 r% i( V! k' Y$ r7 ^ <label for="notice-title">Notice Title</label><br>
$ F+ A7 b- r6 T( K) G2 u" Y <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 K1 |7 x! O/ ]! ^( B( L* s
</p>
$ J& G$ r5 t0 j# B3 K; T <p> ^; j' w4 d% r% h
<label for="notice-content">Notice Content</label><br>
, z/ Y3 D0 u4 v <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! ~/ I% ~ |, x
</p>
e# N/ M' f8 V2 S <?php" A1 Q Y& g' b$ C4 O- U. }
}
! W7 b5 J0 W" [ A
, C2 D7 u; ~- N: b0 c9 f4 } add_action('save_post', 'save_site_wide_notice_meta_box');
. B( D; K6 a7 e2 y+ u1 O function save_site_wide_notice_meta_box($post_id) {
: b* ~. {/ j0 j, r7 U if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))/ `/ H0 t+ Z G! {5 o
return;- p& b U8 M2 f v
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)8 u' m9 r9 C! m9 g% L) w- }
return;
- y+ W( |6 B* m0 t9 I _0 r4 l5 p+ g+ L- J) v
if (isset($_POST['notice_title'])) {) {! ^& f$ D+ Z* F
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
. H5 F! v2 H, T9 G) o; A1 z; ?3 d }
3 _; o5 R% l" A. l if (isset($_POST['notice_content'])) {- D# f8 `% @3 v& H( |5 d
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));7 X7 ~7 Y' _+ F8 D
}
5 L! F; h! U) V" {/ U$ S( Y }5 o8 w, |; V; x+ k% V
```1 G9 Q+ s" P# z
3 w) |; K8 l. D* h3 G
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
( l M: |2 j. I7 [& O. w) I& o3 j2 @$ l+ B( @
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
: }7 |; ?+ U% ~% E; y2 S5 b- S, q& B, x! s
```# N7 r: G9 S& ~: u6 T
$args = array(
+ R* z* K5 E. G% \ 'post_type' => 'site-wide-notices',6 @8 M9 b3 v; `& X0 k, G# Y
'posts_per_page' => 3,6 j1 q0 u6 z6 Y" |2 M& q/ j
'order' => 'DESC',# J7 k1 x* h/ k
'orderby' => 'date'8 ~$ d* b# z- b; G
);2 z. f" E& v6 J) q- z
$query = new WP_Query($args);: R& N7 n+ A/ Y0 @. K
if ($query->have_posts()) :
6 R2 {3 h. |3 H m% T while ($query->have_posts()) : $query->the_post(); ?>8 W9 T( Q: o6 j( r5 X$ Q6 _
<div class="notice"># q+ s" ~0 U. Y% ^3 c, k2 |- ?
<h3><?php the_title(); ?></h3>
$ R, q) Z- j- e" l# |, S: j <div class="notice-content"><?php the_content(); ?></div>
5 M% q* A9 l V6 z/ p </div>1 t6 t: T# v0 Q# E: x
<?php endwhile;9 k7 I7 e9 f' n ?8 _- q
wp_reset_postdata();
; X+ P+ ~" t5 a0 U/ t0 z5 p endif;
8 I2 h; n- I3 t5 C* [ ```
# @/ g& p( s7 c- ]0 Q8 U& c* S% a
1 F3 c2 m9 p3 v) r, c1 P7 y+ C 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|