|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
8 l# d# ~9 S5 [) O( r3 _1 K9 @3 _2 \
# v1 `$ ]% A$ Z6 A% ?1 s. r" x% y如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# W# O: K5 x3 R% w( N: s, z
. ^" i Q% x9 V0 S6 p" s, R* I
以下是创建自定义插件的步骤:3 y! [6 s2 I; c+ {* Z p
: h! ^! y% K) ~6 [3 O1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
0 _2 A% Y% I7 I6 Z6 j; O2 w. V$ E- H" N; H3 q
```- a: C$ J4 |! I
<?php+ b' Q: S* h2 e$ {2 w
/*
; H9 R! x7 [0 x( s, O4 k6 e+ x Plugin Name: Site Wide Notices Plugin
% M9 }" `3 d: V6 r Description: Adds a new custom post type for site-wide notices.
; B7 G' `) y [- g# i Version: 1.0* S. U/ {' Y( |$ T, P a
Author: Your Name
/ I" l' h1 p# R! j/ U4 M Author URI: http://example.com
9 j% J1 [* J4 i& r */
- C3 R' R- I9 }0 v' P* b
# k9 X3 o6 I) v A) H' ^+ e // Add plugin code here..., r* r2 M1 K& N' {& {! A% u; U, g
```
& \, ?& z' S: a( Q I5 x7 b" l7 \2 R6 N- m: c3 y: P) Q
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。: X' w j1 k+ M8 B
0 y3 b7 P5 V& }6 P: l: s, M
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:7 N6 \1 c3 {2 [' Q
' E0 l5 {* d- l2 ?% l$ |0 ?
```
4 A+ {+ C% l; g$ B% ~$ D% P) n2 U4 P0 E add_action('init', 'create_custom_post_type');
8 |$ S9 o: I% m. Z& ?) e% ] function create_custom_post_type() {9 ?: c" r- P7 j* j, s+ X+ z
$labels = array($ G( `4 s& X; {# ^3 ?
'name' => 'Site Wide Notices',+ R# |, @# M- p# J7 Z
'singular_name' => 'Site Wide Notice',
7 B8 B+ ^$ h8 Q: u' g* ]* ` 'add_new' => 'Add New', {0 W8 g) g, P- e( B; |$ ]
'add_new_item' => 'Add New Site Wide Notice',
7 q% |) G, _1 q# u7 A 'edit_item' => 'Edit Site Wide Notice',1 L( q- B: i% W5 ]
'new_item' => 'New Site Wide Notice',7 f2 J! M: ~( h7 d$ k" e
'view_item' => 'View Site Wide Notice',3 H( L1 C4 U# K# O( X5 C8 |
'search_items' => 'Search Site Wide Notices',4 J) A3 T5 F# p- X% I4 a* R' m
'not_found' => 'No site-wide notices found',! m- ~, Y- ~7 V
'not_found_in_trash' => 'No site-wide notices found in trash'/ o! d3 @8 s, }
);% z' ~) _2 A0 p! q/ J" h
3 w8 s- h1 t1 ` $args = array($ D; q! [8 i" ~' l
'labels' => $labels,
* ]: b/ q; }" Q( f& t) S6 V 'public' => true,4 P# v& {% g9 Z/ b& y
'has_archive' => true,0 s6 K1 d3 i4 y j. r. k
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' ? h! H3 n" |9 c/ ?, {) m& x
'taxonomies' => array('category', 'post_tag'),) b$ L: o0 C6 d$ W* k7 Q3 o4 ]
'menu_icon' => 'dashicons-megaphone',
2 B; x* s" Q; v$ @ 'menu_position' => 5,
5 f, |! a/ T& O( S 'rewrite' => array('slug' => 'site-wide-notices')+ c" c; }! t: @' T( S
);
) ~% m( P2 ]% i' C& x, J
+ j% d, b% q5 C; _6 y' t* U7 o7 Y3 e register_post_type('site-wide-notices', $args);- K" f) N7 ^& F" ?8 }! V) R
}
5 V) A+ C. [, @6 J ```& L5 q, q( P$ Q$ {: U# z
$ [! _) ?- w$ k, P* j 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
: o* p; D$ H( u% q( O$ L* ~8 L/ B: Q+ {
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:5 M' R) t) F/ i
* ^+ ]" e0 a4 ^ ```0 s1 I a7 C y5 J5 Z
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
1 s! D: P! U- E% G2 L function add_site_wide_notices_boxes() {
+ {0 D" ?& O! @; @# X: L add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');) {3 ?5 T* G( ~$ `
}6 ~4 c1 [5 l7 L( H
8 @' O+ v; k9 b" k: \/ H3 v
function notice_details_meta_box($post) {' O0 e# P3 }+ ` G8 Q/ J$ N' U
wp_nonce_field(basename(__FILE__), 'notices_nonce');, Z1 ]4 e3 T& a+ J0 p3 k) P
$notice_title = get_post_meta($post->ID, 'notice_title', true);
- b/ q, Q/ |* n% V/ F $notice_content = get_post_meta($post->ID, 'notice_content', true);2 ~$ J2 ^9 G, `! C
?>: J; s0 T8 B: p# m
<p>' _. `, E* q6 o1 g
<label for="notice-title">Notice Title</label><br>
+ u8 V% X0 P3 K' a5 E* h9 D+ v, B <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
& {; [ B- L# u8 P0 I0 q </p># c, S6 l$ d' L3 [7 V- L2 i
<p>3 r5 m$ c2 L3 l. q0 J8 N
<label for="notice-content">Notice Content</label><br>
. r- p! C8 N/ [" Q6 ? <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>7 g# j6 {: u2 L! R+ ^
</p>4 ~# `; ~5 d5 i) d1 w
<?php
% {! h9 M! ^! m) q3 O9 C }
7 A8 v0 i, t% d# C5 o1 E% s/ q4 ?" \& O& {' R8 W5 @, x
add_action('save_post', 'save_site_wide_notice_meta_box');7 a* T: R$ Y0 [4 X
function save_site_wide_notice_meta_box($post_id) {
: e3 S9 b! M( w) K7 N( J if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))* ?: x; q+ N4 d9 W3 |6 T' ~: |, t
return;
7 p2 ~% m: V. S, g @ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)9 b6 E3 m. B6 `
return;
9 I9 s0 u, a. z: O9 i$ E6 s
+ i' n: E" `, V; T$ _ if (isset($_POST['notice_title'])) {
- i7 Z' D" U$ J0 I k6 W update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
! h7 n* g& R& v; N3 p w$ x) e7 B5 @ }
; M/ V! } K- T# g, E if (isset($_POST['notice_content'])) {
5 G( B8 {& c" @/ W0 g- @, S& g# Z4 R update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
9 a9 g, P. N* ^* _9 E( u }3 y' [( M8 q) ]# @; O: b0 t1 \
}
. L: G$ g5 M! W- ~! D ```' m: @* N7 j* t2 r# W2 x
5 s! I' `7 Z" f( ?& p
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
$ f; x6 c; b, v; m" e8 m z' l% |& d& g
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:, V/ l; E- V/ m3 @2 S$ ~
8 s3 l- h e9 H ```
3 O! t C- i" f- B5 a $args = array(2 S% k2 f6 V7 {- O+ \- T2 m4 E3 ?, c
'post_type' => 'site-wide-notices',
+ q5 k, u0 I$ _1 Y 'posts_per_page' => 3,
. i; D# r4 h" U# H 'order' => 'DESC',
! s$ G1 O8 P( c o. J$ C 'orderby' => 'date'
, J, x" c0 ^- D# Z0 H& m" Y8 V );
' b% ?2 X: b( T5 j) @ $query = new WP_Query($args);
1 C. [4 i* v' P7 _2 r0 i: F if ($query->have_posts()) :
8 O/ L# ?4 u# p* w7 ^ while ($query->have_posts()) : $query->the_post(); ?>4 H& ]4 _. i5 q' M# D
<div class="notice">
4 U. o) m2 w; x+ x4 { <h3><?php the_title(); ?></h3>! I) `5 c9 o$ ~* a- [
<div class="notice-content"><?php the_content(); ?></div>
1 t. y& w" ^5 Y </div>$ W8 D" Y4 I& y
<?php endwhile;
; o |( ~: l8 C( ]/ u8 V wp_reset_postdata();# `* w" P$ @5 M1 I4 D" ]" `
endif;
, ?+ V+ ^+ |9 p9 p5 ] ```9 y- F) l) u1 N; t
& d7 C; V! G5 z" f 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|