|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
3 Z, l6 s! @' R6 o, t `4 Z# z& ~9 G9 L0 A7 L* @+ N, o/ h
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。8 h" X. ~" K5 B
# b/ c0 e) p/ p4 P2 @
以下是创建自定义插件的步骤:
! l9 A7 L* ?% B2 \! t2 r4 K' ~
0 z( n/ s9 l! C- {' B* W8 j1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, c% v' D* B2 L; B0 ?: o( I l
2 ?0 h3 b4 _+ |7 j ```- P# K! y& L3 ?2 b5 T
<?php
, i; w1 S/ s9 P# \7 W/ w" }: v. g: w /*7 U, R6 U$ a0 V& t/ q* c( n7 n ?5 R- h
Plugin Name: Site Wide Notices Plugin+ t- O% L+ D4 a5 L3 A9 u6 J
Description: Adds a new custom post type for site-wide notices.
' {; W( _0 o( A& Z& q/ N Version: 1.0
1 S) W X/ j( ~ Author: Your Name
6 X9 k8 R6 p2 p% m! n/ r Author URI: http://example.com8 v: a$ ^# V3 |& {2 S3 ^, u
*/
( ^/ `. h, ?" [* Y7 f
7 k( L7 B1 a5 n H. w5 n* t // Add plugin code here...
. ^' D1 P0 o" Y5 s ```
$ C9 _8 s, Y' T3 M) R9 x! u$ g
7 q: ], _8 d3 F# q, T& S/ w 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
' h9 M: N7 R/ {. N/ F9 q, a1 e* F
. @/ u2 V: J0 _2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
/ M6 d7 i* z( Z& o0 j& R; |5 o# a s3 y2 I
```- [8 H* @9 c) C
add_action('init', 'create_custom_post_type');
% h4 B8 z }" j% G3 d5 b function create_custom_post_type() {
! u8 E' p( @/ O0 U+ Z5 Z $labels = array(0 K- N. y/ |$ J7 k5 E, c
'name' => 'Site Wide Notices',
/ P" ~; h6 |6 ~9 n; L6 x, R- ` 'singular_name' => 'Site Wide Notice',
" V( }& d+ e+ b) N 'add_new' => 'Add New',' _, R& [$ o u% |6 @
'add_new_item' => 'Add New Site Wide Notice',
2 G( F; u7 X- J' s6 x 'edit_item' => 'Edit Site Wide Notice',( T/ V0 l& o2 M$ ?
'new_item' => 'New Site Wide Notice',. u- V2 G* W6 ]: U7 t3 H8 o; O
'view_item' => 'View Site Wide Notice',
) M( e1 k' M+ ~0 Y 'search_items' => 'Search Site Wide Notices',
4 t/ f2 y- k9 S' }- C1 o& _ 'not_found' => 'No site-wide notices found',
: r5 m# G: Q" u" \ x p; g2 y4 ` 'not_found_in_trash' => 'No site-wide notices found in trash'
2 ~( ]6 S/ q" c );0 _ ~) M+ F7 w) z
' O" p4 L0 B( n $args = array(
, y" c) A( T9 K% a! g+ y4 m" L ` 'labels' => $labels,
! ?5 V$ m7 P8 D! L+ } 'public' => true,
) b! ]2 K6 s% {; s! R2 C- h 'has_archive' => true,- {8 Z+ L0 G/ Z7 W: ]2 D
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
3 n# N0 T! Y6 y# d/ S- x6 y 'taxonomies' => array('category', 'post_tag'),
$ d8 q5 F( j3 ]4 u" w6 m 'menu_icon' => 'dashicons-megaphone',: t8 `9 `, T# h" C
'menu_position' => 5,
$ g: ^2 z2 {; q$ c$ F 'rewrite' => array('slug' => 'site-wide-notices')) K) W, N4 X2 X
);
' Z0 r) H: }# b7 K1 Y+ v; c
* c% |/ D% X6 B# v3 |6 \ register_post_type('site-wide-notices', $args);0 h* A9 @5 m/ a
}
% O& T" C2 j( Q& W3 p5 |1 ` ```
+ u2 u" _9 r! R- ^$ z. x( \* \% z3 |7 d4 B. V( j: I
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。0 ?/ j) _7 z& b6 n; z
9 `/ p& J- `* q6 L$ k$ x3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
; W5 l9 {) {8 I; ^
: F$ H& T8 ~+ u ```
e" J+ I0 e0 {, l add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
6 o7 l' [! p; q/ H* D; l function add_site_wide_notices_boxes() {
1 o# }$ T# V3 P9 m add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' x$ y# c$ d0 R5 o }
6 @8 I$ P' S' r' \
* l! h4 v, g9 ?1 l! T function notice_details_meta_box($post) {7 m: y$ p v5 k8 V1 a: X! a* d
wp_nonce_field(basename(__FILE__), 'notices_nonce');
: b: T; e$ ]% c5 r5 ? $notice_title = get_post_meta($post->ID, 'notice_title', true);
0 P2 d/ j) Z7 W4 A& d3 p, l $notice_content = get_post_meta($post->ID, 'notice_content', true);
2 D1 T7 z5 Y1 E% U7 W$ i6 X: c ?># z- _6 `5 V; z0 {6 t4 E- _
<p>/ H+ a) C: L B# T( f, X1 A
<label for="notice-title">Notice Title</label><br>
% R3 h# M$ Z9 z% B. H1 h) U <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
- f: m4 Y# `6 c: z5 Z' _$ t </p>
8 _3 \/ n+ T' D. D <p>
% D2 f4 M. u$ X' A1 t) H9 f3 n <label for="notice-content">Notice Content</label><br>
% f' D5 g! A c+ U% n <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>$ g3 _9 ^7 _ s
</p>5 U, {' N% n# _; [4 q; V
<?php6 C, }4 M- Q7 a% Q! J& ` |
}$ U+ x6 f* G4 f2 c) J
0 V1 q9 e6 l# h0 m
add_action('save_post', 'save_site_wide_notice_meta_box');
- g' ~) y; a( y' { i6 @ function save_site_wide_notice_meta_box($post_id) {
: e& o& E2 U7 u1 X4 ? if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))- T$ ^5 E; X4 P1 ^: N
return;: n" w0 Y3 p1 ~* f
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
) t$ f* Y, h/ G) N6 S% i7 R% L return;, a% U5 F& G) O2 e. q$ N) w
- b' z" s7 K4 I F if (isset($_POST['notice_title'])) {
1 E- x" ]$ m8 Z; V1 g+ d update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));/ F z" j9 |1 k) t! _' d
}6 U$ j6 [- ]& R
if (isset($_POST['notice_content'])) {
7 ~4 i1 e* P; }- D R update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));) H8 f5 C2 c, S( B r0 D
}* D. X8 v* q/ i V v
}* o% k! K' e9 ]
```
% N5 Q- j( s3 ?# Q5 ` a8 N) p! `- n& w! z6 p
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" d6 b0 G" Z9 _- V# h5 L! K% N( O0 `5 n0 _0 r7 @1 X) [3 c
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告: ~: w! ?4 ~+ w. O3 |! `
5 S7 |4 P6 S0 D. Y" b ```
& q- `$ V6 @: a) L6 p $args = array(7 E( |: w k7 g- i
'post_type' => 'site-wide-notices',% b( L% x: _ P; I& c y. B% P
'posts_per_page' => 3,1 r M7 M5 Z% \( {" Z8 {
'order' => 'DESC',. r1 j. Y" G1 R/ Q/ B0 p
'orderby' => 'date'1 I. }' ~+ j1 `7 w
);, u% o1 G3 K+ M8 Q% [1 B) v9 T6 L
$query = new WP_Query($args);
4 f8 ~8 i0 y* D: B if ($query->have_posts()) :. a2 ~2 j6 H* ?9 y
while ($query->have_posts()) : $query->the_post(); ?>; U& }- S) f/ D: i3 G
<div class="notice">
7 N: m0 l. t: b$ ^ <h3><?php the_title(); ?></h3>
! s9 U. y( i: ~9 z: C* \0 b2 e; P <div class="notice-content"><?php the_content(); ?></div>: M* K- o' u4 y- T7 h' i% _2 l
</div>( Z4 f3 w9 x5 R5 b' y9 T6 b
<?php endwhile;2 v, G! D6 L# ~
wp_reset_postdata();$ p% p+ V4 h5 m3 C+ c: E g- N/ `
endif;: o& x. [3 B& y; d6 A1 c5 H/ r: z3 C# T
```
! ?3 @3 ]' E; F' _) }4 |# H [+ I7 e( X
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|