|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ J8 E- A0 y+ j/ v: {
9 K4 k: P% y4 N/ {+ g& O9 F
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
t, q& L& U5 f8 p6 Z3 h8 E* a; C
以下是创建自定义插件的步骤:% D$ G/ H6 e% [& g' P2 s/ S
# Y* t! R+ P; ~* D
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:- l6 A0 r( M( [! s0 @
0 p1 _" I, p% E
```8 F; F! ~ g; [
<?php
8 ~; Y2 W7 T p- f3 R; A- Z. A /*
7 _. v( n* j" L# ^, K o4 Q Plugin Name: Site Wide Notices Plugin0 H* p, ]% i- h$ d+ T
Description: Adds a new custom post type for site-wide notices.
V" ]- E* {; A9 J7 m6 |: J Version: 1.0" |- ~/ E' D- i8 M
Author: Your Name4 S7 |! W6 Y6 v; b
Author URI: http://example.com
' d7 i6 v$ k h( }, }' N0 | V* g */
. y7 O- D5 x8 `; n" A9 t
! m5 |+ h9 S- ?- t4 y // Add plugin code here...
' J u" p, t( s/ i, r' J ```
5 g( U6 J4 \ c7 W; ^6 H7 Y# E: e# W4 O6 b- O1 v6 s& h
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
6 N' m R+ E6 \& ?+ E2 t1 P8 }2 x$ |6 V+ @2 X$ J, y+ {
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
5 T& \- O* X r1 ^2 J; m$ `/ U; p. f m2 H2 ?' Q8 ^& F
```
5 i+ R+ l: v& y' X$ ?- W add_action('init', 'create_custom_post_type');: R1 T8 \ Z0 U" [% m* ]
function create_custom_post_type() {
, E- u- S4 G& A $labels = array(
7 ]4 t) z2 K5 ~ 'name' => 'Site Wide Notices',
8 ?# L" @ y6 s8 Q$ L m 'singular_name' => 'Site Wide Notice',
2 `! C3 ]$ ~ ^ 'add_new' => 'Add New',1 x' T. D1 E4 |
'add_new_item' => 'Add New Site Wide Notice',
* _* k6 r9 T* Z) V* |1 ? 'edit_item' => 'Edit Site Wide Notice',
. ^* E% B5 c% V* X0 E9 q 'new_item' => 'New Site Wide Notice',
5 N# d& C: D9 r( C0 i 'view_item' => 'View Site Wide Notice',
$ z- _- |. q3 ]: _% g& A# e 'search_items' => 'Search Site Wide Notices',
% w# e1 n* E. H" W* Y 'not_found' => 'No site-wide notices found',, J% H$ P: y3 e/ @6 ?" Q$ Y
'not_found_in_trash' => 'No site-wide notices found in trash'- u% I+ R4 W5 N, ]; O! _6 h p
);
$ M& L) M7 _' n* s, o- l& ]3 P. U/ o [
$args = array(
+ C! r" w; K- u7 @0 c4 A3 t! Q 'labels' => $labels,/ d" D/ z+ j. X% o
'public' => true,8 d0 [! e0 t7 _: y; c* ^
'has_archive' => true,
" A2 }) X3 |8 u- }! C 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions')," I- r! _. @' i' C6 r/ U, ^# J
'taxonomies' => array('category', 'post_tag'),
% o2 A8 ~3 J$ ]7 G 'menu_icon' => 'dashicons-megaphone',9 Z- y7 W7 t& S1 a1 I' ~
'menu_position' => 5,
* ]6 s9 J9 S# s0 i 'rewrite' => array('slug' => 'site-wide-notices')
% P+ t6 u& L0 a( g );6 s- l4 T6 a$ k2 I# D0 Y+ G
7 d0 H, s$ l# c5 u! O
register_post_type('site-wide-notices', $args);
7 C9 |0 A* j2 G7 l3 ?0 u$ G }* d& n+ x& j n0 E
```
4 K9 i: @' {7 C% W
& x: Z4 P! I, C! y. n 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。8 Y. ~8 q; r& H& A5 p! A
( e/ y8 h1 R( M9 F6 j3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:5 f1 ^ o/ S( ^4 M8 x
3 v1 O' D O1 u( L0 [8 ? q7 b* T
```
$ r, B9 |- _* x add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
1 M& y+ C* k" q7 {8 U; B function add_site_wide_notices_boxes() {
5 r Z7 {1 u! x. @ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');# `0 L Q+ \7 p; W/ j
}0 H' a; m2 e' @. U, W. i, u
8 \ y) Y4 p# b function notice_details_meta_box($post) {
) W- i6 P" f* {. f7 l0 E3 H wp_nonce_field(basename(__FILE__), 'notices_nonce');
" \: }1 M( t6 H; ~, j $notice_title = get_post_meta($post->ID, 'notice_title', true);3 I# G- `1 j) L" f
$notice_content = get_post_meta($post->ID, 'notice_content', true);: w. e' x# v4 X+ K* ]3 p
?>$ a3 O" j1 H2 m+ q9 b$ R! h( v% Z; _
<p>/ s7 e4 k, z' ?$ I% A4 V" z) s
<label for="notice-title">Notice Title</label><br>
2 G" m* L5 w4 M <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">8 h7 M( `1 W5 g% N
</p>) n' R! m4 x, H; F. W
<p>" A$ V J% H7 H P" F7 v
<label for="notice-content">Notice Content</label><br>& L. E8 o1 \1 m6 x2 J- K
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>* R# y/ Q; ]1 {3 J) ~9 w
</p>
9 Q# u: R: {1 U: E/ N& t <?php
: x3 |1 U. M: O7 z& d4 Z }
# T6 Y) i. T. i0 O" s7 o" S2 v' P# u5 ]6 t& N* |
add_action('save_post', 'save_site_wide_notice_meta_box');1 M" N8 ]! @- z% G8 {
function save_site_wide_notice_meta_box($post_id) {, t4 P) }& X/ u/ A
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
! }% u w* v! P/ @8 { return;. w# M4 K, u; w l9 ^
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" ?/ ~% Y) l; e7 } return;2 c: S+ U6 f; r6 m
+ [4 d3 e% Z! T- @7 l! g; D6 y
if (isset($_POST['notice_title'])) {/ y# F1 ]; J8 }/ ?9 N" h7 i* Y* d Q
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));. _2 r8 F5 k" s- w; W
}5 d7 j' g' u3 L- @% {
if (isset($_POST['notice_content'])) {5 T {6 a* q6 q! E' B! [
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));! X) S% _8 Y* `7 x
}5 x+ K. L3 n5 O# o' Z
}
' h2 P" c9 R5 H8 [0 F7 r! w ```
7 |+ L" O. s/ U6 b) _* |4 Q( | t+ O7 s
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。+ [8 t; m; _3 e- t
; k9 Y0 a5 B+ @$ E8 i
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% [& z# {' [2 C7 I0 m5 b8 i' D$ U4 L0 Y
```
$ x7 L1 s) |0 E& ^8 u5 w3 ^! r/ P9 n $args = array(
/ N2 q4 r: M# p 'post_type' => 'site-wide-notices',
6 @: y- ^1 A& m' r, ? 'posts_per_page' => 3,+ A, P' N0 l* c
'order' => 'DESC',0 B% g* u5 Q, s5 F# E7 M
'orderby' => 'date'
& J1 `8 {+ [4 A3 _ );
" d" i- W$ E& V: M& ?( ` $query = new WP_Query($args);+ z8 K4 V* P: x; z7 j/ g
if ($query->have_posts()) :) z5 Y: p- \6 e6 ^' l f ]' y
while ($query->have_posts()) : $query->the_post(); ?>6 }8 B& g3 D+ d- O; w
<div class="notice">$ W% L1 u$ O- v: @, h, K
<h3><?php the_title(); ?></h3>2 i4 ]6 W# s5 w& t" J- b S) v
<div class="notice-content"><?php the_content(); ?></div>
5 C8 D9 N6 y% [+ Y" f5 d </div>4 v+ N& k, M5 X# T; g0 s
<?php endwhile;/ Z3 t6 H; E7 e9 }
wp_reset_postdata();
0 l# |3 B) j* r F endif;
8 ^9 I. m, C) c8 R3 c8 N$ n9 y ```- S7 u0 Z1 Q# `
6 h" U: a& o) V. l$ q 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|