|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?" F# w3 i' k3 H9 I8 V
7 R* {6 E1 N5 Z- n6 X
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
3 u, Z$ N. }& V: K" {, O
3 {2 o0 q8 j% U4 w以下是创建自定义插件的步骤:7 S- o% T8 ]5 }, O& A; k2 [
) F- S2 O- N$ [$ W
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:8 C, M4 {5 l# B* z5 ?6 E" [
: L" Z/ u8 S# k1 V% ] ```9 f; U% w/ d2 J% _* z
<?php2 F5 }* l7 P" w- J' s) C
/*
5 @. m( m; o! d Plugin Name: Site Wide Notices Plugin
8 Y$ C5 `+ V3 |8 n1 u Description: Adds a new custom post type for site-wide notices.8 X6 |. C" M' v+ i$ G5 m4 }
Version: 1.0
2 z# g- Y! b7 W Author: Your Name. r; ~* Y. o1 N9 n
Author URI: http://example.com9 A. I# j% H8 e- k! f8 x" B; v
*/
4 X0 m" M: W/ r( q; s, g1 g }4 J$ y& _5 q" |9 _( g9 C( T
// Add plugin code here...
" w" o7 Q3 b- V3 d$ A ```: g0 j0 z8 C: R, ?
+ N6 h# q% I! O9 ]! [ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
/ N6 S7 P, ?8 r8 l
$ N0 A/ K6 h) B* u5 r9 G7 S2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
- P) W- ^3 \0 _ B3 w
3 o4 C, H+ h% F# r8 O) q ```
' i/ B+ r2 i* s) [, O add_action('init', 'create_custom_post_type');
. ^5 [* S- ~' R. c9 r0 P function create_custom_post_type() {: g1 P5 t- f& G
$labels = array(7 {5 Q- W& \3 o
'name' => 'Site Wide Notices',
* c) ?4 B2 T2 S/ l" r 'singular_name' => 'Site Wide Notice',8 Y, w1 U' u: P9 [: s* ?
'add_new' => 'Add New',
T. U- [* s. q6 }" w5 V 'add_new_item' => 'Add New Site Wide Notice',
$ d; u/ U# ]7 i' r) _ 'edit_item' => 'Edit Site Wide Notice'," L9 {+ ^4 e3 ]* a# Z$ I
'new_item' => 'New Site Wide Notice',
; R3 B' K& j( t! E; ~7 e 'view_item' => 'View Site Wide Notice',, u5 @! {- n& D' L* `% E
'search_items' => 'Search Site Wide Notices',
: T' E; a8 D m8 x9 s* ~) [ 'not_found' => 'No site-wide notices found',
2 ?7 f/ g3 [( E" p0 v! t. H- C 'not_found_in_trash' => 'No site-wide notices found in trash'
2 G* U3 S5 J7 N. T0 [0 g: t6 a );
4 |1 F0 I5 {0 @- S' f, _
2 ^0 Y: D9 [7 z& V; O/ c& O $args = array(# a& ^- P9 l) H6 |
'labels' => $labels,4 a) H9 A" Z4 p5 x
'public' => true,' E9 S+ e# E/ [, N3 G" y* g
'has_archive' => true,6 M/ `, \* A: [. ~' L! `
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
1 H5 H- E9 q3 D$ s k) T1 N8 s9 Q ~ 'taxonomies' => array('category', 'post_tag'),
$ i" M) k# h) k$ U( N 'menu_icon' => 'dashicons-megaphone',4 {0 Z* H" z" [7 P1 g, l/ _% C
'menu_position' => 5,! ?- a, ~/ s7 \- F
'rewrite' => array('slug' => 'site-wide-notices')$ D8 h4 l) s8 ~3 u
);
4 \$ M: c7 j7 z0 ], t+ R; w- X# {/ C: h+ S# b
register_post_type('site-wide-notices', $args);& x7 m6 P3 C8 e' C
}8 D5 y+ _) [ f0 Y+ ?
```
+ U$ l* n6 P& S3 y
' u, n& N9 x# j3 M- {. @0 w 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
1 B& G U4 f* z3 V- \! A# j4 G+ u4 d( E4 A* Q
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:& k6 m% b' c/ I3 z- L1 S! R$ ]
1 e8 I. f' G, f7 s
```
% U$ d* G8 a& S! E* }# D add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% n2 ?$ v7 J5 @' r$ g8 O
function add_site_wide_notices_boxes() {" B7 y7 Y; {# s
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
, r0 o1 q$ h6 w- [1 i# n; Y, r' N }
9 q) o( u, d! W0 p* G7 \
1 V' ^2 D8 k8 r' T9 R function notice_details_meta_box($post) {
* x, J8 f# O* @6 `' x1 C1 x wp_nonce_field(basename(__FILE__), 'notices_nonce');+ q( ]+ t/ s. A R" l. }7 p. b: u
$notice_title = get_post_meta($post->ID, 'notice_title', true);1 n" w/ H& ^/ m" U! T& U w
$notice_content = get_post_meta($post->ID, 'notice_content', true);: ?5 g* m3 {0 |( x+ @: x, e( N
?>4 ~1 |9 C, V k" I% L# `0 `
<p>
4 {4 E3 N# H5 J9 z3 s" x# Q <label for="notice-title">Notice Title</label><br>4 J' C6 g0 d# J
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
l+ w) m; l8 O. H ]8 G- N# [, Z </p>$ T* M; V) A2 s4 o
<p>
1 a0 u1 e! W% _$ T& m$ B$ t <label for="notice-content">Notice Content</label><br>
9 a) }( K* L1 K6 q <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>0 J6 W& C) `# L9 S4 b: O
</p>1 r) f6 C& z( k; Z8 q4 |
<?php
D! ]4 H/ [' x2 F0 g$ x }7 r# c, S7 T: f
6 |" N4 d% s/ J- X! _. R1 m add_action('save_post', 'save_site_wide_notice_meta_box'); D% M! _0 h. B1 o5 c
function save_site_wide_notice_meta_box($post_id) {' Z3 h0 j; B7 b3 w& j$ z
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))$ D% g& d6 t' w+ B& V
return;
; T+ X( s. F7 }/ \) w if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)9 f" d+ k: {* X* f- {# r3 ]
return;
! q: i: n N9 R) B9 C
& ~+ I! ?3 h. x/ x if (isset($_POST['notice_title'])) {9 a3 e5 y6 w/ c# |# w
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
% V8 g: g* `+ ` X }
/ u/ p+ ~& t+ s: E if (isset($_POST['notice_content'])) {
, z6 O+ w$ {* z, P update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content'])); D4 ?8 i( S/ G1 s# W
}- O- c$ _2 c& t: i; S( x
}( \7 L B& y+ H" G0 z0 V& X
```
+ @5 L5 l V: Y0 F3 |2 I" v9 o# y
* H/ @7 T; [/ C' n6 | 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
5 \" V8 D3 C9 x, ?% o& h
/ A& A) b& h1 C- }; W! f' _4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:& s! ~" B7 K B, i$ ^
z0 [: Z( L. w- o( D- V; \ ```4 D& b* }9 Y, |3 p! r! U
$args = array(
- e' _; U/ A! k$ A 'post_type' => 'site-wide-notices',( F, B$ Z8 J% y" @3 |1 z
'posts_per_page' => 3,6 v j" |- _/ L/ k) \
'order' => 'DESC',/ h, \' k1 e' v3 P
'orderby' => 'date'
9 h+ a: v: x: ~ );
7 ~# W4 P; A5 m$ H $query = new WP_Query($args);! p% I2 _+ j$ t- P8 B
if ($query->have_posts()) :& C5 H7 V9 g8 ?& c# @( B' B; ~
while ($query->have_posts()) : $query->the_post(); ?>, e' r* {# Z( T! }! d6 [
<div class="notice">
5 C+ H+ O! R- q' x <h3><?php the_title(); ?></h3>
) ^( t4 \3 ~1 [2 B, D <div class="notice-content"><?php the_content(); ?></div>$ N& f0 {" N" S: o0 ^
</div>
" S3 k C7 n" ?9 e0 P7 ~' c" a7 H <?php endwhile;) Q3 ^# E s0 ^: X# ^* d
wp_reset_postdata();. ^4 H0 T4 e; z3 B, a1 l! P
endif;' _/ T; J. F' l4 v& t l# U
```
; [: }& w- _# w S: o* g! Y8 f t, T1 z2 p
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|