|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?* ]: `& v9 j$ E7 ]+ }0 g
1 Y& j) Y0 e: N% L. a
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。) E9 s- u5 i/ b3 K' ^ B
, K t* N$ n: w" o2 \" X
以下是创建自定义插件的步骤:: U: R4 F( a4 s* A5 C/ O$ m3 m
5 U; a+ F8 V, z; c# p# H6 \2 T7 f- D1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:' ` u: H) `2 a- h: I9 K8 X# K
! J& z+ Y8 a5 I$ t) U) w$ b2 d& C- { ```3 t! S: e6 i. z% x( P
<?php
& F5 `3 O2 w& d2 T) U: c /*+ o$ x& ]* F' K1 L# h3 z# `
Plugin Name: Site Wide Notices Plugin) Q5 e% i- j' b4 }1 o2 W. N
Description: Adds a new custom post type for site-wide notices.
9 u/ [! y5 x1 i Version: 1.0
: \4 a3 q5 |3 A O- o Author: Your Name( K; Z' v, }' S
Author URI: http://example.com
8 h, b( \% ^" y q */% S$ O4 ^2 e% B' L4 A
9 ~+ i+ ^1 h7 Q7 a // Add plugin code here...5 s3 ~- P+ n) Q5 M9 K
```. i$ c' W. ~4 b$ B$ j+ F# l
" w1 ^$ `! y! M$ x+ B
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
. ?7 L' B/ d. Q5 b5 s0 O f9 h! }! K( u$ q L
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型: N) I' f4 H; |; Q
# {$ d5 w1 x) E' o9 f9 Z$ L( ^
```
% F2 ~" E# t& P1 R) E add_action('init', 'create_custom_post_type');- z9 g/ E$ Y$ L M0 h8 Q, ~- H% C
function create_custom_post_type() {& p, f( B Y- N ~# Q2 ~$ R; t/ V( o
$labels = array(
/ E1 y. | u% {7 R+ O! ~ 'name' => 'Site Wide Notices',
0 G- P3 r5 e; A: f 'singular_name' => 'Site Wide Notice',
\5 X5 T! H/ P) B0 h* y* d 'add_new' => 'Add New',
' X. s9 t: \8 d2 t 'add_new_item' => 'Add New Site Wide Notice',: N; V* X7 `& h% s W
'edit_item' => 'Edit Site Wide Notice',# j' h }+ q/ H# f4 J
'new_item' => 'New Site Wide Notice',/ U" n( I0 O0 ~6 S3 g* b
'view_item' => 'View Site Wide Notice',
. d7 |6 t$ N# e z; e" m# ^7 e 'search_items' => 'Search Site Wide Notices',, ]' u/ `2 r3 r, R5 {- Q' i) _0 V
'not_found' => 'No site-wide notices found',. j$ r! v& `9 g( n
'not_found_in_trash' => 'No site-wide notices found in trash'; x/ T9 C2 c, ]4 G2 \8 x4 f
);
( I- j; A1 j R0 u$ z: p. p5 R' q3 B8 A
$args = array(0 d( @; D B6 A
'labels' => $labels,
1 y2 l9 }$ g( h. e7 p; B5 r 'public' => true,
7 F X2 R# f- N( @ w 'has_archive' => true,
# H+ _& h K4 _ @/ r, }5 Y 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
* a$ B" A3 Y9 f; m* m" c2 ? o 'taxonomies' => array('category', 'post_tag'),7 M' z; y7 k* N, d* H5 v
'menu_icon' => 'dashicons-megaphone',* K) B7 `. W9 I" G$ P8 L* A
'menu_position' => 5,
7 R: ]. L) }+ b 'rewrite' => array('slug' => 'site-wide-notices')5 L' v& Z* q# ?$ X4 ~ X
);
4 r1 \$ b8 s# i \+ e1 c5 B8 u
. j- @. N) g0 t# t; H, I* Z, t register_post_type('site-wide-notices', $args);9 m3 i! M% ~6 F( o$ t9 b
}8 K3 E2 f8 d& [# T- L
```
3 D, b8 `8 Y( j+ C9 n- r( }
- B6 U/ N$ V$ \5 y4 b( F$ l 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。. K2 c! m% K+ n+ ~- S
& F9 d* y1 v! `: ~3 |4 Q
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:0 @ V$ I; a( g. X7 S% R' z9 w
1 j# |9 Q; c, G$ i- s! Z
```' ^7 _0 u# F9 b' Z) Z* c. h8 o
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( |8 s0 v* i; N* d$ D2 E4 c/ v
function add_site_wide_notices_boxes() {7 `/ d( R# D4 q6 l, W2 n
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
: s$ h0 O F4 a }
) d% |, ^1 l! ?
) y0 F# H1 o. A! i; F3 P function notice_details_meta_box($post) {
N# l+ h+ G j# J. g wp_nonce_field(basename(__FILE__), 'notices_nonce');
( ?+ N8 M% h7 ?& B: z0 \ $notice_title = get_post_meta($post->ID, 'notice_title', true);
! N- T$ t# S( W c6 P {9 B $notice_content = get_post_meta($post->ID, 'notice_content', true);# o' n' Q8 @( c" \$ D
?>7 s6 z% H) v# c
<p>
1 t! ~3 E4 i/ V) m- q+ {* t& c, n! M <label for="notice-title">Notice Title</label><br>$ f {6 J' _& k# [) g, F
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
8 r8 t5 X; `- p" o( Z </p> ^) C/ a5 M9 i% }1 S9 A
<p>
1 z- y& O; Q4 u2 [8 f( {& e <label for="notice-content">Notice Content</label><br>
" x0 W( o; i { <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
0 i8 Y+ l/ c1 p% n- _! m! h </p>6 J+ d6 o1 u/ S* k( Q
<?php
$ Q3 w6 V4 J4 K/ Y' P/ t7 B- g }& V+ e# n( K- K8 y# n
: B# n& ^! N" O9 T) Z$ ~7 ?8 a add_action('save_post', 'save_site_wide_notice_meta_box');
* S( x+ i7 B9 ]- ?' E; ^ function save_site_wide_notice_meta_box($post_id) {/ G1 p8 g- a6 [6 ^% v1 r1 \: C6 P! _3 x
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))" i1 r& i: \7 W- J% f$ d
return;! ?2 f, ^, t# W& B8 d) X( K/ A+ V
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
6 [0 P5 `8 o0 r/ m& j return;
* ?* v3 f3 k9 I; J* w* P4 E( K7 Q" B& l
if (isset($_POST['notice_title'])) {3 q& C4 i* p% e* m
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
p( C6 `+ w6 T: d; U8 O, B }
: v9 ~! Z& D" ~, u/ g if (isset($_POST['notice_content'])) {
1 y. x1 z7 v# v% S! `/ \ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));8 @5 H* W, K* P( v5 [5 Y
}5 i, K# ?; D! n& p% t# B
}% ~/ l# O& D# _. i3 H5 H- {$ p
```
, w4 t3 q1 B }1 J0 y7 L% ?' ]
2 h @: X5 b( o0 X# T6 J 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。4 C) g3 _/ K J A
1 y0 }: R7 t6 _) A3 ]3 `4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:/ r/ w2 m, z* }3 } L
. ?: l; x! a* n6 w* y0 ~2 y
```8 O7 g* P5 k% |7 u2 N% y: `
$args = array(
0 B* ?0 `: n- P& ~ o 'post_type' => 'site-wide-notices',
# R; p9 J* z) d! V 'posts_per_page' => 3,2 Y3 F: B+ {+ H T3 d6 J
'order' => 'DESC',
5 m2 T5 ~. N" ^ ]+ Q( S3 Z 'orderby' => 'date'6 T" {; @/ Z, a$ ^! ~. Z
);
5 c0 X1 Y/ A5 E1 W- | $query = new WP_Query($args);0 M" a4 ?6 e4 s7 e- _
if ($query->have_posts()) :/ P" H6 c; F$ [2 t
while ($query->have_posts()) : $query->the_post(); ?>
3 b4 g& O- M ]( w1 Y <div class="notice">& x7 _/ L$ S' v8 M5 i8 J, F
<h3><?php the_title(); ?></h3>; s1 }8 n4 p' R' t# Q- N& f
<div class="notice-content"><?php the_content(); ?></div>! x/ r% U+ G+ u9 V' J
</div>, m; i4 f( l: d- Q9 @7 J
<?php endwhile;* s+ u+ a$ e% A8 F. Q0 A
wp_reset_postdata();
2 n! t8 K2 x/ n) H6 L6 n endif;3 m# q+ p/ u0 O8 r3 [' l+ `: |1 T
```- T/ F+ Y1 S3 n2 [; w8 _0 X5 `
; T3 t( b$ |, @, m4 c
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|