|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?# J5 ?% @) n' t
9 L2 M$ o* w" q) U
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。$ i# ~! z' T& _; [6 B
! G4 t0 [$ `5 G3 B) w) ] T
以下是创建自定义插件的步骤:
$ b+ A; E% k; B6 n9 h4 u4 D) C
) p0 B' q5 f# `3 q/ f$ ^3 K( j9 r1 |. y1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
6 ^) `: t; H0 U7 B+ }, ]
: P1 f, e4 v0 S4 j5 Y$ U; T+ d! z0 a ```
9 Y; Y4 J+ t$ C4 i. @: y9 I <?php
2 `* L7 Y) Y8 ]2 l! Z' b( | /*
3 j" `+ d' ?; q2 K Plugin Name: Site Wide Notices Plugin% x7 ~, I5 n7 {& S
Description: Adds a new custom post type for site-wide notices.. R& s7 i( C3 c# m& z$ O
Version: 1.0- ^; f4 L8 J2 e
Author: Your Name: f+ t! x7 S2 n% m' o
Author URI: http://example.com j/ s. \! ~! H
*/' ^1 J4 v% N1 O; B
! l2 T# D; a# b, O4 k) K+ Z // Add plugin code here...
6 E/ t) Y2 @# g+ ^. L ```& M+ z+ i5 u, z. l6 R8 F
8 D6 _; G) w5 w( a5 O9 z 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 B; ?$ A4 R. G9 q/ S( t
0 x4 T9 ~* l3 V2 ?3 Z' z2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
( M6 Y# c2 p. l, u# P. ?- T7 M) c9 ? X) |+ X# P
```) n: _- l( z/ L3 L" {1 s' d: e
add_action('init', 'create_custom_post_type');2 U# _' o. ~; r! m. b% G0 ^0 q
function create_custom_post_type() {# V& c2 c; J2 w( f; P
$labels = array(0 m1 y8 p7 P8 d5 Q B* e4 C
'name' => 'Site Wide Notices', i0 U3 |1 j8 ]( S V c# X, I
'singular_name' => 'Site Wide Notice',
3 ]% @/ C. Q/ R4 S3 K9 W 'add_new' => 'Add New'," H! O) H+ L, X9 Q+ A, G4 K
'add_new_item' => 'Add New Site Wide Notice',
2 Z( K& h& h; f3 A 'edit_item' => 'Edit Site Wide Notice',9 l# l$ F$ I# |6 c7 m% m) n
'new_item' => 'New Site Wide Notice',, D8 Z. D- T4 Z0 z2 ?; [
'view_item' => 'View Site Wide Notice',
4 m+ X c" {# K( |; T 'search_items' => 'Search Site Wide Notices',6 E' s, e3 Z6 U7 s6 l
'not_found' => 'No site-wide notices found',
3 T+ S% n" a! B5 y 'not_found_in_trash' => 'No site-wide notices found in trash'
$ \3 T. p, j; q# e X1 V* L" v2 K );
/ `" _& _6 n- w, w) J5 G4 L. }& ~: g* |' V
$args = array(7 a7 t9 W3 q; P6 Z: ?3 {
'labels' => $labels,0 V% c9 j4 @' J/ M
'public' => true,. g9 p, N: A! N. e4 g1 R- C) H
'has_archive' => true,
3 x" L/ H- u5 g8 R/ |& _; W4 U" n4 N 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
! Y9 ~' M; F1 g2 g% k% W 'taxonomies' => array('category', 'post_tag'),
) p7 ?0 [( g# x 'menu_icon' => 'dashicons-megaphone', ^% A u6 W* U0 T0 l4 O
'menu_position' => 5,
- P* X$ h2 U( W `# B5 F 'rewrite' => array('slug' => 'site-wide-notices')
* K. B& Z. R9 M: S$ c; ? );
9 N2 }8 D: P- u' D( h- i% e# A
" e, d3 J. U/ I; T9 Z register_post_type('site-wide-notices', $args);' I+ }- Q% Z3 w
}# o" m5 ^" ?" O& o, n4 s, U
```
0 K z" U3 _ |/ s/ y; A$ D+ I a K3 h! r7 O' R, m. p( l
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。& W; m3 O7 d& @+ b: V' J
- U$ u! C) q4 H3 Y( c8 [
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:2 J/ E' ^' F; Z2 W9 a
' Q. e7 Q* E) `, }
```1 u% z3 [# l$ ]# n1 e' ~* h
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
% O. l1 V: y" M9 e function add_site_wide_notices_boxes() {( o# H) x" U+ V3 X
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
( c+ o, g2 F$ w. t# v }- S4 T3 A# g$ V$ ~8 ]- \
# q/ @5 S$ v/ t T, P function notice_details_meta_box($post) {
* Y" I& S" p" K1 c4 y" @ wp_nonce_field(basename(__FILE__), 'notices_nonce');& D) U/ A2 ^; L2 H; r `
$notice_title = get_post_meta($post->ID, 'notice_title', true);
1 s' l) Z M+ Q: L $notice_content = get_post_meta($post->ID, 'notice_content', true);
# X1 q" Z u& f5 h ?>
- S7 @, e! k) W- L" C" J2 v2 g; [) S# P <p>0 G9 @% j p; o' D
<label for="notice-title">Notice Title</label><br>
3 o3 T. E# L9 T2 ^5 F7 Z# X. F <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
; g' ]: f `9 B7 r </p>
' z/ [8 _6 W/ M: N' \ J <p>. ^% q: F! j( o* |& R7 P! k" Q% V
<label for="notice-content">Notice Content</label><br>
3 F8 J7 r" d+ U" G1 {3 q+ { <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
. q6 I3 |+ w+ N: _$ W </p>& D4 G/ S6 e9 b5 K G( x
<?php
( C+ c5 r% _& O6 D& c" a }
% S) E2 A! y& V! i
9 q1 j5 z- u7 q& e$ P add_action('save_post', 'save_site_wide_notice_meta_box');
* v( r- I! t/ S/ w1 o8 m function save_site_wide_notice_meta_box($post_id) {
+ V2 f u5 @; Y if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 V) q& I: E% N8 u5 @6 | return; W8 Y! A! v+ C$ J- o& r* D
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)( s, g2 X% W0 I, _# b" d
return;( c8 p8 r; ~; _( n" J* a; n: [/ i
0 Y0 v; S+ C. G9 _. Q! S* x2 s
if (isset($_POST['notice_title'])) {
) B/ `! b8 m/ B5 k' Z9 N update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));/ t1 P- |0 K7 G& K- w' j% x
}# T9 q/ R E# J; A
if (isset($_POST['notice_content'])) {
. w. k! i6 ^# C* F# B update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
5 c/ v @: C+ v$ N! {- S' Z' R7 V }, y$ F+ j! j6 ]2 Q! A
}. t# o9 `7 ]7 F
```0 a* O$ \8 S7 f7 Y; w: S
9 Q9 m( t0 L4 d3 @6 y- o
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 G0 D& }1 K. X7 u
& z; _. m: M% i- `) H( p1 r
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
( y, j7 k2 k5 p; D ]. y' y- J) x! S' \) y. Q, ^9 \5 z
```/ V m& H) E/ b% P& f
$args = array(7 ]! W, _' g* s
'post_type' => 'site-wide-notices',
8 n. O6 H# U: D6 T% K 'posts_per_page' => 3,
! h9 @* X) n6 e' U# g 'order' => 'DESC',
0 m8 W, Z. j H( _4 n3 Q4 s 'orderby' => 'date'
# \, Z* K8 ?6 C; b9 w: d' r: b v: I );
. l/ R& }) I2 s0 @/ u $query = new WP_Query($args);
6 a) ^( k4 _/ \; T Y' `) l+ z if ($query->have_posts()) :
6 C) A3 G2 j* `2 d4 X while ($query->have_posts()) : $query->the_post(); ?>1 m$ p; Q0 }+ {+ m5 j5 g
<div class="notice">0 Z+ J+ u/ O" G$ H
<h3><?php the_title(); ?></h3>
+ |7 z4 v& ~7 |1 E <div class="notice-content"><?php the_content(); ?></div>+ z6 }9 W/ A9 i, F- B+ e& j; C& W
</div>
3 y' d r3 g, d <?php endwhile;
7 F7 M8 Z! ?% f6 n3 M, z wp_reset_postdata();9 R0 w' K9 b. l! M
endif;
7 G7 b4 k1 a. q' A- b4 C ```7 {, k3 K$ T8 t& A/ \
" @) b( [- N3 D1 J 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|