|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
; o; m; Y+ o' m! \/ f, x! y& F. c/ b+ y9 c- p4 T! q
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
& I* d7 g+ |' z& V8 ~! V5 z2 r2 f) t$ ~2 ^1 `+ Z1 u' ]6 U9 r! a
以下是创建自定义插件的步骤:+ i/ m/ O3 z- i& K {6 p1 d
* b5 f% V( l0 K; B$ \- M
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:4 m5 H4 @' I) T# `0 a- j- p m
! n; o. K+ H* R1 A; |
```, Q9 q1 b1 K9 O# n. j2 ?* k& B
<?php
# [( ^: x8 b: s. M /*
1 M5 _/ _( L7 a: | Plugin Name: Site Wide Notices Plugin
; \4 P1 J) c" _2 X1 e Description: Adds a new custom post type for site-wide notices.% K# u5 A2 w# t, _0 l
Version: 1.0* J0 J5 F3 P9 v( S5 o
Author: Your Name: @4 I$ f7 f+ h& `/ S% D
Author URI: http://example.com
$ D8 P/ i% E( `. i */
: \7 m8 f. O5 W' ~- } I" q1 W' C$ u
" p. e. g# |; } // Add plugin code here...
2 N3 d) a9 E" Z8 ^; b ```; I4 z" f2 z% A! X' Z% F& k
& j. p. P, ^1 v, [
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
( Z. U2 C! i, o0 y6 M! ?( n* ]* t4 W' A
; a' ?% u: r/ `5 R8 D2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
* [- D6 K1 p9 m# H& r4 u* b5 s
( ~3 U8 q. T+ w* W8 ` ```1 S( f6 w% C3 l0 Q
add_action('init', 'create_custom_post_type');1 q( o9 D" K. G$ O% x
function create_custom_post_type() {4 Z5 e4 B2 N, O+ w- [
$labels = array(
) u* q! W6 N* {1 }8 m( m 'name' => 'Site Wide Notices',+ S1 a2 y5 x' R' o
'singular_name' => 'Site Wide Notice',
& B4 z; n6 R- S3 g 'add_new' => 'Add New',7 X! F: A7 X9 a6 f
'add_new_item' => 'Add New Site Wide Notice',
1 j9 k+ D8 C/ h 'edit_item' => 'Edit Site Wide Notice',
+ p% r r( o) p- B( F. g 'new_item' => 'New Site Wide Notice',. X8 ]# z$ n' S
'view_item' => 'View Site Wide Notice',$ _( ]. {/ f$ k( y" c& N
'search_items' => 'Search Site Wide Notices',- X# R$ Z! U7 \4 S. L
'not_found' => 'No site-wide notices found',
3 {, N n$ W. b# }- R7 H: } 'not_found_in_trash' => 'No site-wide notices found in trash'8 i9 N; M9 x% v5 V& o9 \
);3 l6 Z3 C+ }! J( ^
2 @/ d. o+ d/ A- `8 e* z $args = array(
/ A9 Y5 B. C$ k$ G 'labels' => $labels,; g) i% W+ `! r9 d- Q
'public' => true,. ~2 F6 m1 ]% e% z
'has_archive' => true,. l7 P4 T! K" b' i3 E6 m6 @
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
4 r' S3 Z& \8 u( r% y 'taxonomies' => array('category', 'post_tag'),
3 E7 I# c/ F T. D9 R" p 'menu_icon' => 'dashicons-megaphone',
( t" U9 S8 ^7 C 'menu_position' => 5,
, I5 q& l& {/ v+ L/ X3 D 'rewrite' => array('slug' => 'site-wide-notices'): ?" q+ ?. e7 V5 d; b
);& X" B4 d2 N7 _' I. }0 A5 S
Q, P, Y- p- R% h register_post_type('site-wide-notices', $args);
7 W0 o. P: H% l( C0 t5 U. f }
7 P1 ]! l; }/ O7 S' e$ H ```5 ]( [; D' ~9 V. X' O
; D9 ?% @1 ^# Y! o 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
* Y. i j9 G1 a( |0 ^4 L5 i5 Z7 J( @7 B; g. m
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
' [0 o% p7 R6 F4 K* x3 m
* T! ~, X7 a e9 Q2 B& c ```
" D1 S3 C6 ]6 u$ P add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
& I* {: E k4 `9 | K# a9 D function add_site_wide_notices_boxes() {
o2 A, C/ m, I. w* Z$ n+ w, t. u add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');. z. M. X" l0 ~7 A7 E0 @- `2 n( M
}3 ]8 n0 E% O2 b" V
* t4 Q" j* }+ o0 x% Q! Z, b" ^
function notice_details_meta_box($post) {
; o0 Y7 C* ~4 ~ wp_nonce_field(basename(__FILE__), 'notices_nonce');/ |5 X. {3 y5 M2 C/ y
$notice_title = get_post_meta($post->ID, 'notice_title', true);0 }4 e5 K, ~" [2 v0 m
$notice_content = get_post_meta($post->ID, 'notice_content', true);
2 v p. w9 F \( k3 u ?>1 A. L/ I2 d, ^( z6 [6 y7 U! P2 ]' J8 Y
<p>
0 W5 l$ \$ Q3 {' u, `* B6 U+ v <label for="notice-title">Notice Title</label><br>
0 A6 F6 e2 A1 y$ K! Q# @ P <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">: @4 e6 ?: o) Y/ c: b* s
</p>
- u/ |( u8 b6 j7 n <p>- ~" _* ?5 f4 r( R k' W1 G
<label for="notice-content">Notice Content</label><br>
- x9 X6 E2 J- r# ?$ u1 e1 | <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>7 k7 }" ~5 O" f+ ]
</p>. r/ y& U; e4 {" Z& I$ p! ?# s
<?php
{& F. K' M7 v5 C' K9 ^4 j- V3 V }+ N5 F: I# A' g* J0 _: s e+ A7 c
4 S$ P8 X0 C. G, E0 h j
add_action('save_post', 'save_site_wide_notice_meta_box');
2 {' r4 C, G7 k0 J function save_site_wide_notice_meta_box($post_id) {
8 W. n. J3 r* E7 \ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
2 K7 |6 ?) M) j" W/ c! ?7 v return;1 ]9 Y i8 Q0 G6 ~* ]
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE); ~! C# N2 |7 w- q6 U- m# _. s& V0 n( s3 p
return;
; g9 ^ K1 s2 m) ]
( h( N* e2 y. n if (isset($_POST['notice_title'])) {5 d2 y3 o0 ]8 q/ D/ R% k
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
7 ?/ M) K9 d8 ]$ F }
9 S7 x2 v% u! S. B8 r if (isset($_POST['notice_content'])) {
' A0 |6 m+ O3 i" ?6 x3 U update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
$ F W7 Y% {7 T7 m' i& j, w; k. J5 s }$ ?5 R) W- o7 x1 V$ G7 D1 I ~5 }
}+ R/ V& R/ O9 Y! p9 h1 I* ^: d/ o
```
" d' Q0 F0 _' u" f; M/ W7 h9 ^- \, ~4 Y+ ~: T. t
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。. g4 b( k* F s- Z" j5 |3 s
/ O# j2 t# `! L; V4 D/ l8 `' {6 g9 Y4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
/ l. l+ {; ^$ f$ c1 o& C
M; ^3 {5 U6 U8 D ```
7 J$ k1 j6 P3 J% \7 i% b/ Q6 ^9 L $args = array(% d' l4 U; B' Z7 v o o
'post_type' => 'site-wide-notices',% F5 [# P$ `: P1 U. j4 u
'posts_per_page' => 3,; n' L/ U$ m& |* H* w
'order' => 'DESC',
% l& Y. C" x+ x; d- C2 d 'orderby' => 'date'
4 K! i+ }6 w2 N/ D7 J5 _' y" H );7 b7 e7 L. W5 H$ m
$query = new WP_Query($args);
2 ^& D8 f# h5 L$ \! J if ($query->have_posts()) :
7 A. O5 b7 v, H9 Z) F while ($query->have_posts()) : $query->the_post(); ?>1 [8 C0 T: _' V. B
<div class="notice">
: K, B2 X3 r' F <h3><?php the_title(); ?></h3>
" ]0 h( p7 y4 H' w <div class="notice-content"><?php the_content(); ?></div>
2 Q1 T! o4 B& v& b9 L </div>* @: F( G5 l6 L) G" Z4 z7 U4 o% U5 H
<?php endwhile;% e5 H X J0 P% m4 Q1 n4 M: g6 c
wp_reset_postdata();8 G& U9 U% O3 P1 M
endif;
; H/ |; G; @# [) M6 d( O0 N ```% a5 x/ t! H, Y4 l
3 G; j8 {2 v9 h) T5 P7 j 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|