|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?+ ~. ]4 D! _- |, b5 i
9 n& i( V5 X, `% @0 {# e* |5 A如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
( X+ i- J( N1 M5 L' Y" l( j" L# r8 _# E, Z; C) z. r8 F
以下是创建自定义插件的步骤:$ @) b& D8 k/ ^! T- s: q9 i
: T+ W( x; P3 y
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:3 D+ H. u6 O, D1 x
7 K8 X% y% k! \6 k$ b5 z
```2 d6 x- y5 f; Q
<?php6 d, n+ }$ p+ o+ t9 F" K
/*
& S8 w3 c( l% v% h. ~ Plugin Name: Site Wide Notices Plugin- ^% S+ {( C7 Z' C9 U, g$ I
Description: Adds a new custom post type for site-wide notices.1 s; L- }" A) L3 e" A/ w/ \2 F
Version: 1.0
$ q+ l3 u, p& E Author: Your Name
% k( [2 C7 W( R9 m3 z( J( d- ` Author URI: http://example.com
- t- V5 D- X. _# R* y5 n! a */7 U7 w. D6 Z4 _4 Y
. \! m1 J6 N; H& q m! Z# Z H // Add plugin code here...
! G* c; E4 X- W( k3 Q ```: g& x) L' @/ t$ l, j% | e
7 \/ Z- }( H M! |8 C5 K
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
( n) s0 F3 y; d( N+ o. T* K) k3 _
' D7 l3 M5 C- m$ J8 m% K2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
0 Z. L' P$ g, Y: S; o- ]
: F/ D% y$ F1 _: p5 k9 n ```6 {' g) K0 n' f: ~' o0 v
add_action('init', 'create_custom_post_type');" m5 ~& {4 [" T3 L/ Q: W
function create_custom_post_type() {4 l6 P1 m" x" X n8 q+ s
$labels = array(% K7 E: ]# y4 b7 H2 R! M
'name' => 'Site Wide Notices',+ r& k+ G8 S* K) T8 B; I
'singular_name' => 'Site Wide Notice',
E" \7 F, ~1 t& T, s 'add_new' => 'Add New',; x" K4 J$ K* f- h: T! }
'add_new_item' => 'Add New Site Wide Notice',$ k& i+ E; Z5 @
'edit_item' => 'Edit Site Wide Notice',
& ]+ ~/ G1 D8 O" s, M 'new_item' => 'New Site Wide Notice',
- A' M. |$ k5 Y 'view_item' => 'View Site Wide Notice',. {' {0 S: k/ P. N5 U; W
'search_items' => 'Search Site Wide Notices',
3 g) d2 Z7 x+ Q* U' o! @& h 'not_found' => 'No site-wide notices found', w2 @( w4 x O
'not_found_in_trash' => 'No site-wide notices found in trash'2 S* P0 c. h. n5 B# l. u
);. e5 {. M) }) }5 t
F# R/ ?0 l2 |; ]0 U& O
$args = array(
0 T, {6 `2 z( l 'labels' => $labels,
2 R. N3 y# X: c; K) y! @ 'public' => true,
( ^4 F% k0 _1 D6 o& b1 _ 'has_archive' => true,
- w# @) W; s5 z 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
4 ^( E3 X0 ^; {( Z1 ^3 D7 P3 ~ 'taxonomies' => array('category', 'post_tag'),$ i' Q9 j1 L* ?5 v; v
'menu_icon' => 'dashicons-megaphone',
9 }& G4 `8 Y( @+ P4 `8 K5 d 'menu_position' => 5,* [. R" {% l2 F" d
'rewrite' => array('slug' => 'site-wide-notices')
* A( b* o. L$ z7 I+ W) d2 c6 i( `& e );
3 C* E- i7 [8 |; g( [7 Y/ l4 r- z* N7 m
register_post_type('site-wide-notices', $args);
0 f; m) L: f! I3 B5 c7 X# v; u }
' C* L, R) u+ @7 d3 s ```
# w* D c& w( z' y2 u) x
3 V+ O: q8 ^/ E- h 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。2 B8 q3 D$ I1 G1 T: H+ G
U/ \2 u0 k5 w3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:4 a/ t+ S* A* O, n: ?6 `' ^0 ?* s
" K. Y! r3 G- O8 B1 Z( H2 X
```
' H" e ]# k2 J W0 [1 {) w2 C add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
. }* s, f# q5 {) j" f2 i function add_site_wide_notices_boxes() {
) I/ U+ ?" z& T5 m! Q" L1 \ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
4 B" w0 o2 d e4 a }
& m; C& i# ~* c6 C
4 ^4 {* R" D8 ]; v" ], E& `" n* f- d function notice_details_meta_box($post) {4 N t2 [7 x! I
wp_nonce_field(basename(__FILE__), 'notices_nonce');0 A, L/ x6 ^. O" T$ y4 j; U7 [
$notice_title = get_post_meta($post->ID, 'notice_title', true);
# t n6 n; g8 _. G' L. D $notice_content = get_post_meta($post->ID, 'notice_content', true);5 u0 I9 h$ J ?# |( j v
?>- B; O( U: T( q0 B4 p+ o) m
<p>
. n# v# y4 E1 e F2 z1 J <label for="notice-title">Notice Title</label><br>
8 I, u1 ?( M) Y/ v0 a' c8 F/ R/ Q <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">3 l$ C' K D1 a. G: ^2 o) x$ J
</p>+ T" y. s) K, }6 _% b' T; W( {2 {
<p>
. s) E$ x/ M! b2 I <label for="notice-content">Notice Content</label><br>5 Q4 j; \6 q" S' k
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>$ ^. L, }3 i, N; r
</p>, U. T: v3 n0 D; y8 S2 U
<?php! K: A" G# D; E- X5 Z! |* Y
}8 M1 x' V' D! V1 v' E: x( H
- S# d5 s6 L) K2 J add_action('save_post', 'save_site_wide_notice_meta_box');
+ g& d/ C+ U6 ]* ] function save_site_wide_notice_meta_box($post_id) {
% I3 I! a0 g2 L% x% P& N$ v if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))): S7 i% e$ @; W+ ^$ V) U
return;9 I& [* z! P- Y, S' s/ u y
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
4 l' k% A* v# t v( ]* J return;
. A& s0 A9 u0 z* o5 F
6 a) J2 C+ e) [" M1 M- | if (isset($_POST['notice_title'])) {
4 p2 O1 z8 Y; j3 r update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));, h; N, b" R$ r
}
0 l: m. B7 \- E z( L( @; h if (isset($_POST['notice_content'])) {9 ^0 _6 q. X4 w; ]% x2 x+ S
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! [0 M9 C! H2 s g }
2 _2 m, I* M' ? }
, a0 F. ^( s `$ @9 D* P! E: d ```
$ Q; D- n- z4 F5 c' z, I' e4 m5 W: @5 y& L# G S2 k
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。% I- ]# K4 R9 S% x9 M0 \9 E
- Z8 d$ ^* K6 d5 U. x5 |8 Y4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:& v6 J- _3 l/ }- _, _7 ]
0 e( n* [6 b' O% F0 L4 p' }
```3 E: r4 W: P' w$ @9 V9 t
$args = array(
/ G3 m, e+ u: A0 a" N. U 'post_type' => 'site-wide-notices',, H& T# v9 I% B9 i) r7 x D
'posts_per_page' => 3,
" C* c# b/ B2 h" P, Y# h 'order' => 'DESC',9 m1 X' T3 s/ f9 V
'orderby' => 'date'8 R5 M1 n: W6 P) y5 w
);) T4 W% |' C, _* X
$query = new WP_Query($args);- B& x# V% j! O7 n
if ($query->have_posts()) :
+ J- j2 J- P) v. P5 B0 s& t while ($query->have_posts()) : $query->the_post(); ?>* ?2 y& O/ k$ D5 x
<div class="notice">
& ^+ c/ s' u, _' Z0 f <h3><?php the_title(); ?></h3>4 |- ]: u' f2 a! h; H
<div class="notice-content"><?php the_content(); ?></div>0 \2 }+ P5 z' v+ `
</div>$ w& ~; A: G7 B9 g3 x1 _% r
<?php endwhile;3 L7 |6 I! P# |, e* ~
wp_reset_postdata();
0 w! ]. N( f' I# [ endif;
& c+ C, N. k. E3 W( @1 a ```
7 Y1 x- g" ]5 g7 G: K9 L
% j8 j. }$ Y O4 V3 F% D+ H3 W1 U 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|