|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
4 R t D0 t( u* } r6 ^' v4 x7 r5 J N6 ?7 ~
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
' k2 _- g& A. e" A! B1 r I+ b3 z% x
以下是创建自定义插件的步骤:
0 ?8 U4 y3 A$ D4 _9 K9 h& G% `
' ^0 T7 M+ Y+ V! }3 s9 c7 u m1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
2 `7 b% ?4 C0 F% Z* }9 S4 n5 z1 e. o1 f7 i- ^
```- U1 X4 A7 m6 l' Y# |1 \
<?php
( ^! f8 [- e' q0 h( s- ` /*% h8 s: ^6 i9 h, J
Plugin Name: Site Wide Notices Plugin
+ ]5 v% Y* `! X! Q! w3 k* w6 \ Description: Adds a new custom post type for site-wide notices.# \; X6 s2 @" T( `1 a
Version: 1.0! g3 g% |& }& h8 x; M, {/ J
Author: Your Name
- c [& s) k+ A/ n/ S Author URI: http://example.com
4 u1 S2 y: Z' v! U% Y */: m2 o5 z8 t" O6 M8 i5 O0 h C
. _) j/ ~" ]* K. j* u% a' G- D6 X // Add plugin code here...
% b& s+ C; y7 p! P T# e7 U: G" i ```
' K/ a% \: H2 T
9 x0 P" t0 V9 h3 v" H6 f! J1 C: g 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。& T: G! e- W. J/ P
, X g" K5 m$ [$ n( \3 Y2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
( v& ?8 u0 q5 |$ E/ X
V8 r8 O* \! g/ K6 ^4 w+ Q4 P ```: r' J& U% ?* \4 p
add_action('init', 'create_custom_post_type');* H- l9 h# m4 b8 c B& B) M
function create_custom_post_type() {
6 v8 e6 i) ]! W5 q a $labels = array(' \$ M. k8 M! }4 S2 P6 Q) S* w
'name' => 'Site Wide Notices',
+ a4 h) x2 x/ Q 'singular_name' => 'Site Wide Notice',
9 p! p _5 `3 g# k- e3 d7 w, w4 M 'add_new' => 'Add New',
. i* D( x8 b; f8 c 'add_new_item' => 'Add New Site Wide Notice',- g/ F: Z* M3 \5 x
'edit_item' => 'Edit Site Wide Notice',
/ c3 W/ I$ { O% ] 'new_item' => 'New Site Wide Notice',+ {& j+ X" V: p' m! m) q
'view_item' => 'View Site Wide Notice',) W8 `' ], b) Q
'search_items' => 'Search Site Wide Notices',# M/ k( N' [7 {% I
'not_found' => 'No site-wide notices found',5 s9 f' J$ G5 m8 M9 ]" s" A {
'not_found_in_trash' => 'No site-wide notices found in trash'2 c$ K) k! B6 f/ F- d" ~
);
1 k/ S' `% V5 j* V- l) [* l* O/ d- R/ e. M. f# \8 ~2 G% Y" p
$args = array(- ~7 c$ f. x7 e2 k" Q: H V; ~
'labels' => $labels,8 \: I( o$ G& Z: H- j
'public' => true,
4 }( h. R, {" @4 _2 z! ]& O 'has_archive' => true,5 s% J8 I# ]. p% F# W$ O: r
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),! P. I: p. L# x- D/ }7 n( i% G
'taxonomies' => array('category', 'post_tag'), |, x0 E8 t" g( M
'menu_icon' => 'dashicons-megaphone',
( G4 E! g7 j" {* r6 f" v 'menu_position' => 5,
: ^) C7 `; f! O1 s+ D. y& B3 l4 j% B2 y 'rewrite' => array('slug' => 'site-wide-notices')
1 X7 @! s& b+ j5 P4 A );% [* `. p5 n, N o- v
0 o( J, d6 n" `9 v4 D, s
register_post_type('site-wide-notices', $args);
% p# W8 U) V6 m3 A( P' u }
0 s) C! E# G `0 t5 c$ }8 q ```
! D! m5 Y; h; B+ N- T
. }/ d8 p# p" M' l2 q1 q 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。$ }& e4 ]8 a) p0 A
) y; y/ {& v2 S. M& @
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:2 o9 \+ m# ]. ?) M( z
+ `& t4 d a0 T- a2 Z0 \/ v
```3 m8 J u3 @5 q( B
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
( T( y. j4 [0 P$ G) m% ?) M5 _ function add_site_wide_notices_boxes() {/ `" u1 l' b1 u+ ?0 C+ _) |
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
) E# H2 W0 u5 q6 \7 V }3 K+ ~! z; O" \' C/ |; b
9 a5 t/ h) |- m, | function notice_details_meta_box($post) {0 U; c4 }1 `1 J0 c$ Y
wp_nonce_field(basename(__FILE__), 'notices_nonce');
% E* E( T: O8 O8 }; z) z $notice_title = get_post_meta($post->ID, 'notice_title', true);- D. D, ~8 M: t( l* T9 m" O d) A
$notice_content = get_post_meta($post->ID, 'notice_content', true);
3 k4 x# s5 V; V, L/ M/ s; d5 j0 r ?>
: T$ N0 }. R8 J* k5 J* v' e <p>
! [/ p2 E* n. k: Z0 h <label for="notice-title">Notice Title</label><br># o& l# S; K4 v$ T! p& p$ x
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">* J- P7 O2 W/ t5 Q+ R: e+ T4 @ O, ^; K
</p>
. W5 Y) f- {% p) @ <p>' G2 c4 f% `2 ^$ B9 q0 _
<label for="notice-content">Notice Content</label><br>- z" U" u1 B k( h
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>8 T$ _/ @& p3 ]4 G3 L
</p>" F( i2 Q5 Y3 v- Z/ v
<?php
1 U4 `- r2 W% W& S7 U4 q }* {; I- e, ~: k# J% ~$ ?, F
$ D3 l2 U+ d$ B, f# W I
add_action('save_post', 'save_site_wide_notice_meta_box');# R' B2 N# a. @4 B y
function save_site_wide_notice_meta_box($post_id) {1 M7 Z. _7 _7 R; d
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))4 g3 Y4 z/ N: }$ M
return;: e6 o4 i! k7 U [+ H3 z% T' M, q& Y# F
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
O; s, [/ K8 o* B return;
0 P" ]: |. X: s! X: v) Q' k5 y1 j1 N4 p7 Z/ }* n5 f! L
if (isset($_POST['notice_title'])) {
2 o$ ]& d' K L8 P$ ~ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
' z; G/ @( R" _ }1 G# `6 K. R/ \" D0 R. u
if (isset($_POST['notice_content'])) {
" U' ~$ x% j$ [2 v0 Q update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));; n9 y! J) F0 N+ B. T% f8 R* M. m
}
4 N& j, Q7 N( Z l }( [( v' a5 L& {9 y6 T$ ^
```
7 z) L! |* p" n- @3 l F: c8 K7 e: ?& I2 V0 f7 V" S3 V; K1 f
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。! i; V0 R" I: {! v
# D" g4 x6 m1 R/ C4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
; p- z& _/ S- P. w! X8 E1 C3 K2 S/ G }5 S. R1 J
```# b/ [* G# Y! X$ N; H7 c1 i% L
$args = array(# Q1 k- }! ~8 W/ g# Y8 J$ S' q
'post_type' => 'site-wide-notices',( o9 t- J% w6 \
'posts_per_page' => 3,' ~ V3 `* v3 c9 a6 }
'order' => 'DESC',
% X v: t) h0 ^" P. b 'orderby' => 'date'
4 q2 o& t0 S. ^( h4 H% Z6 a- W );+ \3 y" B4 D+ e& W- H2 t
$query = new WP_Query($args);4 @7 T6 m g$ u1 {+ W
if ($query->have_posts()) :
2 Y) l& D6 z9 i/ o. v" v while ($query->have_posts()) : $query->the_post(); ?>5 R2 a A. `: N- b
<div class="notice">6 l/ V$ X# M; w {( g! O& p6 O" T
<h3><?php the_title(); ?></h3>
8 N$ @! H: i. Q0 o) p$ d4 z <div class="notice-content"><?php the_content(); ?></div>& C7 m8 \3 p8 L
</div>
; w+ Y6 n# Z6 D, o3 a& _/ C. p <?php endwhile;3 k, \! m; v+ d2 R. F7 Q
wp_reset_postdata();/ u7 s2 z, {0 {
endif;8 B' W4 g2 p1 A5 ~% H" C- a
```
8 T) M2 G; s0 R @6 B, A6 { z& v% H2 T+ f! Y
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|