|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?* j1 v! t* h! S' U- ~- H( K, U
% I9 v/ P: c$ k% j5 H0 M! O如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
2 g$ J ^, ~ z# a( Z: j! g( G+ w7 s& m0 O
以下是创建自定义插件的步骤:
& {# Q2 I* D! ~# L
! E! v1 H V2 A1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:3 |# O* |0 Q( _! L8 u4 U- ~/ s
9 s3 o! l, L. G; X5 h
```' w. }2 p+ i+ v' h U
<?php
. y; K( R. S, z0 }! ?+ ~ /*& Z5 `3 `( n9 V8 d) ]9 @" q( g
Plugin Name: Site Wide Notices Plugin K3 \& J! x4 b& r# S Z
Description: Adds a new custom post type for site-wide notices. T9 e, b) Z" S% s
Version: 1.0
9 [% w1 E5 J W* ?! F9 V Author: Your Name5 _, K. o1 y$ ~
Author URI: http://example.com
/ ^* w3 J0 `* g- {" u3 H */
& o+ {/ \. q! L% M2 Q# o) i; J1 G. _
// Add plugin code here...
; G, l' g: F& D ```
" @3 v. z6 k) V1 n" o4 N
6 R' g, K4 S3 r: W4 b 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。6 h8 h& |: I: e- |' D" K
: R1 F9 R* T' d6 T+ o# f2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
$ i8 g( M1 p7 o& E+ b' b
4 N/ [2 ?# b* H' Z ```' V3 j/ ~& r, ~; R+ a4 o9 q$ l: _
add_action('init', 'create_custom_post_type');5 s" X8 S# B9 K$ q0 u5 H
function create_custom_post_type() {$ }3 r$ S7 a$ w& K4 e
$labels = array(
; b( \: v: z4 c( O9 Q/ x 'name' => 'Site Wide Notices',$ m. C, Y) d$ S' C7 P" B5 t: v
'singular_name' => 'Site Wide Notice',- C( s( {" Z2 N- p4 `1 [
'add_new' => 'Add New',. k0 s+ m) |7 n- ~! l2 Y' V
'add_new_item' => 'Add New Site Wide Notice',
# Y5 M* K4 G( F' L: u+ u& {4 B6 N. u 'edit_item' => 'Edit Site Wide Notice',) g# b4 j1 N1 @6 ?9 K" w
'new_item' => 'New Site Wide Notice',
# K# Z. n6 y8 w a( e! ?0 K& c" u R" @ 'view_item' => 'View Site Wide Notice',
2 G* |( M3 ]! G$ s. Q- w 'search_items' => 'Search Site Wide Notices',
, O" E% J$ ^/ [) |8 A B$ N0 i; Q 'not_found' => 'No site-wide notices found',
2 x2 z0 P1 T3 `* l' K' Y 'not_found_in_trash' => 'No site-wide notices found in trash'7 P* V/ ~! ]0 @4 X6 Q( x
);
, G9 f4 p1 \2 r! k" O
2 \* z3 v9 m1 W: Q! l7 c; V $args = array(
# Q9 t" z* q$ C$ c# b7 W! D+ E$ T0 y 'labels' => $labels,
5 B4 z" o$ h7 h 'public' => true,, ]% V6 k9 O% A6 S
'has_archive' => true,, E1 M. k0 Z C5 l
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
+ m. T$ t. F ?0 t+ L+ U 'taxonomies' => array('category', 'post_tag'),
- b- q/ _& {6 z+ ]& j0 | 'menu_icon' => 'dashicons-megaphone',
& i' o) j( w6 H( R } 'menu_position' => 5,
8 I7 y2 ?- s$ m$ z! O, C/ ^4 ]5 b3 { 'rewrite' => array('slug' => 'site-wide-notices')
5 ?: J/ w2 m* {! Q- L6 Z' X) | );
) Y: c0 A, J; R+ {' }6 \: W, j- v9 q! @3 i: C
register_post_type('site-wide-notices', $args);
! z3 j l* S7 [2 ]4 j }
4 a0 F1 B' u9 G& b ```/ ^/ M6 ~) O: T. R
0 N$ z& [2 z( u5 h/ m# F 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。0 x% u% t' h2 M$ @+ j; x1 I
, W% ]! T) B+ r
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:7 L+ Z: \, d3 Z7 ?% r" [
/ h6 U+ v! J) X% t- Q7 ~; V. F ```1 n1 u) m( ]4 d9 ^5 ~1 L
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');. V; n: L+ |+ e: n6 m# \
function add_site_wide_notices_boxes() {- w3 Z+ }4 ]& ^1 T T
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
! M4 U: g+ ^3 M( {' s: ] }3 P, V! ?9 o, Y6 s: j- U- L9 l! M
" k6 e( C# S& k+ Q1 d' g o7 _ function notice_details_meta_box($post) {
: R" K4 ~3 u$ Y8 G$ A wp_nonce_field(basename(__FILE__), 'notices_nonce');
5 q8 j' r0 ~6 Z $notice_title = get_post_meta($post->ID, 'notice_title', true);0 n( e' k5 F) {- ]4 z& D
$notice_content = get_post_meta($post->ID, 'notice_content', true);% r) ], ^8 N. d$ t) `
?>
$ S7 Q8 x4 w5 l" O <p>
, S! Z2 c1 [) H" I# T( c <label for="notice-title">Notice Title</label><br>
2 g: e8 x0 t8 t% o <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
% f5 {8 f1 V( l6 A6 H- _5 s% t </p>3 g+ Z) z* P- a8 Y& g- _
<p>
9 v0 r* D5 |& r- d- m3 u# O9 a; ` <label for="notice-content">Notice Content</label><br>
. y( y* o( w+ y8 e: [ <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
7 y, e7 J. S( e" P% t, Y </p>
* F, ?5 F& w9 ^9 H' b4 g( K, K: p <?php
0 }; m1 |: x% ~. W0 t }# l) x8 k7 I& l+ J, P4 b
# }8 |: x8 B' E4 b) K5 a" [2 f
add_action('save_post', 'save_site_wide_notice_meta_box');
7 }3 Z: d3 t% C8 }1 B# [ function save_site_wide_notice_meta_box($post_id) {
; Z+ h1 I5 v+ G; J9 ` if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
7 N6 V' T( w1 ]$ \1 k$ M1 J8 o4 B4 O return;- }! T1 R0 S5 D& J6 Z, C
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)0 m$ J; T, v( f0 h
return;0 L: p0 {! I9 y3 O! S+ \2 f2 D
8 o8 s/ O | x% q9 E$ E& V5 R
if (isset($_POST['notice_title'])) {- d5 S! }+ _; I; }. S" f2 h/ V
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
- {0 R7 a1 G7 k0 _: H }
% }* a$ ?2 Q% c& k if (isset($_POST['notice_content'])) {
$ y6 y5 K" Y) k) W update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& |9 c8 M7 @9 \* J# `6 g& h$ E }
& I. U; H+ @" M }' i1 ]9 W2 ]! B$ P {: H8 O$ d
```
4 N2 U+ J# x( d% |9 @' [- D! ^% b# {" M! M* q4 f0 }
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
) ~; ^5 r7 U+ f, `5 d) H6 {+ T# @& l8 r1 q
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 F" Y( K7 j1 Z' `4 F+ w0 N
9 K" t& }6 w2 \6 |% Y* j$ q& Y
```" e0 `$ X: q7 j' _4 x: `
$args = array(
; r0 S# b1 i Z 'post_type' => 'site-wide-notices',
- _, a- G C5 o* ~! ? 'posts_per_page' => 3,# k& C0 Q, @4 b, r0 m0 X& L: e' z
'order' => 'DESC',, p/ j# b2 X* |2 s- M" m
'orderby' => 'date'1 T" H; ]" l/ v; v4 F
);4 U' M. S3 R) [% |( b
$query = new WP_Query($args);
9 n8 b8 t- {' J) C- C if ($query->have_posts()) :
( S( }' w0 F8 n% w' k) J while ($query->have_posts()) : $query->the_post(); ?>
4 J# a2 q3 d" T' f6 R4 a2 `" I; T6 O <div class="notice">
; y% H1 B) }* a5 x% Y6 j7 ^+ A$ P <h3><?php the_title(); ?></h3>0 A3 y# S2 L. L& J4 t% L' [
<div class="notice-content"><?php the_content(); ?></div>6 Q. q& R% d Z4 `' m0 G4 K
</div>. M1 U$ p1 }* x* z7 s
<?php endwhile;" x; C" |, T2 p: k
wp_reset_postdata();
1 @% T! i1 m4 d' f endif;( A0 F, e3 p* s" d# s& F
```0 T; ?5 h: E/ y& Y
, ^1 f" a$ Q1 r8 \8 R% K. O. v 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|