|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ V$ ]2 m2 i" x: D, d
5 M$ P: R7 j$ T, ^
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; B, Y7 C% q: n
# ]9 {0 J8 c0 B$ u) v) f' v" R. g以下是创建自定义插件的步骤:0 I' F$ t2 m7 @' H$ H- K
/ T2 h" }. |( \/ a
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
2 l# p" U. F* f9 t: R3 f' Y7 @, N" H% L& K5 ~+ a
```
- J2 O5 ?, C' n$ q8 ~' d <?php6 j/ \) L! x4 g
/*" c" g+ G" ?* U) y) d
Plugin Name: Site Wide Notices Plugin) M" M3 W2 g/ a8 J. F
Description: Adds a new custom post type for site-wide notices.1 [6 Q/ E: O' b
Version: 1.0+ {1 _# z0 j9 m4 ?5 N S y
Author: Your Name
3 k# W+ o% x& G: Z; w: i& m Author URI: http://example.com$ Z. q2 |0 Z, y) F% c( \4 J( z! M
*/, o7 Y: E% H; f5 t+ l
. `' h5 F% d0 [/ G3 o9 V( W5 ?
// Add plugin code here...# K5 g9 ?; j! ?. x7 z/ \
```
# R; y' L( T; }( s v. `6 _3 U4 [- g( H7 R8 y; x
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。& V. G* W" E. _1 h+ A5 {" f
q' y; P# H' Y4 J
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
v/ M0 b' f) |2 u% Z# Y3 }; S, ?/ ]) t, A
```
1 H; Q9 `. c* n8 m9 K add_action('init', 'create_custom_post_type');' C0 C% s& `' _- I
function create_custom_post_type() {& Y8 C0 m: G ~8 U' R2 @" H% Z
$labels = array(5 ~$ k- e `2 B9 I! m/ Z) M) F
'name' => 'Site Wide Notices',
5 C, I, j; _" f4 _) [ 'singular_name' => 'Site Wide Notice',
- i! J/ e( @# G' T$ s 'add_new' => 'Add New',$ I7 g! ?; }0 z
'add_new_item' => 'Add New Site Wide Notice',5 n" F4 _5 x, M e0 s# s
'edit_item' => 'Edit Site Wide Notice',
! u) y. N% J) y9 e4 Z2 N! y. { 'new_item' => 'New Site Wide Notice',
7 V/ E) z% {: ~3 P* s8 l" {* J 'view_item' => 'View Site Wide Notice',3 P+ r$ Y; F# r) T7 s
'search_items' => 'Search Site Wide Notices', [) `' A7 w' m8 q/ ^- N
'not_found' => 'No site-wide notices found', U/ v/ |9 Z! a* c
'not_found_in_trash' => 'No site-wide notices found in trash'& ^" _( P- I$ n4 |' A, o' q% R
);$ k, \' w% I' Y( x1 `7 V/ [( O1 P
8 K1 L; W L5 j. j( m $args = array(5 m6 g3 [1 G4 G. \$ F% X
'labels' => $labels,4 h& n# n/ `; _) L- A# u
'public' => true,4 ?0 w! G( ~: L4 K% r* ]3 C4 F
'has_archive' => true,; u9 o5 W' o' n) `+ ?: _# l5 J
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
/ X& g) h A- `# T) Z 'taxonomies' => array('category', 'post_tag'),
; Q+ t& T. R! R! u+ ? 'menu_icon' => 'dashicons-megaphone',6 ^3 P' I& v" e+ G" Q/ {
'menu_position' => 5,* m6 B. Z3 @% m* O% g0 x
'rewrite' => array('slug' => 'site-wide-notices')
6 I/ ]% Z; B1 ?$ l J );
( y/ ]$ F5 N' x" f% A/ D2 O+ g
/ ?- S4 b3 I6 \. x register_post_type('site-wide-notices', $args);% g" Y5 h; s2 ?
}& @% {1 v( t: q8 j, W5 Z
```
2 i+ @5 {+ J% @7 |4 t) R$ m N
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。9 J! B1 B% g& K* G
- X, z' D7 W; _" ]/ u3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:* v: j: T, S9 g5 {- h
, V' W7 b$ W0 Z
```, w" ] E6 H8 g; n a2 o Q5 ~* t
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');" K: Q; _% Q- B6 t) h. g
function add_site_wide_notices_boxes() {' G( t0 _ p1 H( |' k
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
4 t1 F2 g+ u7 d" s/ m }
2 v% ^8 i+ |6 G! Q' _& H
4 _ D; {* S/ [( k1 l$ Q" |, f" y; _/ u function notice_details_meta_box($post) {8 d4 J6 R8 m5 w! A
wp_nonce_field(basename(__FILE__), 'notices_nonce');* L/ { _' s1 a# \/ j8 B- H
$notice_title = get_post_meta($post->ID, 'notice_title', true);! O$ ^! t) r- g* t8 X* G
$notice_content = get_post_meta($post->ID, 'notice_content', true);
* }! \4 S, W+ y( `8 ~5 E ?>
7 ?% q* U) @: w* g1 j/ r) W$ o <p>
$ {& `, @/ v& Z1 H, r <label for="notice-title">Notice Title</label><br>
+ u+ T5 ~+ v% [ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">+ z0 u* N$ k# f$ J- {! X8 Z
</p>
' y% y6 T* C# m; |$ w3 w- ]/ @ <p>; r$ }9 }' b# ~1 `) U7 ?
<label for="notice-content">Notice Content</label><br>2 C" E }+ X6 _% p* s) Y
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>' V) Q; d4 _! L( ~8 M' H4 k
</p>
5 O* W. ?+ K# F" i3 B <?php
. [- }# h" h! X4 j }6 L/ c9 d4 V: H
" U1 x: n2 e1 s add_action('save_post', 'save_site_wide_notice_meta_box');- b+ t- n3 Y- _' t" c
function save_site_wide_notice_meta_box($post_id) {
1 r% ^: r' |# }# U+ ^ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
& j7 z; B( Y) w. f" h' D5 n8 B return;
e2 u3 V# Y4 Y1 Q if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
) Z2 f, D7 d/ B9 ]' K( V) z& [3 F return;
' _2 X# B8 \" S1 N6 B1 s* b3 h/ }- {/ u$ Z5 H5 w. o) [
if (isset($_POST['notice_title'])) {
, }3 e9 g) X; U" W% D4 ? update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
; \0 d- R7 D# G3 ^6 Y& ]# M }: u) G! O# Q$ D" ?$ V
if (isset($_POST['notice_content'])) {
2 x# O* G5 n6 c ` update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));' z; ?1 V% e% A$ E7 G
}1 F) m' E: l: {
}
/ [) T/ b5 R) @$ B ```
% E: L8 W- y; a4 e- N, y7 P" U, K( a8 p% q: n
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。) p; ]/ R9 @+ K) a
( ?* n- }* W3 y2 C* o6 Y# c
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
1 A5 x; `8 Z6 C8 y" t
2 Z% d! {$ L. y ```
7 p0 e7 S% K) Q* ^# f $args = array(
% b* E! y6 O, c& J6 q& v! a* J+ C/ `. y 'post_type' => 'site-wide-notices',
2 P; A8 `7 k( `* X/ \, O 'posts_per_page' => 3,
. X; b8 E1 T& p0 B+ T 'order' => 'DESC',- P; h$ c* e9 |8 `6 H/ k; [- G i
'orderby' => 'date'4 E V Z. a! T3 x" r; B; j) F
);$ }/ Y5 L! h1 d. C( v
$query = new WP_Query($args);
2 {3 Z5 k7 f* ^ if ($query->have_posts()) :! ]; i, {1 M* c
while ($query->have_posts()) : $query->the_post(); ?>
$ [% W' p( N1 n9 L w a7 V <div class="notice">
" m9 l3 }8 s* @) D, f% a <h3><?php the_title(); ?></h3>
( Z! `/ S6 P8 d: h# y- n <div class="notice-content"><?php the_content(); ?></div>
) r% P+ r2 J, H$ O& C4 Q </div>
% u$ ]2 m7 n) @+ D <?php endwhile;: Y6 ^6 j. p$ g% V) h
wp_reset_postdata();! H! @# K# e$ b+ @% k: x
endif;; z" `% J5 ^% B& K6 }* ~/ U% I
```
! f. A8 T; o* P# _1 l
" C, x& P: m9 q" ~2 n+ v% V 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|