|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
$ B3 d& E6 Y) h9 Q% T. O) l5 j N4 t3 @/ g! y
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
; k! v; n) n9 J1 F! l# D
) p8 i% q2 n$ m! X5 B- p0 \* j以下是创建自定义插件的步骤:# L3 e- e, U ~6 {
) ]! f) @* t9 F' n7 Q1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
+ ]4 X8 f7 P& ~$ t$ D3 R e8 `" g6 s; W" A2 B7 S/ d! l
```
+ J2 p9 ^/ x& Z; `4 d0 J6 { <?php+ J# b( f- I9 f, p$ Y7 J
/*/ P1 m& ~) f# d* n# p
Plugin Name: Site Wide Notices Plugin
& u4 ~+ T# A/ X* x7 h Description: Adds a new custom post type for site-wide notices.2 C& y7 y( R+ m
Version: 1.0
2 ~- g$ H8 Z6 L, w' ^ Author: Your Name! Z- S ~7 A3 H! g8 ^# M9 x3 Q
Author URI: http://example.com+ a7 F1 q# n+ c0 o Y
*/
5 U) K1 a0 l1 X0 @% J! K* `6 c7 S; `# I) R, U
// Add plugin code here...
/ e% H& B( k0 e' k ```
9 {9 a" x# u' }- F' P0 w9 L6 q# d
$ H! [ G1 j/ ^9 y* y: C 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
/ T, a7 J7 k8 {5 [! z, N/ I
0 c9 D V3 X+ N. G' b2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
% W" Q3 k @4 h, l6 _- R: o2 B, {- E8 l5 |" d/ f
```; R" u( @+ X7 v) P7 G, j
add_action('init', 'create_custom_post_type');* |8 \/ {1 a+ I' Q
function create_custom_post_type() {
- R- q. s3 e: ]* V [ x8 d $labels = array(( |3 T3 z( c7 V' `7 P% g" T
'name' => 'Site Wide Notices',: G7 K1 H4 l) B7 `0 u- J$ {
'singular_name' => 'Site Wide Notice',: L+ u5 n: }6 p! J! J! t
'add_new' => 'Add New',
4 C( R0 o3 o( ^( j 'add_new_item' => 'Add New Site Wide Notice',
$ j9 [- A* ]! f3 A; t1 q+ F 'edit_item' => 'Edit Site Wide Notice',
, V F, m4 s ~$ h9 j 'new_item' => 'New Site Wide Notice',) X- V$ J: D+ [0 P
'view_item' => 'View Site Wide Notice',
/ {( I2 t6 n& |' C( P 'search_items' => 'Search Site Wide Notices',
6 E/ n+ k, J* Z 'not_found' => 'No site-wide notices found',+ v. b5 ~/ O# E# ]+ m" ~
'not_found_in_trash' => 'No site-wide notices found in trash': w! G( A& s7 J) i% b0 D
);1 T# S$ |- G8 ]/ j2 Z5 t: k+ v
q* j1 f; C( c- h+ r
$args = array($ _) }' ]& \$ H" P4 L6 ^& h. s; {' j: X9 l
'labels' => $labels,
& J# G7 S Z$ y& k 'public' => true,
* x& H0 Y' ?0 f. Y4 e 'has_archive' => true,
2 N2 b# ~- }- U 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
; e* ]9 b: C* D3 C+ x 'taxonomies' => array('category', 'post_tag'),8 w% {3 T( s6 b" \, z2 k* ^
'menu_icon' => 'dashicons-megaphone',
: I7 t& Q% {- P$ G1 `* S 'menu_position' => 5,
( y* o- ]7 d. B' R6 L# r 'rewrite' => array('slug' => 'site-wide-notices')
Z4 V& U4 `! U5 F );
; p/ O7 |7 }+ F( O" d0 }5 ^6 E+ R
5 J* k: s6 }9 i; ]4 K register_post_type('site-wide-notices', $args);
6 G$ O- ?8 F3 |- ^ }
# S8 [% H" [- ~( | ```; R6 d1 z( s( V# T
' b9 s+ H2 ]; G9 e
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
4 I. u3 y& |, w5 u7 a, s
" G2 A. W5 Q Q2 \ F1 y/ w& u5 e5 l3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:% o5 R1 X1 P1 D# G- X
: U r7 f# X" N- D4 S; S: Z i+ b k4 F
```
1 z% U3 T% {7 N add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
. v) W0 C# v3 k) v7 N function add_site_wide_notices_boxes() {1 m1 t3 f6 o5 a, c: E! i) v
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
) {/ \2 a' k' Q1 k" C }5 W; ~: z' i* B0 q+ O9 x
W) g5 c+ x5 a( S function notice_details_meta_box($post) {
* I8 T0 K0 s% b# o- O6 X- o) p wp_nonce_field(basename(__FILE__), 'notices_nonce');
$ C n# | o8 S $notice_title = get_post_meta($post->ID, 'notice_title', true);5 j# g4 e8 M3 _1 L9 U) f% F$ \
$notice_content = get_post_meta($post->ID, 'notice_content', true);4 x/ p, q: |0 R$ p
?>, i: r2 ?* @! @
<p>
. V; |0 j$ @ K1 Q0 X <label for="notice-title">Notice Title</label><br>1 j& n- `2 T; @2 m" O& \
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">: t" c5 D- y6 ~1 X
</p>
6 n7 s2 J' @4 a. T7 |( J P <p>
# O s8 H/ J8 G. O3 U <label for="notice-content">Notice Content</label><br>
4 o6 `9 ]/ J* ^4 A( @/ Y <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>1 ~7 H2 N6 m3 m- B
</p>, k; h" d8 v1 P s L! ?
<?php
( l: R' J; e/ s: l; b& l) G" s: o }
& c; Z7 o! K! J' d/ O6 t1 |: O( R' `
, s6 o2 c- ?: o% p. c add_action('save_post', 'save_site_wide_notice_meta_box');
* Y# r" b5 h3 z- f+ b7 H function save_site_wide_notice_meta_box($post_id) {$ g1 Y8 ]! o: G
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))% q3 D. c7 R# k+ q2 V0 B+ L
return;& L9 ?/ C/ l5 F0 i
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)5 V& v! [* K9 h% i) W5 F
return;0 W9 }" T2 N7 W$ s6 L! ^7 t) B
$ {) q8 I- P |; g4 b) f+ L! R if (isset($_POST['notice_title'])) {
: [( Y. S8 i ^! |" `# W! L! i update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
4 [. k' R9 d' K- Y }
$ }: S- ]4 ]2 m& ^$ ? if (isset($_POST['notice_content'])) {& E) N+ P$ X& \
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));9 j6 x4 [- S% `9 x4 s1 Y( w5 Z
}
! T i: j, y4 m% N6 H: T1 R( | }7 o- E M' T! ?% g+ W, n) \
```
: B5 Q/ G) v( v* e, n# y/ y% a& K4 L' e0 T8 R9 k7 _
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。' M8 F; K7 p; E/ n
$ w$ V% N; ~; G* S
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:( S: K" ?3 @3 S, A/ y
4 W2 Z8 {7 Y+ i1 I# I ```
. E& u: f, l) U $args = array(
; a' ^; P! _" H6 j6 e3 o 'post_type' => 'site-wide-notices',% m# r& M9 j# \1 z. R
'posts_per_page' => 3,
0 G. ]* v$ o2 e7 v2 b* f 'order' => 'DESC',# g# v2 C1 D5 F u# q
'orderby' => 'date'
( w' k5 f3 R* e" b [7 w9 { );$ K" l# A" K: I6 D+ \8 U
$query = new WP_Query($args);. c* P7 e; P. H( B
if ($query->have_posts()) :' t0 J4 T2 L" n+ [( \+ r
while ($query->have_posts()) : $query->the_post(); ?>, Q* S8 s7 }1 }; {
<div class="notice">
0 d/ V+ }% |) c1 p <h3><?php the_title(); ?></h3>6 o1 \) j$ j+ z% P& M
<div class="notice-content"><?php the_content(); ?></div>
" p6 _* v; x: Z# W" ~ </div>& D+ u3 X% Q5 S# V* n5 X. v
<?php endwhile;
* e' Y* }) a$ V, W! S wp_reset_postdata();1 ~# U9 h1 @1 j
endif;
N4 v! n4 I2 P- } ```7 [) B5 z/ x/ B
/ U+ \( S4 D$ o' s+ I 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|