|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
: k. l: U' v' G$ V2 P. z: M: t; A6 j( g; `4 p
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
2 C1 N5 Z7 D- g/ N- D @ u$ k+ b; b2 N
以下是创建自定义插件的步骤:3 Q! J4 ?4 J9 k S, a1 _: O9 A
8 s: s8 P5 l. C: z1 u7 |# [; c
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:' K0 J+ e) [/ @& ~
( |2 J. g k6 ? ```; N: A* R# ], m
<?php
2 |: a& o+ h; m' ~% K3 I /*6 P0 t+ r/ \8 i* k+ P4 c
Plugin Name: Site Wide Notices Plugin$ j; o, R4 o F& P, y
Description: Adds a new custom post type for site-wide notices.0 I* u) m; m# q6 X& d
Version: 1.0
& G2 d' K' K. |( A Author: Your Name
. S$ a6 Z; {( j' p! Z0 [' Y Author URI: http://example.com
. S& I- c+ T! Q+ @- S& W */
) H: c# f! { V- Q9 S' f2 ~/ l2 u# j% t4 J
// Add plugin code here...6 w2 x g) e! z3 M, u
```
% u2 u |& f4 y! f0 K% K, v" B" e: O# E# U6 q5 h; y6 ^
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
2 O. e7 H P1 Y! H3 T4 B8 R( U0 ~1 ~& \% Q1 [/ T$ \2 d
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:3 X$ |1 P3 V+ L3 I* n3 l9 y
2 R2 {- |- ]% U% v% h6 U
```
6 o! j( A; ?8 d! Z2 N# @ add_action('init', 'create_custom_post_type');, ?. f" o! ^ g1 h
function create_custom_post_type() {
9 c! d$ A' U$ B9 ~2 a" o $labels = array(+ h# f5 R+ L! M, _0 } N
'name' => 'Site Wide Notices',
! h: {+ Q/ X: ^# E2 x0 Y 'singular_name' => 'Site Wide Notice',( T% ^. q! t8 b) q) w, }
'add_new' => 'Add New',5 L; ~# ]/ s% T8 _7 l3 P
'add_new_item' => 'Add New Site Wide Notice',* j8 S; T+ p) z# N! ^+ N
'edit_item' => 'Edit Site Wide Notice',
Z, j5 ^, Q: S& C 'new_item' => 'New Site Wide Notice',, Y' E, v% Q# u1 M* z- i/ B; f
'view_item' => 'View Site Wide Notice',
8 d2 i* Y7 G" T& E 'search_items' => 'Search Site Wide Notices', m% C) P+ s) b( ]/ g |
'not_found' => 'No site-wide notices found',
5 y1 a R, ~, g* U# m- a) S( b9 ~; ` 'not_found_in_trash' => 'No site-wide notices found in trash'
0 \* q3 V0 m5 }- F7 s3 g5 h' h; N );
; ]% t3 T1 Q0 g* A! ]1 R) k f
. \/ J8 [6 c% M/ ` $args = array(" `' ^/ Z! z; B
'labels' => $labels,
/ V9 x/ J+ |" G8 Z; o 'public' => true,+ ~6 r& D5 q) Q
'has_archive' => true,& Z1 z9 X& S: b( ]. S
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
8 C z0 @* ]; z+ W# v# u4 ] 'taxonomies' => array('category', 'post_tag'),
7 ]! x, ^+ W5 P( u 'menu_icon' => 'dashicons-megaphone',' n( ?9 q- e, o; h1 @+ L$ f: p" |
'menu_position' => 5,9 y4 }. O; x3 _$ o! }6 W( q1 X# X
'rewrite' => array('slug' => 'site-wide-notices'). d' X4 L# F& Z- k1 b
);
# ^2 n, w9 ?! i9 ]( g4 y/ ]: b7 L, f# }4 T# z
register_post_type('site-wide-notices', $args);% R2 d( J2 l$ `. Y! p
}
: `1 |$ y* F0 a ```
( r4 R5 `# R4 [4 @& v/ }& ?5 D
& M: ?$ _ Z7 p0 V* h$ p1 z: Z& k 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。 k1 D5 x# a1 m' y1 |
% F4 Y+ m$ ~+ l; r3 B$ o3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:& Y. w" m. B" ^3 K, s2 s
# A8 \: u$ s. b* G6 Z ```
! c% M' h7 h& N9 k3 V add_action('add_meta_boxes', 'add_site_wide_notices_boxes');: n8 H8 P2 \) K* B" _2 I5 S" ~
function add_site_wide_notices_boxes() {8 E& I6 d( ^' [2 o3 ]+ t$ u1 x
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
; a# ^1 K9 m+ o6 d, h& f) f& q }
) i- D `: M9 M, F8 X. s, v+ \* g n5 U
function notice_details_meta_box($post) {+ j5 d9 t/ S7 b4 a* q
wp_nonce_field(basename(__FILE__), 'notices_nonce');+ R. x# M+ w+ K; d3 W f* \
$notice_title = get_post_meta($post->ID, 'notice_title', true);
/ ~) z" O/ ? K* l0 R0 q $notice_content = get_post_meta($post->ID, 'notice_content', true);
+ L# P+ \: ]4 d' P9 D ?>
# T- x8 O% ?. d# Q9 L <p>
% q6 {; q$ y1 R, e$ [' p <label for="notice-title">Notice Title</label><br>9 b3 k, A4 h% }9 t/ |
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">9 E4 i5 d3 f- k1 k7 Z
</p>
& U. d4 i0 k: K" b/ } <p>3 p+ Z! T, |2 \) e( x- ^$ \0 O
<label for="notice-content">Notice Content</label><br>
7 M* u Z4 w3 m$ d/ _7 `9 F( i& J <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
( r+ q* `4 S- B/ }) @( B </p>) ~0 j7 |+ z9 |8 z
<?php* W( F y8 M2 Z
}& B7 k* H M, x
* ]8 O& f4 | `: U) |) T
add_action('save_post', 'save_site_wide_notice_meta_box');/ n& @7 I3 P/ g4 I* M/ e4 _
function save_site_wide_notice_meta_box($post_id) {4 M* G. \: f2 ]4 h+ [ v
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))# v( h; M' E2 N2 Y3 _, `
return;
+ k8 U: {5 C# P if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)9 A v. L8 q& A6 u% o6 Z, r3 N. E! I
return;
/ C+ T ~3 h; s* q) I. a
, W9 m9 l, i- ^; J if (isset($_POST['notice_title'])) {
/ h% y2 \9 I5 O: _) q! M t update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));3 I+ y% a3 G( I4 z: u8 g- @
}6 g. m6 f5 W$ z6 d6 Y3 {
if (isset($_POST['notice_content'])) {
! @' O, N; l2 g* Q update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));5 f& O2 m/ G/ M3 N) i
}3 X2 M9 k. `3 |, w' i) L
}
: l$ ]- O$ D/ H5 w4 f ```
' [* |8 F& M" R3 v& u7 R# n' ]: B- x9 T; h/ J; e) a$ p
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。. u% f" F; p) F% r% V
, }6 d" M3 r* A* \$ y5 ~
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
p- A7 W8 L8 x. D; ?
$ U( I+ q8 v+ ?1 B9 ` ```
: q, x- i7 X% |) O $args = array(
" H% h& W5 h* I& W+ @/ R 'post_type' => 'site-wide-notices',% C" h" x; l9 C7 X
'posts_per_page' => 3,
" k/ q% ]% F* o7 t 'order' => 'DESC',
! {% X3 t! U% f8 d& ` 'orderby' => 'date'
, V2 m, g7 s( b1 N1 {" M );
& k9 w) V3 S* u* o- c9 U( U $query = new WP_Query($args);
$ T- t1 Y- c# t' @) ^ if ($query->have_posts()) :
/ V# W4 N- v7 W4 _: ^! R6 W6 r( D while ($query->have_posts()) : $query->the_post(); ?>3 P1 z/ w/ f, P- \3 j4 z7 Z
<div class="notice">
) x! ]' p; m/ J P7 H6 I( N <h3><?php the_title(); ?></h3>
7 H; I' S, j. k: m, {$ I# U- W <div class="notice-content"><?php the_content(); ?></div>2 M3 N8 m& a$ Z+ R
</div>
0 e. J% d4 s9 F7 | D <?php endwhile;6 s7 |$ J/ k, P: D/ |' \ E
wp_reset_postdata();& M0 N& _; P- r! k7 o' ]
endif;$ {- g, f& g( w/ o/ g+ h
```8 T0 V0 e+ [% s. l& w! A
O& I, h. `6 U3 v
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|