|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?7 ]4 a0 C8 ]; p5 b
5 \5 j) u6 O. X$ J7 e# V$ ]# I- q
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
M' X; q' g. x0 Q/ I' s6 I3 V
3 D# G# c& G4 j7 }3 F! ?( f6 z以下是创建自定义插件的步骤:# }! O: ^1 ]6 Q8 o8 W
# H: q1 |; T/ n3 B1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:6 l6 H0 X3 d" E/ d' T
9 D' H0 z6 ^, e6 a ```4 @" Z6 [$ |7 }1 `5 R, X
<?php
0 Z9 \4 s8 k0 B9 s0 ]5 e /*
1 y2 S" C# b! F. e0 k( A Plugin Name: Site Wide Notices Plugin- n, S8 E0 k8 v' v
Description: Adds a new custom post type for site-wide notices.
3 C6 [8 K5 D) f Version: 1.0
4 U9 \% U$ h& R, ?5 ?4 |, i( } Author: Your Name: T1 `/ J' z0 I
Author URI: http://example.com
# k4 a: h4 s9 Y9 N */8 R- X0 _1 Q3 u) v+ `0 w' ]: U9 Z
: ^1 D" l- I) ]/ f
// Add plugin code here...
4 d& ?+ l% m0 C$ S ```
0 M5 x M& i2 Z, ^2 D. Y
8 \* o/ E- ~" T1 P. _4 {7 ` 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。* \- T6 V' b- X0 P2 N
8 B' [7 Z; I( a+ ]* _) M2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
/ {4 g+ S, d0 B$ V% e9 m/ q# V% J }0 U t; q
```/ H- a- F- O0 i/ {" {
add_action('init', 'create_custom_post_type');
! [% N k, X/ y. A+ X0 i function create_custom_post_type() {
! B: }( m8 y& l6 q, V $labels = array( f6 M) I1 q$ a0 |% g9 o
'name' => 'Site Wide Notices',9 ]( g4 `- `, k4 ?' W, N4 x1 I
'singular_name' => 'Site Wide Notice',7 O% _ A3 w1 n$ ^1 F
'add_new' => 'Add New',
8 T) V7 o. A, y 'add_new_item' => 'Add New Site Wide Notice',' ?) [& f. b# W$ E0 R
'edit_item' => 'Edit Site Wide Notice',4 S2 k3 M# i1 W) P5 h2 g) e' I
'new_item' => 'New Site Wide Notice',* P& u; q* ?1 m- _0 a% l) h; m
'view_item' => 'View Site Wide Notice',! b' M+ h) ^- ?( O
'search_items' => 'Search Site Wide Notices'," y& k6 D0 m: H" M& o% ?
'not_found' => 'No site-wide notices found',# \; N1 a0 Y4 q9 S9 v) D: P
'not_found_in_trash' => 'No site-wide notices found in trash'2 Y! H3 T& c2 j6 | \" Y g
);+ \* K K! W" y: s9 G5 C( H
6 | x9 t" E- }
$args = array(
7 H9 B$ R5 Q; q$ {8 B& W# O8 ] 'labels' => $labels,
9 ?! O2 b3 D/ u I$ G 'public' => true,
d& I B5 Q6 J7 Q3 Z2 } 'has_archive' => true,
+ F8 r7 Y# f4 K ?" r 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
+ Z; C' [4 D$ ~! O1 Z 'taxonomies' => array('category', 'post_tag'),' @8 ? q, K* p4 v
'menu_icon' => 'dashicons-megaphone',- |: @, a) I# ? D8 F$ D
'menu_position' => 5,2 b4 _* o" F! m* }& r; B
'rewrite' => array('slug' => 'site-wide-notices')
1 h- Z, j" r/ ]( D7 n$ J3 x );7 K# v' e# H7 O3 T
9 ?: Y, I0 N8 R+ F register_post_type('site-wide-notices', $args);
! s F, A& _9 }& I. o1 B }' V0 S; x/ X: o* V& e
```
5 G4 T$ V% T* p g; i0 U4 K
* ?2 i0 E: N L: s 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
1 a; e: L# r; Q0 F: l
. ?7 e8 G2 Y" B" T& W3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
# F# M& i' X V! \5 c, J7 N# F. [8 t" w6 |
```
& V# }; h9 M- N; R* Y add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
- O, J g8 D d function add_site_wide_notices_boxes() {7 x7 K0 V& r4 _) e/ l) d
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
/ a4 ?- ~9 O2 l3 D$ M9 q3 s/ J }
$ y$ b! C/ R1 G8 h* @. e% L- _% K
function notice_details_meta_box($post) {/ h0 k# y& m- N1 p& _* A- q
wp_nonce_field(basename(__FILE__), 'notices_nonce');
$ v! N0 s* {5 M, T5 f( D ~( L $notice_title = get_post_meta($post->ID, 'notice_title', true);% d- _2 d; ]4 B/ } e9 o3 s
$notice_content = get_post_meta($post->ID, 'notice_content', true);
, T) U& K" U9 i3 ~, ? ?>
- V2 o1 U" J2 E3 Q. r% t% [7 c <p>7 K0 s5 t* O9 o+ `; U% `( J+ K
<label for="notice-title">Notice Title</label><br>
) ^+ t" G5 I* `1 P <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
* U$ x. O! v# L </p>
- j3 A( _0 w# |+ Z3 M <p>
* E( e, g5 X8 P1 i <label for="notice-content">Notice Content</label><br>6 n/ Y( F" k% x; m
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>; |: l g; \, @
</p> e; O. N/ V! X0 U2 @, l
<?php! N" S! Z+ s5 K& o( u* r$ g% a
}
7 e% Z4 k+ ?5 ~) {" w# @
3 d$ H; }3 K# ?1 U& t add_action('save_post', 'save_site_wide_notice_meta_box');
s9 y, J E# B0 }# M) W, n& U function save_site_wide_notice_meta_box($post_id) {
* A, y9 t% n! K if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))& _4 w+ K8 Y6 s# y H; x
return;$ o9 D7 ?) k3 g( e& b8 r# [( D
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
3 y6 V+ _1 Z+ n( }+ \ return;
6 [% K2 D9 ~1 L Q w; d5 a: S' `
$ e6 i9 k( J& E- x8 r1 x$ u if (isset($_POST['notice_title'])) {
+ D4 G! p0 _- Q6 y update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));" e" F5 R, Y' N/ ~: c
}
' g% v; o, {6 q' B4 \: W# a2 } if (isset($_POST['notice_content'])) {
1 l8 H. X( _; @% J0 N! h update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));% B, W* b6 }# S
}/ f! e! \! ?, N: N2 U/ S6 U
}' q8 ]" e+ M1 X& C4 z/ i
```
5 P. a3 @3 v# [; _* t" l& x' y
) @0 D: Z4 I$ x- s# F4 v) Q: V 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。4 M l% v% S- d6 a8 t2 I' o
/ p& x3 b! O7 S' H1 _2 t
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
5 H' g* w2 G5 n1 Q0 M: v, }6 {6 F; q$ N' ^
```
9 g) @$ m0 V- l' k $args = array(
* s: X2 N. z9 ^, O( T, @6 R) U+ f- ]( l 'post_type' => 'site-wide-notices',- i7 w; u# E$ N. s/ a# ~1 H: h
'posts_per_page' => 3,6 e( \( h ^' m- i0 l7 r( l& }
'order' => 'DESC',% |- C2 q" |2 D/ E4 O/ I5 y8 e
'orderby' => 'date'
- R; ^! _" }& V5 J );9 K: n- o4 A" P2 j; J
$query = new WP_Query($args);$ Z3 T' w) g, w# O2 }& K
if ($query->have_posts()) :
, Y9 P. E' G# l5 d1 t while ($query->have_posts()) : $query->the_post(); ?>
i# A! E, W( I1 a: Q <div class="notice">
4 V5 b4 D1 Z, g9 a" F" M. d5 O- D <h3><?php the_title(); ?></h3>* Y7 Z# |$ j7 Q" D! d Q4 w
<div class="notice-content"><?php the_content(); ?></div>& Z# Y! b# q6 o( Z% i; }
</div>, y% I! f" m) c2 s; f2 o
<?php endwhile;
5 ?5 h+ T# P/ E; N/ ^+ S wp_reset_postdata(); _# c0 y* V% I
endif;8 a/ ]0 J7 R& v, P# K- t" E
```$ s- S' Q; V& A+ m1 M9 D
6 H9 L" f2 l+ |: |8 X
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|