|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?9 M; E/ t: |2 n9 j
: O1 e$ e b! \9 O. G7 c/ L4 l, r
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。2 M8 o! Z' l2 Z# p# W' n$ Z. z7 v
2 S# X$ P# \; K2 S! A1 H
以下是创建自定义插件的步骤:
, i5 |" B% g1 i8 h: \+ r5 [- m! |
3 r7 t, C/ z2 G' Q4 k. E1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:. g$ H$ ]: a* R0 n/ s3 B/ `8 K
. K% {* R) ]; {% ~' w& ]( r
```5 Y$ x) [6 d6 r$ C
<?php' x* M8 o: S2 ^+ O, A
/*
5 Y6 ^/ X, t4 Y; G' ~% T$ `% s Plugin Name: Site Wide Notices Plugin. I8 c% f) W( `- P9 f3 ?2 O: h1 E* |
Description: Adds a new custom post type for site-wide notices.- B9 Z( x3 [$ U6 ]
Version: 1.0
9 i. \, f# _; N* Q7 Q Author: Your Name9 w; c) m& I" t2 D6 B
Author URI: http://example.com, m* w+ r. |- s: y9 Q/ R+ g
*/
+ y& N" ~, O$ v, k, \/ W7 k
# ^* k9 V6 n; c8 s9 n: a // Add plugin code here...
7 U- D' n8 B! d, Q3 ` ```. m7 X* y# a" S# G% o
* j' f C: r- ^/ f+ `
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- R. }1 i9 N( ]! o9 L* L! m$ V
" I+ {3 g5 q6 V& r& ?' R$ H2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:0 {' Q, b: G8 |, D0 I0 H
7 F1 ^# c% r1 y, k7 o ```
7 [* T- a: ?0 i add_action('init', 'create_custom_post_type');5 _2 M' C9 H4 e; n7 @7 X
function create_custom_post_type() {, }, M1 |, h j
$labels = array(
Y% j' Q$ Q9 z8 b 'name' => 'Site Wide Notices',* y) L1 ?1 i$ x
'singular_name' => 'Site Wide Notice',- v" ?. D2 I% F( p3 [/ m6 O' K- `
'add_new' => 'Add New',- m# S% L1 f9 J! k" ]
'add_new_item' => 'Add New Site Wide Notice',7 K" f7 e5 Q6 T3 b$ O- |
'edit_item' => 'Edit Site Wide Notice',
* }5 R! k2 M/ A; H5 u& q4 d+ c2 C 'new_item' => 'New Site Wide Notice',
( O8 a) y: j3 j% f& S5 d 'view_item' => 'View Site Wide Notice',: X) t3 l4 Z2 \1 Z0 `
'search_items' => 'Search Site Wide Notices',/ R' h' b9 S3 j+ M
'not_found' => 'No site-wide notices found', F0 x8 ^3 Z: ^; l: @2 [( F8 m
'not_found_in_trash' => 'No site-wide notices found in trash'
9 M8 i0 ^' Q5 E7 a) _/ I; _: J );
' u" b8 x# c3 b" S- Q
! L0 c& q. {6 u. V# v $args = array(/ C9 L8 r4 z- F$ U6 @4 X2 t
'labels' => $labels,
8 a0 I0 ^1 m7 \, B. ^( X6 ~ 'public' => true,
: Q# w& T6 w0 \: b 'has_archive' => true,
/ u% i% r6 T$ W. d0 b' B4 s0 W* Z 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
) v3 `5 N$ W, k1 i( a5 r 'taxonomies' => array('category', 'post_tag'),4 k! u9 y* M1 b4 M8 K
'menu_icon' => 'dashicons-megaphone',
: I$ _1 d: s$ s2 N: o0 {/ M 'menu_position' => 5,( H& g5 d/ P$ k3 c% x7 u! k7 R6 O/ o
'rewrite' => array('slug' => 'site-wide-notices')
4 g: u* I+ x# Q) g7 k* O4 ]3 J );
/ Y" W# H S4 ^' B8 W& {
8 D) g1 n$ `! E9 _" f* j& t register_post_type('site-wide-notices', $args);
3 O- ?( Y* \; I3 Z' Q! q. |8 n }
+ Z" Y2 ]. j% v; L4 w0 e* L ```; C7 n2 P+ M! j* U) e; \7 V
1 N. H/ G2 E$ C5 s# `5 r5 R4 L
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。" t' `# N$ A- A2 \, Q1 G
; g; R& `$ F9 d$ Y# ^) Z- x
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
0 k O; w* S0 H/ S4 Y [/ r" l6 n/ ~& u, a9 w9 w: w
```
) B: u o6 i) q7 \* ]6 W add_action('add_meta_boxes', 'add_site_wide_notices_boxes');& c( _; U0 F9 ?- A1 J: o
function add_site_wide_notices_boxes() {! {4 N" [; F/ E' l: Z( G
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');) T+ R, O: A$ Y& u9 j
}
" ?. Y5 S7 k, T9 I" D9 t% H) `
! g2 h4 u) W2 q9 B) f! E8 I8 Y function notice_details_meta_box($post) {: d' ~$ h8 O1 g* Y8 U
wp_nonce_field(basename(__FILE__), 'notices_nonce');) n/ z& ?/ N# I7 G& Y
$notice_title = get_post_meta($post->ID, 'notice_title', true);' N) X0 Y4 _) T
$notice_content = get_post_meta($post->ID, 'notice_content', true);
2 @( D. q4 L+ d, o ?>* v% K D" d' h
<p>8 L$ c$ Y5 f$ e0 a4 n' E. T4 ~
<label for="notice-title">Notice Title</label><br>" n1 }6 h& \# A R
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">. t |( i3 Q) j- u6 i& Q) W7 h
</p>9 \& L' A. ]+ q0 z1 m0 e5 d
<p>
7 l7 m: b6 F' ^0 \9 e* E$ z <label for="notice-content">Notice Content</label><br>
: R" C& c, w$ h: e0 J7 S <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>$ L; u+ c8 D; A/ [7 M! X9 z
</p>: M9 ~ \# ] V8 r
<?php
' b3 Z E( Q, o$ d5 C! t }: N( P7 K- S7 O/ j1 O- b: n
3 e( u+ W% H( U add_action('save_post', 'save_site_wide_notice_meta_box');
$ ]( q+ \! s% H' r' q; Y function save_site_wide_notice_meta_box($post_id) {
1 c; t* o6 Z, f1 r if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
# j4 J9 }$ f i% J return;
4 D% v; ]" e4 [3 @ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)' i. Q: m+ M% t1 w, K
return;
{ U1 m8 v% Y, X2 _ a# q
6 N2 ~/ \1 u# c, m' V3 w if (isset($_POST['notice_title'])) {
. K8 a/ x Q2 U1 A3 K update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));0 \) |/ h- l( D: z6 @8 o1 A
}. \! R- [7 B7 x2 L' e- k9 \
if (isset($_POST['notice_content'])) {
" R: o9 I2 q! X update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
0 W. q5 o6 t9 h% ] }
" `$ j- a/ w: D v) W- e5 r5 l& ^ }! a; Z: j X* @! [$ j
```, i& d1 h0 _* L8 |4 G' b0 |
$ m9 |6 K( [7 D& {, y; u" {: t 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
0 z: q$ H. D- U6 s- E: g4 `; ~# Y3 @" C
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% g7 Y5 _3 M2 E n# [& U2 R; D' [, x b* n; Q3 M
```, t1 {; k1 A7 _# N" C
$args = array(
( V* i4 [# f" V6 Z/ [- N 'post_type' => 'site-wide-notices',( x. j7 y! g% e4 l0 d, Q. l
'posts_per_page' => 3,8 Q3 {! L7 n% {) _$ J
'order' => 'DESC',
0 l# P( H. V! ~2 P 'orderby' => 'date': F( x4 ~9 | p3 m5 r
);
. k2 l1 y8 E" q, V $query = new WP_Query($args);. o! \/ H; E" V) f+ o
if ($query->have_posts()) :" J7 \" x( N$ h) o$ j+ ]
while ($query->have_posts()) : $query->the_post(); ?>1 @( z6 ~5 k7 F* d9 q
<div class="notice">
3 W6 X# r. {# c+ ^ <h3><?php the_title(); ?></h3>3 a# ?$ f; x$ B5 m) [6 M
<div class="notice-content"><?php the_content(); ?></div>
2 ]% ^2 F0 w+ c1 K; ~! t9 K9 E. ` </div>
: g; y% j& G u& A <?php endwhile;
6 P: G( Y8 z Q5 Z wp_reset_postdata();
- K: _4 `5 ~- e endif;
4 H4 z( ?9 q: f9 f; j9 ~ ```+ g+ w( `9 ^, B
+ y% v. O5 D) _" h# X' F 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|