|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?% s5 a' E2 d( I
% c% J; R* h0 t- j" n如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。, X6 N; R$ S7 y! f7 X4 {# ~
) W0 {- Z) W4 Y1 J2 D以下是创建自定义插件的步骤:
' D) C4 `) D& {" b$ w
|, C4 X Z; n4 N' m+ y2 b- e1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:; D s; P7 t9 b+ r
1 D: C" X- }4 d8 N, \2 a& [+ I
```
# f5 {) f3 R* O9 n2 `5 C8 c <?php
/ f5 ?7 i( D* t9 Y /*
: m/ P3 ~$ b9 m Plugin Name: Site Wide Notices Plugin
& `! m# m$ p* _+ t) w Description: Adds a new custom post type for site-wide notices.
, ]/ H9 R7 } V% @+ G1 E% E Version: 1.01 c9 o# l1 I, T& [/ v
Author: Your Name& b! _' c4 c/ d1 a5 r' ^( r
Author URI: http://example.com
+ n3 |) L! C. w& I */
% e- J$ [" [+ ~ ^" e+ b4 }! X( X3 I9 I; w/ t& a9 X
// Add plugin code here...
! Q5 R- q# q; S# e4 }2 {7 K- K ```
: o( B& l" _- W
' n) k6 ]( h) \: ^8 f 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
4 y! f4 C8 x v7 O ^) Z# R. v7 |4 m; q9 d3 T* D
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:6 m5 T$ n4 G* ?! E- F) w, D3 T
. D" G U; }7 C8 B# d ```
( E# V; k" J' c& {6 l! v. ] add_action('init', 'create_custom_post_type');6 B T$ \. A5 e8 b! \( m3 ]
function create_custom_post_type() {6 H# K/ ?- H3 E# z4 N6 Y
$labels = array() ]# o: y2 D$ f/ M0 u
'name' => 'Site Wide Notices',: ^7 a8 g$ ~& j. O$ i
'singular_name' => 'Site Wide Notice',
m' x/ N- e2 T, G% C. q" ] 'add_new' => 'Add New',
5 i0 j+ ]* U9 J* @ 'add_new_item' => 'Add New Site Wide Notice',
3 b- [8 B+ X: |7 y- J# x1 ~ 'edit_item' => 'Edit Site Wide Notice',
, n" B$ D( O: A6 i( U' q: C, D- x 'new_item' => 'New Site Wide Notice',2 J5 P: k8 a5 t0 E$ Y+ P
'view_item' => 'View Site Wide Notice',
; a# |6 e R9 S3 s. q% n0 C8 B( w 'search_items' => 'Search Site Wide Notices',
9 t. C0 r, w! k- h# }( u 'not_found' => 'No site-wide notices found',
+ n: E' G& T: z+ j, p7 b 'not_found_in_trash' => 'No site-wide notices found in trash'4 J; C3 d1 S+ @6 f6 a7 I
);) O, T- w, d1 b) T; W+ [( l& [" F
" q- v- g. j# K
$args = array(8 h8 J7 h( {7 n' x) V1 O
'labels' => $labels," k; s& m+ ^( W9 B
'public' => true,5 I! y% \ S# s1 L" W
'has_archive' => true, t0 B& C6 [1 a/ q; g4 F4 I
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),% b7 X7 A3 L0 _, X( Z0 B
'taxonomies' => array('category', 'post_tag'),1 m3 h3 a6 Y$ w
'menu_icon' => 'dashicons-megaphone',
5 W2 z6 T5 I$ l 'menu_position' => 5,! q8 o" T* k T1 `) U
'rewrite' => array('slug' => 'site-wide-notices')
U* Z) ~, z. a, @- d! z9 F8 g );
- V4 _* l0 E+ b% r0 u1 r1 N* I6 b9 @8 r7 I
register_post_type('site-wide-notices', $args);
& t5 j5 I# I$ x9 R2 _ }
8 U) S/ K4 A: `( V# q ```. o7 u; q4 {$ G- _% T; Q* M2 ^6 M
' a) |& e' x5 n5 l d) A: V: y
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
+ O3 O. Z, u/ \/ W) b0 s& M: \) e# C% a. F1 k* Y& b
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
( m K. G: j: T: ?: D7 p/ i' M }8 B8 U( p9 q1 h) W
```. q% N5 \' X9 g0 |- s
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
' H& i! K. K9 V function add_site_wide_notices_boxes() {5 e; i# p0 n# P4 e# [1 v% F: G* o
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
3 S4 r' D1 {/ p( z2 E% ]/ _0 y- r }
# o2 m! c3 F! c, a1 X, g4 A
! T2 R5 B: _$ r5 W, g* L function notice_details_meta_box($post) {
3 V& ]; _4 I. {4 Y" Q wp_nonce_field(basename(__FILE__), 'notices_nonce');% a+ j+ z4 p" o$ Q* E, I
$notice_title = get_post_meta($post->ID, 'notice_title', true);
* Q% T) @( l# l! C4 h% b+ I $notice_content = get_post_meta($post->ID, 'notice_content', true);! W/ S d4 _: y) m
?>3 }( r: S3 z; ]) m
<p>
) J K6 Z |' q# m <label for="notice-title">Notice Title</label><br>8 Q: w0 S+ M: p0 A
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">! _# D" S Y/ p: h$ t' h, \- z5 ?
</p>
6 i. I- z$ [4 q3 q+ c4 s5 d <p>
r3 Y. J" [* W" Z: `. q <label for="notice-content">Notice Content</label><br>
6 U; u8 ^' r) Y( ] <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
1 `( B+ i# N. p/ M9 |3 A </p>
8 ~8 x4 [' R0 o( S( U$ n0 X' B4 R. X <?php' v4 J- |$ a4 b+ V) L; g
}
7 A* W# h) j/ X% J$ H( H* j( | {% v' H
add_action('save_post', 'save_site_wide_notice_meta_box');9 t: \% w5 H. ?3 H2 R$ |9 k# n
function save_site_wide_notice_meta_box($post_id) {
0 y+ P. g/ j- M. Y4 ] if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
: L% z/ g* ?" q return;
8 o) H$ h# R/ D: A' [7 J8 {. { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)4 R/ i8 L1 Z/ d1 L, |0 o. g" V S' u
return;/ y" b1 ?) g. W4 w0 i4 X' ?, ^
. b' y% S& C+ ?+ T) v5 Q7 d
if (isset($_POST['notice_title'])) {- l C3 } c/ V
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));# s* c, R0 {! c0 X) h8 V6 p
}
% K2 p, o' P: g w if (isset($_POST['notice_content'])) {
/ \; n1 B; H) K6 r0 f9 I update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
: V, d5 }4 e% p" U8 o! E( l }6 H+ L: M( o Y0 ?1 \
}
$ h* G5 h. M7 [/ r5 B/ Y5 |* W: h ```$ r8 |2 G) q: s! l! H8 \% g6 L$ [5 P4 U
' |0 ?7 Z( G9 A" L5 R0 l& b* b 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。1 G. \: g; D* e) x, O- j' O6 l! o; @
L- z3 L( p+ @1 ?' w; O5 A
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:2 ^/ ^. z; g; }! @% V2 }
6 {/ i/ X0 S9 P8 w0 L8 O+ L
```, u6 O3 z) _8 j- _' b5 I8 L/ `
$args = array(
4 R" J" ?' a( ^- F 'post_type' => 'site-wide-notices',- P }4 `3 ^. g5 p1 O
'posts_per_page' => 3,$ g& w5 L$ M2 v
'order' => 'DESC',# }, G1 q1 ]+ q1 d
'orderby' => 'date'7 f0 a6 m( A' b; K/ l
);4 ]" F! i3 r# h3 R
$query = new WP_Query($args);
; r$ x+ {/ J) _ ~: I! u% [ if ($query->have_posts()) :# I. g9 ]' |: x2 M% \
while ($query->have_posts()) : $query->the_post(); ?>' }1 s, M9 w- J. T# e
<div class="notice">
/ m" J& \3 x/ B, p2 x# u" W k <h3><?php the_title(); ?></h3>. e( z9 p9 O( O0 Q( N
<div class="notice-content"><?php the_content(); ?></div>! {' O% ?3 `3 z; s- `5 a7 w/ G
</div>
/ o$ _4 H2 W- k0 T# D8 G) m <?php endwhile;
, `6 b+ M% H. d$ F wp_reset_postdata();3 B1 x. P# C# u+ F* C/ ^ \
endif;
O6 a' r0 c; }. I ```: ?& ?8 H# @ a3 @
^. i" j3 E( D; K; H6 b- ^, U 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|