|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
3 z& L+ n l: J$ t' a: J$ ]$ g# O2 p/ {0 u8 T& ], [' p
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
i [, S& \$ O* Z" f8 }# e* @! u& h5 n3 J, n) t
以下是创建自定义插件的步骤:# z% J" n2 |0 Y. _' N" x# s! M4 W
) u" e' e; h: i* |3 g! f+ q' k
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:4 H% j9 i* D7 I- c
; \) ~; u P. R" V
```; [, O. q) d4 g7 ]9 Y
<?php
+ c$ @1 D% [, o: N /*
- y5 O0 W3 v% D$ Q Plugin Name: Site Wide Notices Plugin
) Z+ l& N+ @: x5 D7 O8 i; _ Description: Adds a new custom post type for site-wide notices.
6 e' E6 K C. a# V6 p( l Version: 1.07 u6 k/ v) O8 l1 X6 D. @
Author: Your Name
! K' x9 f1 S1 J- B6 w5 c* [ Author URI: http://example.com- t+ X5 @! H' K2 ?* |; @( g
*/
, X) W8 s' x% [- u* g' Q) ~; m A" X
// Add plugin code here...2 B$ `7 Y! [" x N
```. |- G# Y) ~! E( k7 {' V/ Z" }% L$ R3 W
6 p( } u8 D5 n: M0 G) {; e- T 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
$ ^) Q% U/ U- l. Q0 q Q
- ~; r9 F: R2 ?- A- G3 B" Y2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:! k/ a# O2 _+ |/ k0 P* V" e
2 ~. Y: U9 v* l6 s1 \6 F2 j6 T% y
```; X; ?* i3 m# E+ P8 }( }& z
add_action('init', 'create_custom_post_type');
5 k- g: r& o, C# S8 c function create_custom_post_type() {
7 q, ]) j* V: o* Q# @ $labels = array(( D+ j+ V- i; c' g/ V+ N9 W- W
'name' => 'Site Wide Notices',
! {3 J( [: I# ?# F; H 'singular_name' => 'Site Wide Notice',
6 X% m% |8 c, M T 'add_new' => 'Add New',
, S: u0 [! q5 k2 P V9 ~* u 'add_new_item' => 'Add New Site Wide Notice',
8 q0 v: M0 p8 r' U! _: F 'edit_item' => 'Edit Site Wide Notice',
! W! I G' l. y/ p9 ~ 'new_item' => 'New Site Wide Notice',
' O4 C- J- d" L2 S) q5 R 'view_item' => 'View Site Wide Notice',, o h4 G; N3 G+ k* g: E# M: V0 q
'search_items' => 'Search Site Wide Notices',+ L3 e6 n- [9 B- ]. p
'not_found' => 'No site-wide notices found',* w2 W& q( W. h+ n' z' ]
'not_found_in_trash' => 'No site-wide notices found in trash'& s; ^* S+ h) h: l4 W/ S7 X1 n
);
* t `* [1 }6 u* ?9 d. U2 o+ Z! D% |" H, A7 Z
$args = array(6 f3 R0 L2 S9 H/ }
'labels' => $labels,
1 Z$ C: z( [& v. n3 f- U. ^ 'public' => true,
" t/ ]9 V p4 a- m( f 'has_archive' => true,- [2 ~- @" B+ o
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' D9 K% \' p, ~- t
'taxonomies' => array('category', 'post_tag'),& I* A# b, o1 q; T i! z4 Z( p
'menu_icon' => 'dashicons-megaphone',' o* L) p; `' t0 v; \4 `" ~6 L `
'menu_position' => 5,- I0 d$ i- {6 V3 J0 n2 P$ @. D8 M
'rewrite' => array('slug' => 'site-wide-notices')
' `% v, |# F: [/ s1 C6 j );: V3 K/ }* B h% O. l: s* L' E$ e
. V3 [8 e% ^/ C* e- P" S register_post_type('site-wide-notices', $args);* c9 x: J0 F' p4 { x% g
}' j+ ]4 X: l/ a0 t
```
4 I' [; }, z9 [! g) Q3 k6 ]7 B
/ g, P* c; d: f- R+ U8 Z1 b 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
: q, g- e: A9 ^8 y6 C! N) q! l+ [6 V! Z
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:' f# p6 ]9 f/ R+ L% T3 j* S
5 `+ D" ]2 y& I8 m8 I3 h5 a
```
) V3 Y% B& F) k) n4 W6 C add_action('add_meta_boxes', 'add_site_wide_notices_boxes');4 D& g* g6 q# p% P3 d8 B2 K+ U
function add_site_wide_notices_boxes() {* E5 m1 {6 g! |. J6 w& E
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' r5 H3 T- p" m4 X& O5 K" p }
; r" Y" r: Y3 s! m" J1 N9 @. Q0 W9 [8 |4 h( T
function notice_details_meta_box($post) {
2 P. ]3 a m, B3 J) `% a6 _" Z wp_nonce_field(basename(__FILE__), 'notices_nonce');
0 V8 P3 _2 ]- |) u! F: ? $notice_title = get_post_meta($post->ID, 'notice_title', true);
; C0 k+ g- f7 M4 r: n1 r $notice_content = get_post_meta($post->ID, 'notice_content', true);
7 Z8 }% c3 b* a- Y+ I# `6 F ?>
; T: c! D+ [3 U <p>
1 I9 K+ ]# @' H# L <label for="notice-title">Notice Title</label><br>
2 U+ F( i" T* ]4 p, c <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">) r0 P$ A$ G' R
</p>
& \, e( T6 e \0 U7 ]% }# v* f <p>
0 k5 ]+ u: T7 _# r+ @# t+ \% y <label for="notice-content">Notice Content</label><br>
& R1 t; u" r5 ?: E; |+ |9 u <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
+ u+ j% \+ H# E </p>4 e: ~% g, G& R- A* ]5 c
<?php
$ |4 u4 Q" O3 M9 j! t }- S6 J- Y6 c+ v! s8 v+ I
& u4 i# R# R% |( T
add_action('save_post', 'save_site_wide_notice_meta_box');5 e+ D) `: ?% g d# M! L6 L
function save_site_wide_notice_meta_box($post_id) {7 b5 |. y, J& @
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 l, Z1 N" u+ n* r, d* y return;
, g' `7 `2 r6 `& H if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- W; F% V: `6 [5 W7 T return;: q3 d0 e, D# U3 x1 r
3 n6 W5 h- x" h, f9 ], x7 s7 ]
if (isset($_POST['notice_title'])) {
; |% l0 d6 z7 f+ T1 G" D update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
5 v5 n: |! |0 Y+ k. t8 p9 \+ K }& A5 S. E5 d8 I' U" G
if (isset($_POST['notice_content'])) {1 y; g0 ]+ O% f+ b* ^
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));: k& ?3 {2 G; ~
}
& z( M2 f+ F/ y g3 R* \5 b+ F }
- k7 h/ a: t" R7 K$ E" H# d ```
' ^& N+ C# z& C2 U0 D+ q3 x! H& s* {4 P6 ^9 X% B$ }6 E
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。! e3 m2 |) T7 s, G% Z- o6 }
" E0 C7 B Y7 L6 W: E
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
( h7 Y# t! ^# y3 Y0 E% S6 u- f+ A0 i7 a
```$ \+ w8 ~% }. P9 q
$args = array(( M$ U8 [8 P( Z: p
'post_type' => 'site-wide-notices',# _0 f2 ^8 \; W, H
'posts_per_page' => 3,
2 s9 c& E. @9 D. d* o 'order' => 'DESC',* o$ o. n9 K) E( |- L, |% o
'orderby' => 'date'
. P% d: G4 H, V( J! e- l );
" i' b: g4 t' Q" j( P- {2 C $query = new WP_Query($args);0 v$ V F6 h ^ x; e* e
if ($query->have_posts()) :2 S2 T( |" R- v+ ^
while ($query->have_posts()) : $query->the_post(); ?>6 d; |2 B a r# V1 j
<div class="notice">
( V& |) `: N F. Y2 } `6 U+ g <h3><?php the_title(); ?></h3>. T9 s2 l6 `: G* E. Q
<div class="notice-content"><?php the_content(); ?></div>" p8 F9 _$ h" s8 A @; v) d
</div>
7 G4 \! o2 K( a2 u <?php endwhile;
: D+ D) C. a& N; ^8 g; j. [ wp_reset_postdata();
: i, r6 K1 S+ g' z# B, w! i# a1 x endif;6 ~# d3 t! \# S' Y2 }5 T
```' f. l; i, H+ ]7 K) e% G
( i% E8 y. k- X: p# O* F1 b7 g 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|