|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
; P! V o9 J; F5 W) y
# O1 Y# k2 g( S5 c如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。' X7 r2 V5 O5 }
- }1 i0 \( y9 j) r& u( e以下是创建自定义插件的步骤:
, Z5 ?$ e( F2 S# N4 ?# |: S) R
5 p3 R/ t6 r1 _9 m7 m: ]8 \1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
6 R/ r, W7 a; J0 W$ ?' I5 o: P: I9 s+ R) W8 n
```* X' ]: `, P5 e# ^, [$ _0 d
<?php
* \6 @3 l* P4 A! ^; R0 d# V m$ t /*
* V' u9 a7 ~- `5 C/ ?8 R! g A Plugin Name: Site Wide Notices Plugin7 X0 r: U- N+ R0 u% l" D* M
Description: Adds a new custom post type for site-wide notices.9 |0 l- g C6 V- A
Version: 1.0
, J) V. ]0 v# ]3 i Author: Your Name
6 h9 x$ A& B; P- }2 r Author URI: http://example.com
" u7 ~7 X7 O) {+ T( B/ C. z1 N */5 d' \9 N+ G' @
$ L% w/ B$ k. M // Add plugin code here...8 Q% A% y- ^9 E7 o; u2 \' e: m
```
$ ?) [% C6 @. |' B# u. n- Y8 I/ c, \
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
0 J6 `& |4 S2 B S% r
/ X8 c0 P; K& M& B8 J4 ^) _2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
! U* h, G, _+ T9 B2 ^) R8 u2 t; V6 D: s3 H. v
```- ]+ _6 e) t! r- d! C
add_action('init', 'create_custom_post_type');
6 H% Y' W, q; s# d0 Z7 w( k# Z7 I function create_custom_post_type() {
! z0 r; x; i; Z: Q0 r $labels = array(
2 ?$ @0 b7 K j. F 'name' => 'Site Wide Notices',3 d2 [/ U/ i& _7 \. j
'singular_name' => 'Site Wide Notice',, k- k/ ~/ A4 O" I8 Q" W
'add_new' => 'Add New', i5 G. }% T7 B* B! s: V
'add_new_item' => 'Add New Site Wide Notice',# Y/ B; Q7 |7 Z8 {
'edit_item' => 'Edit Site Wide Notice',$ O9 W, T5 H V$ j
'new_item' => 'New Site Wide Notice',
) f4 W5 L( j; E( S8 C) A G 'view_item' => 'View Site Wide Notice',; a$ ]) z7 c9 q' o- Z
'search_items' => 'Search Site Wide Notices',; Z- ]! \$ x! u
'not_found' => 'No site-wide notices found',$ ~+ [# B- p+ X" s
'not_found_in_trash' => 'No site-wide notices found in trash'
' V& q0 T# L2 |+ }- W );0 e' v/ J+ W5 \5 x& [4 A- m
6 T1 t1 L( N6 e8 v6 ]! x $args = array(: g$ ~) P5 p8 P0 \: [, c
'labels' => $labels,& c( m8 T6 a2 a* N! `/ Q N& f
'public' => true,
: ]! r8 e9 w# O: [4 B# K: {# \5 R 'has_archive' => true,
: [+ ^0 N& P" @. W/ a. N 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
- h! @6 |: K# u' v2 K2 Y. D6 a: a 'taxonomies' => array('category', 'post_tag'),6 s! n5 W+ d) x7 W6 O
'menu_icon' => 'dashicons-megaphone',9 j; _3 q0 ]" y- F" n
'menu_position' => 5,7 u( R& }& P- a+ ^# _
'rewrite' => array('slug' => 'site-wide-notices')
4 b+ G$ L! C+ @& s );, A- e+ Z/ W7 i/ g+ t- p+ R
5 H. K* i$ h! h) q7 j) ~8 J
register_post_type('site-wide-notices', $args);
0 v' c9 i3 ?5 y) q1 ]4 [( {$ d }4 L2 Q8 P3 i1 T2 Z; X: P
```
& v& G, s5 n6 d" M
- L/ M. z4 J- I5 U7 {5 ?8 h 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
! M! N# |5 h8 p- `& f- A4 A$ P( t# t1 {/ T
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
+ U8 j) \7 w# C) w" _
& ]2 d; z3 M+ J1 a# k ```
6 P1 [. O# E2 y4 ~2 m add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
u% D! X1 P. W function add_site_wide_notices_boxes() {4 h' E/ J! c6 d& [; k
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
- {8 y0 Y# T4 s& b } H# m2 Q. q3 W) B6 ~( i* Y
+ |$ L" x' d" C7 e; U5 ~
function notice_details_meta_box($post) {
, v- o! N" j4 f) _' c9 y wp_nonce_field(basename(__FILE__), 'notices_nonce');$ f: ?6 R& H0 T# d
$notice_title = get_post_meta($post->ID, 'notice_title', true);
8 H; X; w$ b! | \* Z4 \( B $notice_content = get_post_meta($post->ID, 'notice_content', true);
) A. O. I/ ~( O1 h; H! N3 z2 R ?>
" d; M2 i0 b( o$ U <p>6 N3 }* l7 m: D
<label for="notice-title">Notice Title</label><br>
, d; W7 S! T0 i4 n. q4 }+ v: d4 E/ v( o <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
; Q9 M! z! Z! e# S$ k* s. { </p>$ Y$ \1 ?$ O* e
<p>, ^6 w- ~6 {8 H& K6 G9 a
<label for="notice-content">Notice Content</label><br>
; z/ n5 a/ a( N6 n <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>) d: Z+ V1 f, B. R+ Z
</p>
. c0 K* m& h; v$ ]+ W% X, ` <?php
0 ^7 k i/ u; C0 V( A F }& B& C1 n2 O1 p: K+ s5 B
2 T+ s. d# Y! G( _
add_action('save_post', 'save_site_wide_notice_meta_box');
" ~ y- w9 G- [# Q4 Q function save_site_wide_notice_meta_box($post_id) {
$ }7 X# ~8 v0 ^1 z. }; n if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
" d8 v6 c& V, ] J$ L( M3 Q* M return;
9 C; V& e- L" z if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
' @# n. o5 {9 P2 d+ B1 Y) } return;3 p( {4 J$ h9 L X q- F4 t
Y2 _% Q8 d/ K) s if (isset($_POST['notice_title'])) {
* ^2 O% z. w! x( t9 a: F! k6 h" N update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ D6 i% J$ p5 R+ j& X% o( Z
}( T2 M# L6 k$ I
if (isset($_POST['notice_content'])) {5 w+ A% i) }3 d8 h7 T( p
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! H4 B4 u) F: t9 D' B/ `( ] O }( _ k7 A7 K- F( z2 c2 \' n
}% L m2 j# b4 e3 Z" m
```
; M; ^3 O; h0 V3 H4 t+ q/ P& j2 t4 R1 |: R$ o
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
) q( ]) P) ^ v3 ~5 ]
7 A4 V8 r3 U, t0 p2 J' S- y$ Y0 _4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:' ]' T& _* j8 }
# j6 @% _" P5 t$ N& m6 ~- l
```
- ]6 s' v% P! b% B h0 D1 R6 ^ $args = array(3 S( R/ ? u8 D4 L* h& v, o
'post_type' => 'site-wide-notices',8 l- }6 B- O/ i& `& k6 z
'posts_per_page' => 3,
- j% p2 S- |$ ? 'order' => 'DESC',
/ u! D+ ^( N) H, M& B* g 'orderby' => 'date'
; V8 Z+ Y% @7 D' B$ m) Q# \ );
! `+ |% ], u6 D1 M5 t/ h $query = new WP_Query($args);9 B, x7 D- L8 y* U
if ($query->have_posts()) :# ?# s. G3 z6 _" W c" Y
while ($query->have_posts()) : $query->the_post(); ?>& T1 v/ v2 L! N3 d1 Q
<div class="notice">1 r7 m* ~5 |! C& v
<h3><?php the_title(); ?></h3>2 ~6 l: N) r7 U3 E) [
<div class="notice-content"><?php the_content(); ?></div>
% N& ^4 k; l6 f' a, ]7 {. Q </div>
+ a3 {) b3 ]$ F2 \ <?php endwhile;* q; T* q' t4 V. T9 o
wp_reset_postdata();6 e0 t4 j. H9 n9 \5 R2 [! k3 {# r; H
endif;7 V9 }9 j) ]+ G1 ?; b3 }2 t$ Y6 w
```) z: N" U) I2 s" a0 V. ~
: L' M+ I$ t/ i 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|