|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
" f9 u0 ]( J0 J; R- D* n R6 T( e% C4 o2 c* ]: i" n* u
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。/ A" e& o, r# b/ ~- M1 S
& p9 B7 p6 i0 }3 K6 K以下是创建自定义插件的步骤:' T* @& `) ~# D! }9 \
, v6 G! a# h0 o1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:6 `7 r7 o, Q/ W2 U
3 C! p& \% O6 V% P% y* w7 ?( R ```
6 p- { P! _$ w <?php
+ T6 K5 @9 L7 F2 L$ J1 U: k /*6 i( }3 s: M' i( R4 J2 o
Plugin Name: Site Wide Notices Plugin
' M$ D& A4 f) e- r" b5 F5 n Description: Adds a new custom post type for site-wide notices.5 e2 e$ O5 q3 ~6 X) m
Version: 1.0& f! T% ?) y2 X
Author: Your Name t9 J, U% g. a5 @4 t: g
Author URI: http://example.com. k! P: j; G$ O; o" b6 I# j9 E! d: _
*/
2 b8 I5 Z9 l3 G+ ]* f% @
+ r0 f: P7 j) G // Add plugin code here...! d2 r% L- G: r
```
4 ?+ c- M; x8 T5 j* _6 ]
7 i2 T3 a) M3 a$ ?- j6 V* [# k( q+ ?* R 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。$ a7 L; o/ i; g4 o$ U
% r8 \* t6 E Y3 P: T0 Z$ H* L) F
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:% w/ o1 @' H# p, d* {0 n
+ Z9 y$ c! w% Y+ |& C% c ```% A6 j' g! v, S+ k
add_action('init', 'create_custom_post_type');) t1 {: e9 U( C. B* }
function create_custom_post_type() {
$ q- @: `: }7 {' h& Y $labels = array($ x$ G4 m' e1 L6 W V
'name' => 'Site Wide Notices',3 {/ N% S, E' V
'singular_name' => 'Site Wide Notice',
! Z4 ?) Q, t4 r) ?7 m# v' _ 'add_new' => 'Add New',
/ R1 y; G) y. y# n3 y1 G9 q 'add_new_item' => 'Add New Site Wide Notice',
: Y2 j! { X2 k' y 'edit_item' => 'Edit Site Wide Notice'," H9 M$ W. j ], H9 e
'new_item' => 'New Site Wide Notice',0 K) S- R8 R B
'view_item' => 'View Site Wide Notice',- r5 s( F% n/ U5 X. ]- e) E) h
'search_items' => 'Search Site Wide Notices',: {. o R, k$ j/ X" H. ~8 U" b* m
'not_found' => 'No site-wide notices found',) R4 A9 x1 a0 I( ]2 K1 a; ^# Z
'not_found_in_trash' => 'No site-wide notices found in trash'- n4 `7 `3 a5 F0 T1 w
);
/ S% N4 h$ I& R8 Q& L9 }. h5 h% d% n, P; T
$args = array(0 t" b$ v: P0 J2 l
'labels' => $labels,
+ Y1 P3 q9 S' I2 I) w0 ? 'public' => true,
" }; v4 H( Y- D+ s3 M 'has_archive' => true,
! O0 E5 _' c1 b- j3 H 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),2 H, F1 Z5 D$ c: u' d" E V/ @2 H
'taxonomies' => array('category', 'post_tag'),
6 Q3 ^& L N, B4 s/ y9 @( u( k 'menu_icon' => 'dashicons-megaphone',
+ O+ b- `) d; c 'menu_position' => 5,- [4 {7 D P, B& S: S/ |/ q
'rewrite' => array('slug' => 'site-wide-notices')
" [8 m, ?: Y; D/ [% h );3 {! v( e% |% j8 v( i9 Q- I0 u. O
7 A/ f, b5 b% u' @; ^+ K register_post_type('site-wide-notices', $args);
7 U, u: d$ \4 W4 D2 ~& l4 u. M+ d9 S }
W# L/ D" Q% w2 n* R9 F0 L ```
" ?# R% d a1 T! O
# a. F. R$ t, e6 Z 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, s7 W7 }9 w4 W: h5 X
r) E5 h% a! p7 d; }9 S0 h3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
( r4 Z1 K9 Q1 `, S& E3 K% N8 w ^! v, e( L, L6 {4 n0 S
```
' W K5 ]8 { f/ j add_action('add_meta_boxes', 'add_site_wide_notices_boxes');) b' z, s* _# M: N" J! H
function add_site_wide_notices_boxes() {
5 S8 m- z2 ]9 S T4 r add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
* e5 m! Q# r9 C. |4 o. y% P V }( a) l8 R7 S/ o q
$ J# i( w& X+ |$ ~6 e! n* z& Y: H
function notice_details_meta_box($post) {
5 p5 [! \* _) U) M2 E wp_nonce_field(basename(__FILE__), 'notices_nonce');
5 p1 ~5 c( Y3 Y* x $notice_title = get_post_meta($post->ID, 'notice_title', true);
( @1 \$ W! X" I% T* A0 i' B" Z $notice_content = get_post_meta($post->ID, 'notice_content', true);8 m! V: P/ f8 w0 z- \; q8 d. m
?>, q# X. A! r- s- s2 v. y. I
<p>
( x4 }0 v/ Z9 O: I: ~* N/ ` <label for="notice-title">Notice Title</label><br>8 I$ h% ?6 H. [4 q
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">7 Y( L, o, N: Y& S7 K. A4 }
</p>
( ]8 |" S. f# L8 F <p>
$ _0 f- _6 v( j/ t& { <label for="notice-content">Notice Content</label><br>
3 H+ ? c7 k1 S7 N+ Z5 O; s9 G, g <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
- R- M- U* B% E* O$ {! i1 Z </p>7 r8 W9 a0 H/ ^, D1 L
<?php
/ @( `) A+ @& S/ K2 t o T }9 g% q- m% I; y: C7 l4 w1 _+ K
% m/ J& b* P' B5 B/ @' a
add_action('save_post', 'save_site_wide_notice_meta_box');" C4 U9 ?, r. u+ w8 P. C
function save_site_wide_notice_meta_box($post_id) {
: z8 x f6 f8 U3 r if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))- |' K: d% X& Z/ m/ X, t
return;% A7 ]. t, R; \: Q+ [
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)( g+ K( i, q% e
return;
8 H$ C& U. W) o
' ^" y# |6 k; c' Q( q if (isset($_POST['notice_title'])) {$ z' m9 I* r+ s5 h1 k! Y
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" O$ `" x& {) ]) S0 {
}+ C& r+ V! }/ u! A" X, u
if (isset($_POST['notice_content'])) {5 b# T2 C4 @& A3 d
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));& B* { @+ \* p+ _! f T+ f
}
) F+ U% v* U3 S7 x" { }
8 p; Z! O& E, |5 n ```7 i( ?& N8 B4 G: B! m+ j* i* o: Z
* C! Q2 J7 E7 u/ J q0 N 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。' ?/ \# ?7 q* o/ r( G2 q
) }0 y9 t5 E, G1 b
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:' W, E3 |4 y1 |& |+ b
) y4 Z. z. [3 k' B5 Q- J3 g
```4 D+ B6 n4 d5 }- ~( G- Z5 z
$args = array(
0 ]- {) y1 {0 U* q& ?5 Z) z 'post_type' => 'site-wide-notices',1 l3 v* X1 k5 c9 ~( b9 x
'posts_per_page' => 3,0 O `0 t9 c. e& _ m6 @; t- J
'order' => 'DESC',8 q: I# U! [$ S* O! C
'orderby' => 'date': J# x m/ x+ s7 ]' e' F
);
- i8 K2 C" k: |4 m+ M $query = new WP_Query($args);
; z) u/ }" }- Q2 d/ y+ D if ($query->have_posts()) :
; j1 u( ~, X2 Y while ($query->have_posts()) : $query->the_post(); ?>
6 g( P H2 {) D) w <div class="notice">
% i) Q' h+ I$ J' v2 D <h3><?php the_title(); ?></h3>/ w+ I4 K, f/ j+ U- A. K! J
<div class="notice-content"><?php the_content(); ?></div>( `: W) z3 y6 P3 ^+ D" C
</div>9 \$ R- v2 N3 Q3 B/ p
<?php endwhile; C* {5 ?$ u; M3 d7 @! V
wp_reset_postdata();
- A0 f# p8 E# B& Q. o3 y endif;
- Q" J& O& x, g( o ```1 d' ~. c! A. W8 w; H
2 ^6 t" s% z) T8 C 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|