|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?/ V, h* N6 ]% A7 h6 I
4 d6 B, |4 S9 c% q# S# r% B如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。5 U3 c0 R! n* z* v" r, X# b
6 Z7 ]5 \, H1 T; k% G+ d以下是创建自定义插件的步骤:
' K+ o7 ?- H4 K \5 D
: C7 d& K& |4 q# W1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
# H& G) y( _+ C
: @! h2 d) U4 n/ o. l |( b/ _ ```
5 _7 L5 W9 o# }& t' U8 |7 s; Z+ J <?php
8 S/ G! O3 {, \4 S7 S8 i- G /*4 D$ y+ v) j4 p: N$ I, J- i
Plugin Name: Site Wide Notices Plugin4 t0 a6 K. H5 H9 Q' n" D
Description: Adds a new custom post type for site-wide notices.
2 z8 F$ g! }+ _) ` Version: 1.0" ^: H! `6 u/ Z5 I, ]- u+ J
Author: Your Name% c/ N: u: U9 H$ M1 M- ~
Author URI: http://example.com
v8 e3 U' t8 e# y3 r */
/ E( z, y. ]9 u) x& n6 b8 b' m! C# U& l7 O" Q8 M
// Add plugin code here...
* { E% Y1 I7 [/ T/ Q9 i ```
, s9 X8 j5 N. z* t% ^9 T+ \8 Y( E3 J' N* ]8 N- R# i9 ?
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 p2 B' o+ J, d: g7 x
3 [+ v$ k1 x' A' l" i, P8 O1 c& i2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:7 u4 X( C+ l. E) N. |
/ s7 _& s2 U9 U: a ```
8 T, Y( _; r8 g: N: y2 T add_action('init', 'create_custom_post_type');
9 T. ^ d& x2 Q9 Z function create_custom_post_type() {
7 R3 d* a5 j8 ]5 v- E# Z" e' U $labels = array(
1 Z1 E# b q, G, Y5 ?! s 'name' => 'Site Wide Notices',
7 P: C$ o6 _8 P- Z, g; V! W' a 'singular_name' => 'Site Wide Notice',) n; u/ v g7 x5 T1 G
'add_new' => 'Add New',
! z# c! a- {8 Z: U( S 'add_new_item' => 'Add New Site Wide Notice',
6 j3 S I( c, _ 'edit_item' => 'Edit Site Wide Notice',9 k: R) C. |3 y; d9 F1 V- O
'new_item' => 'New Site Wide Notice',5 @% q2 C4 g5 w/ S/ n
'view_item' => 'View Site Wide Notice',& ~7 W% s0 P' Z8 M; i5 F6 B/ R
'search_items' => 'Search Site Wide Notices',- e- r# Y# {' |; e7 c
'not_found' => 'No site-wide notices found',
, w5 v- x9 A2 P5 s- G 'not_found_in_trash' => 'No site-wide notices found in trash'
2 Z; v$ G' g/ v% P );! t( l, p/ y4 {! K6 M2 Y
% J* ?2 |. l8 H
$args = array(
+ O/ O- X* v. d$ E 'labels' => $labels,) s1 |7 S# a6 h
'public' => true,
4 n! B! P" w9 G( r* l$ F- B+ I+ a 'has_archive' => true,6 M" D! K& U) J
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),$ T4 u1 |7 d& G$ o
'taxonomies' => array('category', 'post_tag'),
( w2 b/ n9 E; W* W, X 'menu_icon' => 'dashicons-megaphone',/ S) T2 f: \& P* C; f
'menu_position' => 5,* n) c! n8 \: @. u, z7 x/ F
'rewrite' => array('slug' => 'site-wide-notices')
5 w) c: d7 F# |; |$ e2 o( y. s );# }9 |" ~2 h" ^" n' h* K
- ?: x2 k, y! ^) I" \ register_post_type('site-wide-notices', $args);
0 o: s# W1 z* ]3 c2 D# p }# |9 E3 v4 e1 d/ V4 A
```
& f8 A+ R+ t+ B. p9 F: I. q2 e6 S7 y: Q1 ]! a9 b. _
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
- Y& b+ W" y: y( I( T5 j7 z1 M5 m3 t/ Z
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:4 ?' _0 i3 |* E* K. g- e
( P) C# A3 |& g, J3 h( G
```
# ~9 O8 L5 p3 v8 ?. w+ ?$ g/ X) o, u add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: C& o2 W$ @6 M. K2 R0 ?, @, j function add_site_wide_notices_boxes() {
! ^% X |( ~6 O! U8 }4 r add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
$ ^9 \1 {' b' i1 t& c& R }0 c8 G) }2 ]" K9 {' u7 p: W
; i4 D% f2 D" f# X8 Q( G% P3 d% _7 O
function notice_details_meta_box($post) {* Q: w) t' W% G% ?8 \
wp_nonce_field(basename(__FILE__), 'notices_nonce');. W" m+ u8 a9 x/ k2 Q9 T
$notice_title = get_post_meta($post->ID, 'notice_title', true);
: x/ J+ r$ a3 N! \1 l, N $notice_content = get_post_meta($post->ID, 'notice_content', true);
. R1 B$ B& K; `: k+ r i; c ?>2 W/ I! _7 Q+ ~! o- F( j
<p>
3 n( S# |2 {4 Q- ^ <label for="notice-title">Notice Title</label><br>9 x0 m% K5 c7 h, M1 ?) F9 ], H
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
/ \6 t- j1 B9 B% Y2 I! ~ </p>
- W4 @/ e5 D2 Y- V" g% b <p>
. q: [" \; M! s0 c) |) G <label for="notice-content">Notice Content</label><br>% c( E- R* m4 l6 `
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! g/ X% [: H) @2 F" ]$ T0 k
</p>
8 u/ r4 N# u2 S" U2 t <?php4 A+ t( ^+ F* t- x
}
4 o+ F: u' R2 w' E0 ^9 ~% k6 y6 B& L' W' l1 z
add_action('save_post', 'save_site_wide_notice_meta_box');
1 Z3 R$ {3 _: n2 w function save_site_wide_notice_meta_box($post_id) {* W/ d" L8 p7 Z4 i4 U2 R# K" ]% }2 ]
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
1 a& x. _# _' W \ return;
. n$ k- j! @ K! B0 o0 N) \ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)& d% C' l1 b; B$ Y# j
return; {' s) `5 z3 ?+ r4 u) E) f; S
! {! \& ?: W# J0 z5 X) w1 W
if (isset($_POST['notice_title'])) {: n* D d n* r* M7 ~
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
7 `2 w+ P! H) a% C" K5 U X }" z5 p: ~6 O' q8 e
if (isset($_POST['notice_content'])) {% D5 v8 {, g( b. [; Q1 N* _7 z
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
; J5 \" C$ n5 F0 u }4 v7 D3 r% v" g$ x5 q+ M
}
5 d* E- i0 a- ]* O" o1 S ```* b2 p% E) z- I6 L
8 a, l& i3 A i2 o% D; K
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。4 R* N4 ^4 E& D6 F7 e$ W
1 F. e. a7 _ K& p# Q4 a! i. U
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:& {; p; e# D( m* v
' A, i* P6 N' [$ c: N6 C8 [7 a! r+ ?
```
! T( K8 D: R+ u" s E& g+ c7 n5 S t $args = array(
( `5 q* V' |$ w1 V: k# ? 'post_type' => 'site-wide-notices',
Z7 f: ~( H# u4 d! `: o 'posts_per_page' => 3,! |5 G0 E8 t& {9 o9 ]; A' C
'order' => 'DESC',- J9 W0 Q, |3 g: N# {& }7 N9 T I' H
'orderby' => 'date'6 J+ d9 S' t6 p7 h1 Y- j
);
c" z9 }: `; k $query = new WP_Query($args);
8 |. ]# z+ e" H: f' D if ($query->have_posts()) :
1 Z7 J! E( ?" R( W! U while ($query->have_posts()) : $query->the_post(); ?># a" [# \! i+ h( }
<div class="notice">
2 e# x0 X! P d0 \+ } <h3><?php the_title(); ?></h3>
" f9 w- r" Q& ^: k( Y <div class="notice-content"><?php the_content(); ?></div>, X, n' W0 L) }, e/ D& ]; Q* K3 w
</div>
% d9 d I7 q: O+ G+ | <?php endwhile;5 V; o5 ^, |2 A7 ?8 N" X" y
wp_reset_postdata();8 d; P" P V: y
endif;
: J; L4 w: ]6 \ v" ^ ```( |7 P/ v6 D0 t0 n! _+ t
, l. h( c9 C7 R* Y. \1 Q5 t7 `* Y
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|