|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ O+ u( W% j+ ?
& o) `8 C2 p( f# |4 K
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; n1 x' O5 _9 y! g
0 V* {- o% e% ]1 [. [以下是创建自定义插件的步骤:, ]1 e9 c! N# e) u8 A2 ]. i$ E
' o5 F! b) N' V' U
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:/ N1 o2 d5 U" T: z5 v
" m/ j+ G' @# F
```
* o5 i$ H5 m7 I L: J7 r' P <?php
7 d1 X, t) W5 v/ {5 z% L /*
/ I6 i8 S! c. a \ Plugin Name: Site Wide Notices Plugin2 D2 X% o; B+ f
Description: Adds a new custom post type for site-wide notices." R6 w8 g5 E3 q1 R6 A6 o
Version: 1.0
9 ^3 J h4 G) }+ H0 ], A Author: Your Name, R- h. y9 @! N7 n
Author URI: http://example.com9 _, W |2 C% ? L6 J
*/
2 E; T" f' Q: H8 X3 f& ?1 ^* N% ~8 P$ c9 @, z2 e1 C2 U
// Add plugin code here...
9 m& `( B8 `. I: e ```
% ?! E8 a2 ^1 I% a
8 B0 K: I; }9 N+ i& Q! C; S 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 L- m. ]8 O1 ~9 x% Z6 [/ B
! Q6 _% K1 B. l& V- ?2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:7 T/ V* o! a, v8 A+ m( h" E3 u
" d& i' `, p4 @' p! Z! t2 d; f
```' J0 F" N: w& J8 \/ b
add_action('init', 'create_custom_post_type');
% w* J1 {8 r0 e3 P function create_custom_post_type() {
. @! t9 d& u5 I3 t- l+ V, k $labels = array(
( ]' ?8 U$ g9 p2 [1 B 'name' => 'Site Wide Notices',8 E. C1 _; G& o$ u: _
'singular_name' => 'Site Wide Notice',0 B# b4 l' q' a* N) `/ N' L5 r3 p1 T
'add_new' => 'Add New',# F, o5 T2 U2 l: D* T( a6 x
'add_new_item' => 'Add New Site Wide Notice',: W- r" [+ r% Y
'edit_item' => 'Edit Site Wide Notice',4 S, z- v; ?4 b
'new_item' => 'New Site Wide Notice',- a' A: _- J& ?
'view_item' => 'View Site Wide Notice',: y4 b( \- J5 B4 f; l' J
'search_items' => 'Search Site Wide Notices',
" W1 Y) c. w+ n2 B- i 'not_found' => 'No site-wide notices found',- S% ~% o- Z/ C% Z+ V/ ~% f' \
'not_found_in_trash' => 'No site-wide notices found in trash'
4 T% y/ p9 j3 f );
& H- q0 G$ j. R3 d! J
# S Y. V: r3 M- g $args = array(. o- Q% I: A0 E# {
'labels' => $labels,
; f w/ L8 v8 q" M5 R6 O) T } 'public' => true,
: ^# R/ o* V$ X0 c2 V" `" c 'has_archive' => true,
- c& |( T$ w% }, ~ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),4 E9 B# R8 M/ A& Y6 Q
'taxonomies' => array('category', 'post_tag'),. `* Z$ ]$ c1 [0 w0 k8 d) H7 b
'menu_icon' => 'dashicons-megaphone',
. F3 X% U2 u- W5 l 'menu_position' => 5,
^; h9 _, _ X2 R 'rewrite' => array('slug' => 'site-wide-notices')% v0 R1 U( ]! z4 G9 B' |
);
, f9 _: d9 Q+ X% z" o9 f6 E& f* R2 S6 Y# p
register_post_type('site-wide-notices', $args);
. V& I) c- r. ? }) ^# U3 J: X n* s; H
```) q8 \/ {$ I( g$ m, B; w+ c7 \. b
2 M9 E$ b6 U: t 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
9 ~6 `6 D/ y8 z, k- U/ @9 \
- a4 }$ A h' P& n }5 n8 w3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
* {4 B7 R: k4 N( N5 g
" }* ^- C/ f( q ```
* ?5 Q; R- B- E& }; v add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
1 _% u6 _$ l9 i. _- I6 Y6 K function add_site_wide_notices_boxes() {
! ~% ^, z4 x W$ V8 G add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');% @3 r; D4 `! _ G- G& v0 k' j9 }' [
}1 O7 T: e X/ I
- {* @% C \4 t4 F$ d2 p function notice_details_meta_box($post) {7 R. ^* A/ d# T% Q. d
wp_nonce_field(basename(__FILE__), 'notices_nonce');
5 b1 J: e/ m* Z $notice_title = get_post_meta($post->ID, 'notice_title', true);
0 K7 T& |7 {$ k! k $notice_content = get_post_meta($post->ID, 'notice_content', true);
% N$ z6 o$ d9 Y f; ?3 c- z# } ?>
; D3 Y* r, R6 ]; u# }+ d <p>
0 V! J- Y5 M! F R8 n <label for="notice-title">Notice Title</label><br>
9 O6 |* W; b" v* }5 I2 c <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">% [8 e* l- e7 r. ~7 i& l
</p>
6 D6 \# ], N% a' n <p>
% ]$ H9 e; e( u% E <label for="notice-content">Notice Content</label><br>
1 U# a2 ^7 }5 | <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>" p; j B2 ]; t4 S
</p>
/ P) X. z4 i2 t <?php B# M, a- b* w7 x1 [
}
+ M$ T. r% u7 w3 S" S( D$ Q1 K8 y' |5 E
add_action('save_post', 'save_site_wide_notice_meta_box');
$ s# Q7 M: O/ t: o function save_site_wide_notice_meta_box($post_id) {( A5 I/ m1 [! @' R; b8 c# ?/ _
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))5 m' e* ?6 Y) q4 |
return;) X+ \/ @% W( e' N7 a) p- @# v
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)6 X! S7 ?+ p* a2 d
return;# ?: \% \% Z' ]" @
/ j8 T2 q( H$ W, J* e2 B, H, }1 E% s9 ^ if (isset($_POST['notice_title'])) {
7 \: d9 \, q, {7 n* A( [ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
w! p) _# e9 N7 |& M4 a }# D3 }* G9 Y; J) ` U3 e; e) J
if (isset($_POST['notice_content'])) {
) Y8 ]/ [. d/ J update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
; K h; m$ B5 N9 p, l }- S1 l. z! P4 a" ~7 K5 E: i
}
: \% d' O. `+ |" C0 x/ k# Q ```
* T6 w5 }( N( Q; g" P; c1 o# v% N9 A: [% l$ m
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。# F9 |3 u' X- z1 S; \
2 `1 y7 f+ I$ U2 w1 d& @0 L4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
& v2 w* O5 f" u+ F
% A7 w, p$ r, E. m ```
* a+ m$ R# ]' h% p6 w $args = array(
1 c( \, e4 f7 Y; Y6 C" m0 G2 p 'post_type' => 'site-wide-notices',$ A- K, i5 F* H8 Y% \( F
'posts_per_page' => 3,
3 T( E/ f0 \2 O& _+ a 'order' => 'DESC',1 N' \/ ?: }6 @# g2 ~8 V, R" k
'orderby' => 'date'
$ {" t/ g* Z& v: M );
0 S* `7 R4 k z $query = new WP_Query($args);
& Y4 z% e: _; Y: O G+ B, O: b if ($query->have_posts()) :
" j3 S, h7 ^/ ?0 T1 ~. B( Z while ($query->have_posts()) : $query->the_post(); ?>
6 A8 P5 v8 b& I$ ~ <div class="notice">, p" h& ~! R! P& G5 h
<h3><?php the_title(); ?></h3>
2 w# \: W3 f& z4 A K <div class="notice-content"><?php the_content(); ?></div>+ R6 S, Z# f. f& ^
</div>
* L! [8 q0 k; q/ Z6 \" _$ ]4 A! J <?php endwhile;
- w& R8 r3 }, z wp_reset_postdata();: l* ~$ g+ T- f( ?4 h' ^$ P
endif;3 H6 d( D% Z; q) d
```
0 n h4 ~7 o1 k% K, V/ b; P) |; n6 \3 P& M* |
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|