|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?+ E ?8 Q2 g9 S9 ?' I4 f
, R" c a) ?$ |3 m& @- \如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。' m8 `$ X/ z8 H- t* X6 ]# H3 l6 S1 G# e
/ b4 f2 _& i+ g# E& {, i2 L
以下是创建自定义插件的步骤:" ^) b5 k/ a1 J: ]7 j" S
, O+ m! d* y/ S
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
5 c4 H6 d$ Q6 n1 y* S. C
+ a* N2 X4 ?$ e( Y8 S* ^1 r( D+ q ```, S$ z4 r5 f9 o0 m1 ~
<?php
! \" V$ o& _6 W* _1 U /*
9 c3 Y1 t _4 _4 m. G- ^ Plugin Name: Site Wide Notices Plugin6 x# J3 x( }! }* }" q7 u" \9 C
Description: Adds a new custom post type for site-wide notices.1 E4 V( @+ S- h, d- g3 j
Version: 1.0
- m3 j( V0 U" F" g Author: Your Name) a6 R+ l% L0 g! }8 c5 a' o7 [
Author URI: http://example.com
* ?8 f# C/ j" n5 ^$ O */* D! Q. l4 D4 U$ s) x! T
0 I/ K2 {6 l( p // Add plugin code here...
7 r* m+ a- t% A% W ```
. A7 H8 F% g) b- @9 s' p# m+ Z! d, Q: K! k& n* b% d I2 b
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。2 W- P3 Z3 D6 I |! _4 z
2 Y1 f- t1 J& F6 o, c0 R+ t
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:0 L. C) d f a$ n' i
3 C S% K9 R; k) N& B+ w
```
5 t$ G y$ x2 ?+ D, ?; t/ L" ^ add_action('init', 'create_custom_post_type');+ H6 b& a) H" b. I e
function create_custom_post_type() {
+ [8 _7 O! ^. k, H0 Q. O m $labels = array(
1 j, t: v1 G! I! ` 'name' => 'Site Wide Notices',
4 D9 {3 s% ~* W. p1 @ 'singular_name' => 'Site Wide Notice',
% ]' O: C# V* X 'add_new' => 'Add New',
! a6 T Z$ h" \ k- V7 Q7 } 'add_new_item' => 'Add New Site Wide Notice',
2 S1 x K$ {* ~, Q' O# L 'edit_item' => 'Edit Site Wide Notice',- ^3 t1 m. ~" {0 C
'new_item' => 'New Site Wide Notice',
9 p3 d' Z4 v c9 m, e6 a3 J d/ ] 'view_item' => 'View Site Wide Notice',
8 U6 j U; S/ r5 {) Z 'search_items' => 'Search Site Wide Notices',
# W7 l" s8 V: i9 ?4 K; P 'not_found' => 'No site-wide notices found',, Y7 o0 \- ?4 ^( ?( ^; ^
'not_found_in_trash' => 'No site-wide notices found in trash'
4 E: h* b0 L" L8 }" ?/ A );- R& x' |8 Q! P1 m
3 r! E6 ^4 J& n' H* Q8 v+ \' ]8 O $args = array(
; U1 @- x0 L9 ?9 f d 'labels' => $labels,8 |; t2 v6 H* N! l, W3 ^
'public' => true,
2 ^4 }+ d% X3 N' } 'has_archive' => true,
+ A" ]! V6 w6 s9 d 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
5 f) E! F+ d z) O! _ 'taxonomies' => array('category', 'post_tag'),
3 p6 ^$ v4 y [" X 'menu_icon' => 'dashicons-megaphone',# R" f T5 s# J* u3 j, K4 y1 V
'menu_position' => 5,
0 R: t9 G# `- L/ |2 D 'rewrite' => array('slug' => 'site-wide-notices')
$ D, f# e4 p1 y- E# t) t );4 l* P6 B( x/ U' K1 O% a1 I
: c" H8 i& h7 V4 h register_post_type('site-wide-notices', $args);. Q' e1 x4 [- `+ m' E
}9 B; {6 t3 G; j O/ O$ a
```
: F5 r; d1 T8 `( v% g; h6 [# A
) W7 `2 u7 R, \, |' F t 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。' t% r4 I. i1 ^" g% h) t
6 H0 d+ H, o! R& s0 x$ W# p' c
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
& r+ ^! ]; u$ H9 h% N% X' N% ^
2 E6 o( E2 O$ V7 K( g$ F ```
$ m7 x/ Z5 B9 v2 W& r add_action('add_meta_boxes', 'add_site_wide_notices_boxes');+ w* `0 r! N- T, W
function add_site_wide_notices_boxes() {
0 P% [" e$ m7 P add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
/ p# G' p2 s0 t }2 M* s0 V" ~7 M! R( h1 P$ X
) p; k+ W' f3 k* q, E function notice_details_meta_box($post) {
1 _4 F6 P; B& ~/ h3 z wp_nonce_field(basename(__FILE__), 'notices_nonce');
1 L$ L. d; k T4 p& S $notice_title = get_post_meta($post->ID, 'notice_title', true);* J) Z4 f1 L/ K N$ d* R
$notice_content = get_post_meta($post->ID, 'notice_content', true);
g f) J9 I! q6 j3 X ?>$ H, J! t7 h8 Z7 I- j3 f
<p>8 a, z/ i" Z# H; @: R, @8 p1 _) i
<label for="notice-title">Notice Title</label><br>
$ q; H7 r( G. v; D' I <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">7 ?# D+ N( ?/ _2 t3 z; I
</p>
3 G3 @. w' a/ n1 x <p>: m5 O( f; }6 q# V2 e6 |) @" s
<label for="notice-content">Notice Content</label><br>; D& I/ ]0 s: A9 T; y- W0 f) a
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
7 Q3 ^$ g4 Y8 j$ b; N+ n; M( { </p>
! P3 T. C) ?5 Y9 b- z* m4 G <?php9 L6 [4 ?0 L! m' O
}! Q$ X; H0 u! }/ v% x- Q6 ]
6 l+ i4 M5 c& H# I/ { add_action('save_post', 'save_site_wide_notice_meta_box');3 M. b0 Q, B x% t/ r
function save_site_wide_notice_meta_box($post_id) {
# }8 J+ I$ u4 t if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
/ {) Y* P) ~9 j return;( n8 C7 y7 @' v9 }9 e
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
! y) `0 g% ?- V& r+ `8 I4 d' ]; } return;$ O; V9 T: X3 R( D3 A. o" Q8 U
! Z* D7 a1 g' J7 ?! r/ t if (isset($_POST['notice_title'])) {# s! ^, `8 u+ ~9 k1 z- |
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));- V, S0 s' r5 `7 q& m0 ^3 ^
}/ u- b% J; f$ }& P5 k: d; Y
if (isset($_POST['notice_content'])) {
: T% _6 j5 \9 t+ K* ~" T update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 Q, w( M* v E' ] }* x! q( f0 P S! u
}
$ D5 h% p8 j" Y* i6 w ```
" z, X S! W$ b6 o- Z: w" V
$ T( y7 T9 Y. g) Z6 \ x m 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
* `5 A9 e _1 ? U' A9 z2 p8 q+ f8 f4 A: v/ X
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:: ~7 U' F" i. u9 v ^: y+ k
9 X( v, H+ c2 D
```0 c/ t, b. A8 Q4 C
$args = array(! t; _1 c/ L( z9 Z
'post_type' => 'site-wide-notices',& l8 J! P4 b6 S, f' w8 S3 {
'posts_per_page' => 3,2 U4 M4 o8 |, x3 N0 s, K
'order' => 'DESC',
! y$ t5 x. e: { 'orderby' => 'date'. q+ d; K6 g, ~0 J4 d
);! o! T( ~( T7 D9 ~/ V$ F9 h: w2 C2 s
$query = new WP_Query($args);
: ^) V/ w% ?4 S% s+ F if ($query->have_posts()) :3 h" q) [/ y" T& D+ n
while ($query->have_posts()) : $query->the_post(); ?>0 O7 s6 {) j+ H: b( O+ E$ B; N
<div class="notice">
0 ~# Y* i+ \; y q, m <h3><?php the_title(); ?></h3>
9 ~% e% F3 l) p2 _6 M <div class="notice-content"><?php the_content(); ?></div>4 l; }5 I' V2 z" R
</div>
9 V" _; |: D( E; y8 M <?php endwhile;
( I( B* H1 O: d" u$ M+ \ wp_reset_postdata();
& r0 P5 [0 g' e$ D endif;4 ?9 r w: F3 b% @
```* H! s( K* M6 s2 @9 T5 m- I
0 O& a2 |7 ^5 c( }+ H1 P3 a1 _ 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|