|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?5 M* @3 k4 Y7 B- E5 {/ ]5 P
- K3 w: J+ i4 G$ p t
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
|% I' R, ^/ V% d q8 u6 s. N1 a7 f% d; E" L7 v9 g" `) c, ~
以下是创建自定义插件的步骤:' b. m0 f: H; x/ C9 E, U3 c% C1 E
: w: R1 F% G8 b" C _1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:5 n! K* R. n- c/ q9 m! j
7 O7 N0 D$ S4 U8 D3 I! _
```
6 _$ ?/ d6 K J% { <?php; X' b; R4 n$ B+ l# o) b
/*/ @4 D- J( w9 ]8 ^: z E
Plugin Name: Site Wide Notices Plugin' k7 V' \0 t* D1 q, N
Description: Adds a new custom post type for site-wide notices.* F9 }- }; ?. y# e, j
Version: 1.0
$ e! a$ E) _* g$ H& x1 H3 A( ] Author: Your Name
. r% c1 i; e* T Author URI: http://example.com5 Z9 @% J- m! F7 B; K% Y% }7 v
*/- a0 Q) z; h( e; |
0 H4 r$ q# p! g0 ]: p% p" _6 c // Add plugin code here...
& C/ O8 Z' q. v1 j) F; ^) V ```4 m7 R5 M- m# H( F* ` u) G
* s$ k" ]: k( Q1 O( K 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。5 l1 @& X" ` u) \* L$ a
( h! A C! Q+ G/ j1 }7 P3 ], z
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
4 p5 E2 G) K" E4 A
2 k% O+ j5 s7 V: {5 L ```6 X# K' l& f8 O x$ {
add_action('init', 'create_custom_post_type');
# o3 W1 }% `- S4 |, X d' H9 F6 C function create_custom_post_type() { G5 V& M& A: \- N6 H! B/ L
$labels = array(3 L$ E- F: l( E R& P6 K% B$ x
'name' => 'Site Wide Notices',$ p* e( O4 e+ ]2 S* S) `8 }2 c
'singular_name' => 'Site Wide Notice',
C, u( S1 ?8 j/ N$ P7 i9 l- { 'add_new' => 'Add New',
! f* v. e& ?9 T ^% j9 f2 M 'add_new_item' => 'Add New Site Wide Notice',
9 a# d0 M- @) T! r+ ^" \: |6 P 'edit_item' => 'Edit Site Wide Notice',/ k; X! i0 R+ @! p# P) l
'new_item' => 'New Site Wide Notice',
- ~5 O/ Y5 n/ q7 L 'view_item' => 'View Site Wide Notice',
; f. j0 x- ]6 ^. e, h5 x( q% ?# \ 'search_items' => 'Search Site Wide Notices',: ], z: e0 f7 G( B$ ?. u
'not_found' => 'No site-wide notices found',
3 [1 T' R( O7 w/ o8 V2 Q$ @( ~' m. F 'not_found_in_trash' => 'No site-wide notices found in trash'+ k0 c; R! G& F. Q7 p
);, c. c( E* x' c& x# v
4 Q* e g% z% G7 K' W/ a $args = array(8 K4 d) i/ S% j, n# A: Q
'labels' => $labels,
/ T- d4 G4 y" q% d, k 'public' => true,- s1 L/ v# I1 a# b7 ]7 W
'has_archive' => true,) Z. M) r: k' {
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),4 I; D% L( X& y% P! b0 \7 j
'taxonomies' => array('category', 'post_tag'),$ O( q0 x5 m% a5 y/ M: A; a
'menu_icon' => 'dashicons-megaphone',5 m! P8 B8 T% D( Z0 R0 _
'menu_position' => 5,5 F; w' y9 a7 [! p: F! v" S
'rewrite' => array('slug' => 'site-wide-notices')
+ t3 z: G; D$ }9 C );. Q1 `0 d3 I" l/ r( @! {, J
3 k6 }' W; B7 L w y; h9 k8 |
register_post_type('site-wide-notices', $args);
+ t) O* m2 `2 O }' G7 O' Z: [ o, ?! f* y
```- S7 a l- }9 b& e
& O' H, C( f2 |/ J. J4 Z! n
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
! n, _$ S/ [, z+ Q# @8 |( j; C' H6 j" d" `8 s7 y$ L, C* A
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
( m* w M; M9 K t& ~: ?7 S4 G+ ^0 s+ l* B! B# Y4 I
```
8 t( R9 W0 D4 S4 H { add_action('add_meta_boxes', 'add_site_wide_notices_boxes');! k+ J" x% }3 R- \: C2 e$ q
function add_site_wide_notices_boxes() {' A( Q2 S( E5 |- j
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
$ ^* t( I3 V# u: u' R6 v9 y! A }
1 R W D& o6 N# Z f* U& [$ `5 n- t+ ~, _$ H, J
function notice_details_meta_box($post) {( N; y* |9 h$ `; c2 ^% N F7 l4 L
wp_nonce_field(basename(__FILE__), 'notices_nonce');
* E- R, Z1 M$ l- K $notice_title = get_post_meta($post->ID, 'notice_title', true);
/ |9 R( S4 x! a" h8 i $notice_content = get_post_meta($post->ID, 'notice_content', true);
" O$ q( b2 c a f7 i ?>- [& \ _2 Q( ~% F
<p>( n4 A/ o3 p9 L' K8 [
<label for="notice-title">Notice Title</label><br>+ E+ S$ X K N' E- h
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
. f* C9 z6 L( o' j. t5 v </p>2 d8 [6 ?! n8 [% }6 K# i* b
<p>
% y; [: B1 w+ e" t. M( g <label for="notice-content">Notice Content</label><br> D/ K. T4 b2 o6 M
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
8 o; Y$ _2 E, o7 u# Z- y+ z/ \6 R- |9 U </p>
* B9 C7 o2 f5 ^: Q <?php) y4 C& p# {( ?0 _, x# L
}
) _/ Y% u0 ~# K, y3 i6 a* p* @; b. J( H# n
add_action('save_post', 'save_site_wide_notice_meta_box');! g" ?7 c: V# }, U, B
function save_site_wide_notice_meta_box($post_id) {
0 H0 o( D; i# ~, { N( a u if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
, q. _2 @" U- ^/ w return;
5 n( ]0 I; u# h3 K3 N R# B if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
2 M" x: A% o( o return;
$ T" S1 S. D/ @9 C( a$ W# p, H# e
if (isset($_POST['notice_title'])) {" P' |$ q' m+ U
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
0 ^5 w6 A- o: p1 ^* [ }
R m$ N1 G; H" v* F- g if (isset($_POST['notice_content'])) {6 h( E) d. K* y9 Q6 V6 t$ [. ]# l
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));5 }2 \" s; h, v* B4 |
}4 K4 Q8 [6 j* T4 v. \
}
) ~& d+ `8 J$ B+ W$ K% e ```
" P1 N7 P5 X* x% B# O* {4 N4 g$ ?" M: O* P2 I* q9 X- q8 Y
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
4 o; D5 h/ `( [2 {3 D8 {
; R8 c" v( w, k" X( _9 z4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
$ x1 V9 j: y# H5 v( K) b, p" w4 l1 y. |7 s, M
```
8 N& ~2 Q3 p1 A1 x! T $args = array(% K P8 l2 R s6 Y5 ?0 O
'post_type' => 'site-wide-notices',
" Q) G% Y' o i- Y/ a! H+ m 'posts_per_page' => 3,: v+ W( ?, A8 | M' j& s
'order' => 'DESC',
j: Q5 @- O6 g. e: t 'orderby' => 'date', {: P% R2 T7 @3 a8 r+ w
);- A. g$ u5 x: ^+ q' D) G
$query = new WP_Query($args);
2 B5 K3 d5 v3 | if ($query->have_posts()) :
( }4 f0 i% l5 i* f. a8 T* P while ($query->have_posts()) : $query->the_post(); ?>
5 l' J( A8 J% K6 y4 Q <div class="notice">
( e$ R. J" T' D) R- ?1 \! K <h3><?php the_title(); ?></h3>
1 v" \8 c4 t' k <div class="notice-content"><?php the_content(); ?></div>
5 ^3 p3 |! s; n& e$ ?: f </div>
- m$ `4 B9 p' D/ \. a5 k <?php endwhile;
; t7 r: H; r# \8 } wp_reset_postdata();
* S7 w1 P! S( T, i% T0 u, d; A( ] endif;4 b* }0 P9 n6 p; \8 y. W. W& U) ~
```0 ~5 d' u# @8 L- s
% k/ i0 f9 p, z: X9 N2 d* i- ` 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|