|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?9 r/ i# C, X5 i& X% b4 V' y( B9 W7 Z
" _6 @# R: G3 G) H, Z0 ]
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
* g. k9 u! ~1 l7 H! v( Z
4 W6 n! V& W7 @以下是创建自定义插件的步骤:
- ^4 a- j' f( d- @5 D7 S1 c8 W, p' T
& x& @* Y2 O4 f& `1 B3 f- j1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
, V [6 Y" a- C6 b7 l9 k6 x/ e1 ]- ?& m: }- Q" I
```0 o, B% Y$ [( x
<?php
4 H# T6 u$ J8 ~8 [& m1 s& `! x /*
9 k5 S3 d& h! l1 ~ Plugin Name: Site Wide Notices Plugin
3 y3 v9 y8 D* W6 P9 }7 Q3 I, s Description: Adds a new custom post type for site-wide notices.
1 W. U; F3 }6 Z% O% W% E3 J Version: 1.0, b H) ^2 q1 L; F( J1 G% y% S% c
Author: Your Name
- J; I' k/ n6 \- _6 |# d Author URI: http://example.com4 [0 G7 a. }7 X
*/# q! A; l$ O& J8 P8 _
% j _1 f; s. A* N/ }
// Add plugin code here...: o3 V/ S N% P r& y7 }- k: a
```
, t, B( ^' `1 [' C, I/ z
) P& O' {" N7 q( `0 V. f9 L 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。/ H! b4 z) F8 K$ U( ?' L8 m
4 s4 f6 g$ z C5 T$ x2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:" j' \* h) d: I$ o
, h4 v! W$ f' ]) I5 X ```
/ R8 e& u' L: }0 \ add_action('init', 'create_custom_post_type');, ^2 \3 g# w' h1 T. d
function create_custom_post_type() {% G& T& p/ g- b+ _! H. N5 T
$labels = array(7 P3 G* q: S3 W8 ?+ n) i& u
'name' => 'Site Wide Notices',
) F, H+ O; m/ r/ |4 J 'singular_name' => 'Site Wide Notice',2 z5 |0 E- ?. t4 g- f1 A
'add_new' => 'Add New',
2 l8 o- Q: _% }& g 'add_new_item' => 'Add New Site Wide Notice',1 Q- H) R& @: o" I7 f r
'edit_item' => 'Edit Site Wide Notice',, D+ O' {* a5 s& S* }& N5 D
'new_item' => 'New Site Wide Notice',- b6 S9 A2 x( K% i, k. F
'view_item' => 'View Site Wide Notice',
) P4 R3 Q$ u f# q 'search_items' => 'Search Site Wide Notices',0 Z" V- k9 k Y: A
'not_found' => 'No site-wide notices found',$ l# W% K Q3 i
'not_found_in_trash' => 'No site-wide notices found in trash'3 [# V0 n+ J* {, M
);) p, \3 Y8 F0 O4 k' ]1 D+ `+ q
. E# C4 }- P1 f- G. V $args = array(
4 V: b# `2 O7 F5 I2 s 'labels' => $labels,9 |! r. X8 w5 }" K" k Z% m) G
'public' => true,# k: n5 W7 Q1 D3 ~. V9 P3 I% x
'has_archive' => true,, T' R0 s. J) ]1 a6 u
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
3 A( L" h5 |3 y, r, L2 }/ { 'taxonomies' => array('category', 'post_tag'),
$ Z9 E& c% F! j; T d m( d 'menu_icon' => 'dashicons-megaphone',6 X0 u2 U ]# a: u
'menu_position' => 5,
7 C+ l( a( L% z7 d; T: p( Z 'rewrite' => array('slug' => 'site-wide-notices')& T8 ]% m6 W7 R$ Z7 Z5 {& B/ `
);# a0 l2 f. f4 @, ~
1 R: r* M1 x' M5 z* i: p8 C
register_post_type('site-wide-notices', $args);$ h9 o( ~5 Q9 h
}! ~2 I1 _6 H4 l. z S) T/ [+ i/ C
```0 R) _' ?2 E( N9 E) Z$ a+ `& ]7 j
- q. x' X8 m* G. [1 y 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。/ { B9 x3 \2 l
8 V, o3 T2 C0 X4 W, g
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:$ ~% ?9 i# v5 M |
1 k: t0 Y; v2 n* V- @& E* F
```
3 |4 X" C2 k6 c5 j add_action('add_meta_boxes', 'add_site_wide_notices_boxes');" j# N$ {! ^2 l
function add_site_wide_notices_boxes() {& ?$ _* t; ^* o# I7 |& @ z- D
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');5 I# ~, x' V1 M$ L8 ^9 Y! M! A5 L
}. A, w4 M F) ^) K) C5 s' p
+ l! @7 |, b* j6 z+ T/ e" G+ E function notice_details_meta_box($post) {$ O: w; @2 u- ]
wp_nonce_field(basename(__FILE__), 'notices_nonce');( d% _# s% v) z1 B- M ~# b
$notice_title = get_post_meta($post->ID, 'notice_title', true);
) a! M! l/ I) | $notice_content = get_post_meta($post->ID, 'notice_content', true);
$ W6 W9 [* O8 k1 U ?>4 ~) u Y1 A0 B6 a) q* ?
<p>
- p% J( {2 U: |5 I2 `$ q! W <label for="notice-title">Notice Title</label><br>! L3 O6 n: `6 c
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">- b$ {9 e- q6 R- T& Z/ \; u m
</p>
% Y( a+ s, n5 R5 t Y% S( y) d <p> Y% ^9 T. J0 v. } y+ Q
<label for="notice-content">Notice Content</label><br>
/ i0 B/ N" k: S, C <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>" i( i6 T- Z# r( Q/ r" z' G
</p>
+ S" s+ H; s9 N; W) n% \0 n <?php
2 T; a" J7 A+ X }2 U3 S! d* W) H4 x
0 D& p- }/ a2 I7 k5 X) j8 d8 ` add_action('save_post', 'save_site_wide_notice_meta_box');
/ e/ o7 T: s9 C7 `* ]% S function save_site_wide_notice_meta_box($post_id) {& a: u+ l( E# ]0 s7 k
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
5 ]* ]# J8 M& [4 R3 Q7 s, w return;9 ~5 V. ~; W+ Q
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE), `7 J0 x! T: J* L$ K: L
return;
* o6 A& I) b( e! x; h4 P1 o7 L
' b5 V" ^* S5 l; J' R6 V [! U) c if (isset($_POST['notice_title'])) {
. z- c0 i7 ^7 h) | }- s' O* R5 W update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
: w6 E7 b9 ^4 z }3 \2 e1 h+ |8 o% f
if (isset($_POST['notice_content'])) {' A4 g1 [" [. t5 G
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
/ f6 I7 ~7 v) r0 G' \ }
' |6 Q+ P4 M. e7 e }) _' y! t8 L6 N' W* ?% L
```
, L- k" D6 [4 C, _* Q9 t8 G9 D, j0 \* Z$ `; l/ R U
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。" g! y% s" Y3 g) j
n# W% b$ f7 `8 Q f) |4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 C* i/ y ^' @. j J" s+ q: ?. m
% g5 V( D" M) [+ W+ }0 i* n ``` s2 R) O; X5 J6 @" C3 |
$args = array(4 c' m- G9 V! P$ f& A' F* \
'post_type' => 'site-wide-notices',
; S& T' r. e0 R, a. @9 d# G8 R 'posts_per_page' => 3,4 ^3 x, n! v& \2 `; K$ ~( P( E# L
'order' => 'DESC',
: t8 V3 C3 C$ J+ M9 ^ 'orderby' => 'date'# F4 _. I# {3 @- V
);
+ ?+ a# w6 {" C9 s $query = new WP_Query($args);
. L: v! X1 G( ? if ($query->have_posts()) :/ X) }$ F2 H7 l
while ($query->have_posts()) : $query->the_post(); ?>: N8 J- H+ ~* W. H0 j+ W
<div class="notice">
8 j- S1 `. Y1 {: H1 k- h$ t- d% v <h3><?php the_title(); ?></h3>
# ^7 y7 C1 A/ k/ ^9 r) _4 d$ Y <div class="notice-content"><?php the_content(); ?></div>- h) u y( \: f8 p5 n
</div>! [3 |1 j3 @! T/ F3 `
<?php endwhile;
" S/ A8 k' ^; {4 s) q, Q% n wp_reset_postdata();
; C5 U' T& p$ u endif;
/ I) L' g2 _7 g( d& W- v5 } ```
" {3 i6 i. ?7 R! N# H$ Z. q% ]- S; T j% j" M3 F' u+ G3 f
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|