|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
+ L5 K0 A$ ?7 X' C. g+ D6 O& r7 y5 N( j' S# @! a2 a
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。1 T& H' M" l1 U! Z0 v. o4 \0 S
! i) y, i" D0 t2 c. m- `
以下是创建自定义插件的步骤:5 E) a2 G4 k6 a5 A+ G4 J* _7 I
3 k) D. H8 B9 H4 L" R6 L8 V+ Z1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:5 \5 O b" M6 d& G2 L! g3 t
# W8 X# j0 A7 z ```
+ M4 i% y) H) D6 Y# U* K <?php* R# U z1 m+ y' V1 l$ h8 g
/*% [9 y0 G! i ]1 a$ P- Q
Plugin Name: Site Wide Notices Plugin
' [' C$ v$ ~1 @6 }% L0 p4 A" i Description: Adds a new custom post type for site-wide notices.( P- v2 R9 I6 W7 C& j: U, r
Version: 1.0
0 p! I7 k( r& J4 m. O. m* F Author: Your Name! D" j [1 s" ^$ F/ P2 x5 s
Author URI: http://example.com
& n- K; i/ m$ D$ c7 } y, X; I */
+ N* O8 D0 W$ [
7 d. W, g) Q# J0 m6 ? // Add plugin code here...
' g6 X, @! m# _% i3 @ ``` R$ d( Z: r, n7 W. o
% |( `9 g9 X2 r1 `. d2 z
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
2 v! j2 z5 x) e9 [
) o. q% R K i- @; v# ~) d2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:1 E) p- Z! m `( v; F4 s, h
; \7 ]9 \: Y# q* h/ ~3 V
```
4 K3 h2 K6 y" s. E6 ]: w add_action('init', 'create_custom_post_type');
( D3 q/ h* \' @" R! H# x% } function create_custom_post_type() {+ _ V3 K( W# `# O1 k3 l! A
$labels = array(
) |* u7 T/ z% H9 L% E& n/ F 'name' => 'Site Wide Notices',) b& `' n% Z8 J: n; l
'singular_name' => 'Site Wide Notice',
& Q9 Q; Z! _. G/ P 'add_new' => 'Add New',
" b3 N5 `; r% m* d 'add_new_item' => 'Add New Site Wide Notice',, @. x' p; b# o2 D1 j2 D: ?
'edit_item' => 'Edit Site Wide Notice',! x% b8 @. m A! L
'new_item' => 'New Site Wide Notice',) \2 W, t% A3 E
'view_item' => 'View Site Wide Notice',
9 x5 h: g3 T6 f: Y5 w- f 'search_items' => 'Search Site Wide Notices',
; ]3 Z& ~# c+ [+ I/ w2 @2 f 'not_found' => 'No site-wide notices found',
3 y8 I' B- h" o p 'not_found_in_trash' => 'No site-wide notices found in trash'
; a5 @) E. h; O. b );0 v& c" c6 {7 j
+ L: X5 d+ N. Y $args = array(' l% f- @9 r0 n1 q* Y! d
'labels' => $labels,
# D/ ]! ^5 c7 |3 z. w 'public' => true,
! _4 D0 m: s: ^1 R6 W' w 'has_archive' => true,
' R9 C6 N/ i2 ]) i9 o/ o, h 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
8 @1 |. D9 | ^ 'taxonomies' => array('category', 'post_tag'),. |* `7 I& ]2 ~$ U$ Y+ c
'menu_icon' => 'dashicons-megaphone',
( F+ [. P$ t2 {' o. r9 m9 N 'menu_position' => 5,
( v5 q8 c) l. y1 q/ a 'rewrite' => array('slug' => 'site-wide-notices')" C! E3 M9 K7 {& H1 A3 @
);
. B6 f. h/ ~& o' J* g- | `5 d# C+ `. D. R1 [1 m
register_post_type('site-wide-notices', $args);
) w* ~* i% S' M" H" p& ] }
- \. P3 }, w- R6 H ```& Y& D! Q6 B. g. K+ X9 J
# z9 P! l# v U% b- V: E E
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。 o9 E" l4 n! P8 n% x' T; n
2 n5 E* O: Q( y3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
' ^4 e( i$ t' r. g& h' h5 N; E2 L. v) `
```: K* Q$ n0 ]' a
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');& ?6 Q1 l4 K) y$ [& g# i. C+ S
function add_site_wide_notices_boxes() {
6 U; j7 j, ~" E/ u+ o% v add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 U# z1 {2 ]- p6 _+ }' r7 m } ^. g8 N |$ j. e7 f# _
+ Q# S5 J7 ~9 W1 H% S0 O! p. ` function notice_details_meta_box($post) {5 N4 K! x8 @0 P) j8 w$ Q4 b
wp_nonce_field(basename(__FILE__), 'notices_nonce');
K! G$ P$ j, k# T7 \ $notice_title = get_post_meta($post->ID, 'notice_title', true);
* ^4 g* g9 c* S4 m0 N $notice_content = get_post_meta($post->ID, 'notice_content', true);
- J) b0 {/ m3 j# j7 o0 f ?>
$ p$ ?# ?. O% x3 K <p>
2 l# A5 y- {6 R1 l% K3 R! y <label for="notice-title">Notice Title</label><br>
6 n/ G. A4 Z. Y2 M' q <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
' @1 u) W0 e c2 Z- _6 {# x </p>* ~( g; Z/ U+ ^/ ^
<p>& A2 l: i+ c! b5 E& M9 l- h* f8 F
<label for="notice-content">Notice Content</label><br>' u7 l! f4 w- K; ^7 j& `( I
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>4 o0 k4 g5 c# r7 }1 h/ N* P$ J: L7 v* D
</p>
7 Z- U$ B: C2 x1 b4 E <?php* R" g; r9 A5 z1 h# q
}, _+ `9 t/ b% D1 H
/ y+ F+ \. l2 o5 x8 d add_action('save_post', 'save_site_wide_notice_meta_box'); [6 M& ~' S8 H& ?' t
function save_site_wide_notice_meta_box($post_id) {
! d; x" P" N- o6 T& w if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))3 x- w6 Z) X/ {) f
return;
2 U( |) C) @9 Y$ w5 _; `$ l- g# ^/ v if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE): O0 O( c+ n1 _* r, W7 Q5 V
return;1 L$ I9 C( | c2 W+ S" T
+ `( V, M# k9 e( D1 J) F
if (isset($_POST['notice_title'])) {4 k. q, f4 W! G# U& X$ f- ^
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
2 u" I3 Q+ Z, j' u L4 d2 a }3 } M+ S9 S0 H* t4 ?6 {: M7 g Z
if (isset($_POST['notice_content'])) {& A7 t7 |! A }1 v+ b1 n5 s' W4 t
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) N2 M9 w- ]( |) s }. P% Y0 t* T3 Z$ [2 m
}
8 d* u- i3 X) w* j; P, q ```
, @$ h& V4 X) }8 V t% l
+ M1 i; C$ N! t2 Q c) u" n 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" {4 P# y: V6 `
5 p1 }7 o% b- j b4 ]! D( Q4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% R7 g( C) @4 y2 F& P. x8 G1 `
4 R" {6 Q% i, I ```
6 d. g1 W8 O' u& q $args = array() M) S9 Y- A0 P* z
'post_type' => 'site-wide-notices',, N. g$ C3 \ x! C$ R0 P6 g8 H) m2 m: e! s
'posts_per_page' => 3,
* P- K+ H# J. L0 A$ j- ~ 'order' => 'DESC',- m/ q+ S6 R: y, a2 H* x
'orderby' => 'date'
. b) ~6 F* O6 s |/ m- x" Z$ B0 S );
6 R; W2 V9 ?9 O: m% C $query = new WP_Query($args);
0 B6 P$ S, l: O! ~ if ($query->have_posts()) :: ^+ k( L/ `8 w, s1 O8 G2 J, E" Q
while ($query->have_posts()) : $query->the_post(); ?>
) `% J! k0 O/ |) N' m <div class="notice">
" v( Y$ f- L( y0 a, s( y <h3><?php the_title(); ?></h3>
6 D) d( D& ]& O <div class="notice-content"><?php the_content(); ?></div>
" ?: [/ K1 @/ c, j </div>$ h, \+ a9 D3 S! `0 M& K
<?php endwhile;
! N( U* y1 R' h) l4 F wp_reset_postdata();1 w0 j3 F |- g+ w2 F; _
endif;
( `. ^2 y! t4 G7 |: a( X+ Z$ Z9 x ```
/ |" Z ^9 U1 g5 N' w4 \" _7 h3 i4 i9 Q* Y) M& R: _# P4 Z' Y8 S6 m8 P
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|