|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
' T2 I: M7 t+ |8 `- j3 ?4 z @5 d+ y0 z! D- X' I) E
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。9 X. @& \$ Q/ H3 ?+ X
' Q% j0 i3 I+ v
以下是创建自定义插件的步骤:+ y! w( p4 Q+ s' Z! ~+ s* E; t" s
2 v$ J, P/ H+ A# c1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:3 i5 _, I5 S9 R7 c) m9 C( d3 F6 l9 F
" ^6 ?5 B0 x2 t; G ```/ w( E9 f; a S
<?php) {7 |4 H8 }' D1 X( O; \
/*2 M; g2 o- ]1 o+ K# ~' \5 ?7 b' `2 j, e
Plugin Name: Site Wide Notices Plugin- B: {; u/ ?; U
Description: Adds a new custom post type for site-wide notices.
7 R1 f- w9 t" N/ j @8 R Version: 1.0
/ g$ b+ e+ s* H Author: Your Name/ e& z$ Y7 B) p
Author URI: http://example.com
) F% z8 W" ?+ P( q d1 V& I' b */: \" I2 g! @! Z% B& w
$ z: x- F I r // Add plugin code here...$ d- D1 U( e+ P1 N3 |$ d2 j
```$ B8 k4 I6 q3 c/ S; x- Q' ~
9 c2 [# a6 H+ \% T! Z9 D
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
6 a2 N6 Q r& l; d( e7 T
, B. U" Q- X+ n1 {2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:) Y6 B& E' I' D2 `
$ k& H7 V! b4 f; o9 ?5 Z ```" E! Y X9 Y& `) x! ^* x
add_action('init', 'create_custom_post_type');
: v# w& m* h& a m0 @ function create_custom_post_type() {# d* [) m1 g7 |' f! q6 D9 |) d/ _! L
$labels = array(6 `" f/ @4 a, J# M
'name' => 'Site Wide Notices',
( q. M$ Y+ d; O" w8 h 'singular_name' => 'Site Wide Notice',. c9 L0 y2 E: B4 Y) W' u- O
'add_new' => 'Add New',
n$ {- j: d" D: S$ x 'add_new_item' => 'Add New Site Wide Notice',
9 B3 Z, D6 g0 m0 q 'edit_item' => 'Edit Site Wide Notice',
7 A+ w2 k" Q7 v1 V$ C/ q( J) X9 l7 a 'new_item' => 'New Site Wide Notice',! I% n7 U+ I+ G: S3 m* i3 Q, A& L
'view_item' => 'View Site Wide Notice',
( q6 B$ n% u1 Q) W' m# ? t# e 'search_items' => 'Search Site Wide Notices',
+ o) m2 Q% x) H+ I5 M' s7 K4 } 'not_found' => 'No site-wide notices found',
5 ]' o# b# q, Z5 B 'not_found_in_trash' => 'No site-wide notices found in trash', M$ C+ i/ F( I6 z6 Q
);7 j7 F) q7 t" g+ p# G
+ t; [2 g9 v" ]8 f9 {
$args = array($ F* s' u$ z1 x/ ]- e
'labels' => $labels,# t) B5 v( _! K& O# F
'public' => true,
( H, F; u4 t% W E' N" Y0 `0 E 'has_archive' => true,7 f4 f4 K# p9 [. R3 f
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),; ]$ T3 z0 n% n$ j1 M3 w
'taxonomies' => array('category', 'post_tag'),
, c6 R q0 z" K* t9 _; Q4 w 'menu_icon' => 'dashicons-megaphone',. i' P% o6 @6 ]
'menu_position' => 5,
7 ]7 N$ `' ]( ~! }6 J 'rewrite' => array('slug' => 'site-wide-notices')
6 q- w+ d3 z/ }0 M; G* O );: \* y/ J1 V' r0 a
2 W3 v# R' W7 T4 s0 r. w register_post_type('site-wide-notices', $args);+ B) `) s4 l- g
}
: P7 B, ~# P; y: M0 W5 g ```3 F3 z+ X; R& B2 b5 c- i0 s
7 A& _+ d7 n+ Y' c5 P 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。* p( O/ b+ C! r6 M7 C: v+ N% ?7 N
9 {; N5 R- W9 E$ x4 _3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:& R8 Y; y. s1 X% K D
) S5 v2 r. K( E3 m$ Z4 I ```
: j, _+ r/ r; {) x8 \, ] add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
* N) b+ \3 T8 ^8 D function add_site_wide_notices_boxes() {
; E+ \' R: A1 [ add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
1 Y# O! |$ \6 C }
: s8 K9 `5 r' n( v' R6 f3 Q6 x
/ c$ W- @% R1 T9 m: M a3 K function notice_details_meta_box($post) {
8 g; [: E8 j% v( R4 x3 J5 C wp_nonce_field(basename(__FILE__), 'notices_nonce');# l! T ^4 I( t: y9 e
$notice_title = get_post_meta($post->ID, 'notice_title', true);7 V" w$ I9 {, ?0 m+ |; M
$notice_content = get_post_meta($post->ID, 'notice_content', true);# t0 P1 R1 W+ x- Y2 z P
?>6 s# V/ \5 ` x" s# I E& C
<p>' _7 D+ X5 ?& o! ^0 v
<label for="notice-title">Notice Title</label><br>+ A8 s. Y+ Q( q0 E% q x) B# x: C7 a
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">0 a* t, i# Z/ }8 [' U, i
</p>
0 \) s( w$ k) `9 O) L' p4 `% X. ~ <p>
+ ^% e; k6 r! D7 @4 { <label for="notice-content">Notice Content</label><br>) U0 v7 p$ \$ _9 T0 a% k1 _
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
# N2 T9 ^- o$ ?6 M5 |, y </p>
* t3 ?* M4 e- T. ^9 L3 Z# s <?php
- S+ q, r; O, X# s) G" b! g! a3 t }
5 R8 b: H. c( [( k; u1 M8 h
+ h) D0 j) I* @( Z* C add_action('save_post', 'save_site_wide_notice_meta_box');2 Y& ?8 H+ J1 U q
function save_site_wide_notice_meta_box($post_id) {
7 H& M C1 s% {9 _2 \ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))7 O. Q4 Y7 A* J$ \" R6 I; Z* g4 @ b
return;
6 b) N% k& W* k! W9 {' l, c1 F if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
* |7 T! M7 C# v return;
& k6 O X6 c0 \/ Z; j2 S
1 X0 [' x4 L6 A) h8 Q$ { if (isset($_POST['notice_title'])) {# ?1 D3 E: c4 `% e x3 ?
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));, L# u* |; j. C8 g1 w8 Q; w
}9 }! O V0 c' S: W
if (isset($_POST['notice_content'])) {
& M* C7 I9 `: G4 _/ v+ N/ H update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));5 J" k8 p* {9 d2 {8 d
}
: n9 F! U0 N" s: ? M }
# M( Q) p, L8 g( } ```! H* j7 A& X( z
. i$ L! V) P+ g2 ]0 ^- p
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
4 w4 a, s' ~0 ]& N$ O. L3 g/ U8 _9 w3 `8 T2 i
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:5 {, g: M8 ]" D+ P+ K# E0 ^* B
% k. @" f' o" A. F+ {
```, l5 ? \: ^8 j/ O. f! K
$args = array(: c2 T# b+ v# q
'post_type' => 'site-wide-notices',7 I H. W9 i3 L* q+ s3 P7 u: O
'posts_per_page' => 3,
$ o$ t' E; ~" e; O4 s' \2 F! x2 a 'order' => 'DESC',+ E% {0 w, b, _; g2 y8 C6 G% l
'orderby' => 'date', t5 ?& F" [1 m$ d; i" e: k, B; b
);
8 T( e0 t1 A M: c3 Q $query = new WP_Query($args);
: j/ P: Y) S$ w( G( t* D( _( @ if ($query->have_posts()) :% [: k( ]2 W! w) \- K1 `7 g) E1 H
while ($query->have_posts()) : $query->the_post(); ?>+ H8 c( I5 |( d0 `# t2 e
<div class="notice">! o; R% x# S' f& h6 ?7 Y
<h3><?php the_title(); ?></h3> {& E) o+ A1 v7 ^$ ~0 A
<div class="notice-content"><?php the_content(); ?></div>
8 o" u" t. h8 h) ~ </div>3 G( Y! W+ i/ Y B6 v
<?php endwhile;, c' Y Q/ G' }5 r) m" e5 l
wp_reset_postdata();. e6 J" |) a0 r V2 D: M! ]& R& U
endif;4 `$ o/ t0 x) |! I; O- G
```9 C. z3 R' k8 L9 Z
! V3 _5 `9 S+ O; M5 J
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|