|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?: O- S [9 ?3 H, w$ t4 P, |
8 \7 n8 E8 i/ y% ^# G3 V
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
$ T+ `% K/ I8 b! r4 o7 m" |/ \ }6 X6 j. A; i0 x2 _& l
以下是创建自定义插件的步骤:
2 ]0 r! [3 ?9 ?
9 c$ Z& t8 q* S3 ^5 \, J- F1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:- X, c; `: U9 ]6 z D3 i" o1 K
" _- h& e y* K8 r7 Z' k' Z
```
* ?1 ]5 w4 |! b <?php" z L1 w& f% P' H$ U
/*
" _+ V0 ?, O3 [4 k/ n Plugin Name: Site Wide Notices Plugin* F' L v/ b& {1 C
Description: Adds a new custom post type for site-wide notices.
, y, k2 P g9 l `% V7 z2 o& c: ] Version: 1.0
/ o+ _# u# K1 l Author: Your Name
: V& y% g5 ~7 g& [ Author URI: http://example.com8 D( _6 u& I; W% \( i, X9 h
*/
* L8 e2 K! |( q G: O& y6 b0 n4 w6 a0 f: s2 C+ ~, i7 L
// Add plugin code here...# A% ]% F: D8 u( m/ X: e0 L
``` `% L' O$ [4 ]" q
: A( E5 {3 w3 D 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。8 q, s( K/ R. B) Z
$ y% X3 D7 m B2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
, c/ E! J. Z- I! z
0 u0 A/ d4 [" w6 D4 ^9 Z ```
1 L" ]4 j* @( g/ u, U" g) c/ E- v add_action('init', 'create_custom_post_type');! k9 j H ?/ `: Y9 p
function create_custom_post_type() {
$ Q2 Q- N' `, A $labels = array(7 J" ~1 {* q9 V! h. }
'name' => 'Site Wide Notices',
; ?+ Y# q5 w( E" J5 U 'singular_name' => 'Site Wide Notice',! a! v* U$ L) A, l6 M6 {( U4 s
'add_new' => 'Add New',
. f; H5 d9 {- e 'add_new_item' => 'Add New Site Wide Notice',7 y* ?* o1 p2 o, R
'edit_item' => 'Edit Site Wide Notice',
) N% m' K, t% c9 w* e 'new_item' => 'New Site Wide Notice',
* v1 v, q1 R. Y 'view_item' => 'View Site Wide Notice',4 K! P0 R0 ` ] `
'search_items' => 'Search Site Wide Notices',
! g# I. V7 v A3 u( |) Z 'not_found' => 'No site-wide notices found',
6 W! [8 O& Y( _8 V2 O0 l* p2 n, @ 'not_found_in_trash' => 'No site-wide notices found in trash'
( W. ^& w7 n v7 y );
9 o9 n a, F1 A( D j
( p: K0 e S+ T- I% f' j- ^! R $args = array(. j8 [- M$ e8 b/ e/ S# [' z" ~
'labels' => $labels,. E7 q: ]7 P, l; U9 ~- L/ W
'public' => true,4 L c$ A2 u! w; J [5 Z) [
'has_archive' => true,+ V; c% ]$ R( a/ @$ C7 m e+ t
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
, K; n# r4 s. E9 t 'taxonomies' => array('category', 'post_tag'),
* o/ z* d0 t1 \" ]% Q& ^8 b 'menu_icon' => 'dashicons-megaphone',* A' o' ~* F Y; u9 H* c
'menu_position' => 5,( L1 q( n) K- A1 {8 w; c% R
'rewrite' => array('slug' => 'site-wide-notices'); W1 g' f0 B; A+ k
);+ Z6 P% C/ G# H
$ r* u2 S1 }; ~" y' K
register_post_type('site-wide-notices', $args);) z8 U3 t) Y) H% Z
}
5 O- |6 L) |( {1 J ```8 h; k% U5 _- P5 R# }( u
2 r2 E8 ?3 U% Z% W9 g! M4 t' C 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。) T+ J9 I. k! L, f7 b- c7 O
0 P5 \5 O% X+ w3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ O+ G. N( r; U0 |- V* j# G, s. p
9 n& E3 f$ o' x: j; r2 U
``` P, V: T; ?1 _
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
9 `, G6 j9 x& o- _ function add_site_wide_notices_boxes() {% J6 ^+ w$ j$ _( e' Y
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
3 o h; m6 N( N }) S& \8 `% e% H# g4 S
# t) X6 {2 m0 b# Y4 V9 C) s* ^ function notice_details_meta_box($post) {. ~& f) L! V9 R5 K: q
wp_nonce_field(basename(__FILE__), 'notices_nonce');
8 J8 Y2 `( F9 ?; _ $notice_title = get_post_meta($post->ID, 'notice_title', true);( w$ D; r0 n& _& y. s
$notice_content = get_post_meta($post->ID, 'notice_content', true);# V3 F) b) ^9 n) r: d
?>
" z" R! t5 |" Y* E' d; y# S7 G <p>7 Q8 s& O% r* x; \2 P, _) `
<label for="notice-title">Notice Title</label><br>
3 W/ Z9 X& `+ `8 i% j <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
0 P7 d- j0 l; }# N2 @ </p>
& V& ]9 o$ A. Y1 Q <p>- h/ e9 s7 b' F' ^; }' \: Q
<label for="notice-content">Notice Content</label><br>
* J* X! r2 \' r: P <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
, m* s' Z J9 z& ]( n </p>
H; c1 U2 H0 v3 E, N <?php {0 }7 j: ?* d( F2 t
}
& d: F6 `5 |1 X: ~$ Y$ I9 t+ ^! y( a% a8 N; E8 g
add_action('save_post', 'save_site_wide_notice_meta_box');* G" X# H% v$ p& H/ y& L. b
function save_site_wide_notice_meta_box($post_id) {' [( B9 O- `9 u6 D
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
! w& ]8 f3 T% @/ u return;
3 y& ], t9 E; b2 N+ W; k if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- u$ S) a3 t/ J4 r# Z return;; }" d6 F2 F4 J8 i! ?( `
( P4 A8 Q( D4 M" _0 t" R if (isset($_POST['notice_title'])) {4 h4 i, d& t; z* K3 h4 g
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));% S4 w% |# o" _: P
}
3 ?6 `+ U& x }* ]. g( ?9 S if (isset($_POST['notice_content'])) {1 f3 n5 Z+ l& ^6 {
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) t- F8 G' o. R }2 l9 @0 h! P3 H% }2 ?- }% F* I
}' o1 _ d/ C% G2 t4 V7 R
```
: R8 _3 r3 k9 R3 _5 J$ w0 n2 M2 ^
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。) B! @% Z8 @6 m) X2 ]
' }/ I4 f# X0 a: ]# p4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:) l- r3 `+ }( j8 B" }
$ ~2 L( N7 R4 C& m% q1 `5 q% b
```
4 } z( E4 ^0 w% v3 q6 w$ e* f $args = array(5 Y4 k+ ~+ Y# C$ V/ B& V# _5 d
'post_type' => 'site-wide-notices',. _2 u- O+ k5 K. ^
'posts_per_page' => 3,' Q6 t$ I0 I) H8 a2 d8 I
'order' => 'DESC',
" X, C7 P3 `9 b0 j 'orderby' => 'date'
: P3 p3 A$ [: o );7 [7 p) P7 Q! [' N" ^
$query = new WP_Query($args);& i7 D" W3 G$ H3 ^
if ($query->have_posts()) :
8 u; }' M& i1 N* o7 y2 n: W while ($query->have_posts()) : $query->the_post(); ?>- T: t1 r! V! F5 W3 ^) ]
<div class="notice">9 c9 X4 G: g/ G% T2 v& M$ [: @
<h3><?php the_title(); ?></h3>& _6 ]+ @. a1 N5 ~, [5 f" R
<div class="notice-content"><?php the_content(); ?></div>4 j: b2 T7 i+ S9 i7 C' M
</div>: H0 k; n/ p5 O- }" @
<?php endwhile;6 D4 g8 F: m5 K2 _0 B7 s
wp_reset_postdata();
- f6 O- U6 |: q8 e* ]5 U endif;
6 h- s) Y5 k3 V ```0 G) W5 Z+ L- b& R" {
, O9 z1 Y4 \. h- o1 n
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|