|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ c" K+ q4 y: x2 \. }
# }" F2 _: _% @% S如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。6 ]3 a# Y; w5 ]
$ _9 r0 q7 u- E3 m8 Z8 \! T- d以下是创建自定义插件的步骤:5 }1 ?* k8 b1 M; @
& C; a) i7 |2 Z1 L% s, `" }* }1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:" [6 ]- e+ E x( e' Z0 V% u
3 R/ w0 Q: J; [8 k5 y ```2 q; @6 V. x1 m
<?php# V5 s' _0 I8 {
/*: O1 H. ?% ]% t6 e2 W/ {, b/ W* f
Plugin Name: Site Wide Notices Plugin
' r# B7 l6 I1 z* |# R4 m Description: Adds a new custom post type for site-wide notices.6 i/ j; a: P! @! E/ y2 D
Version: 1.0" C" P, N3 M$ t4 B
Author: Your Name
0 ]! G; h, [) k( a% D# m Author URI: http://example.com7 n8 b9 o, T* d+ B# E
*/
& G# \3 Q9 c& ]/ }' B( }4 e7 A$ ?1 f3 A/ f9 Q- T' y/ R
// Add plugin code here...
5 p2 a: o K, a9 V3 l) H ```0 B+ l7 b& f# W9 L" ]
* M! ~9 x& ]* w 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。0 ^7 E# C u# S4 a( J1 H6 w- F
G' n5 v! t, i9 |0 _* a/ e2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:. K0 Q6 N0 n2 u+ U4 x' o
5 ?* W+ T' V( D+ I# T, J/ D
```3 W/ a" g p7 q! \3 h
add_action('init', 'create_custom_post_type');
! D$ z6 e% h( N; b$ p" e! V function create_custom_post_type() {
) \! _! w& g1 m9 l1 _ $labels = array(8 s; Y( w- {+ x! v" Y. i! z0 f# ?
'name' => 'Site Wide Notices',
5 u4 c7 O& X! ~" C1 h- x3 P( e 'singular_name' => 'Site Wide Notice',, ^1 c# @) _( z# {8 A7 e8 I
'add_new' => 'Add New',
0 S, V ^+ `7 C; ~' q 'add_new_item' => 'Add New Site Wide Notice',4 ]" h, ~# W/ X- s5 d w2 e: P
'edit_item' => 'Edit Site Wide Notice',
9 M) B$ Y8 h$ p4 K5 o 'new_item' => 'New Site Wide Notice',: }; Q" J9 D* N6 ~* _
'view_item' => 'View Site Wide Notice',
" f; G: o' p p 'search_items' => 'Search Site Wide Notices',# X* D8 Q' A- y# r( m3 A7 L6 s
'not_found' => 'No site-wide notices found',; {- \5 W8 j# ?/ L# x8 b
'not_found_in_trash' => 'No site-wide notices found in trash'7 O( s, T2 u( A# e8 {: I& L- z! `. o) V
);
% Q4 Y4 ]( L, _; J6 ^" |" c$ H' A& Y
$args = array(; b4 ?8 _1 ?" E7 H8 M1 g
'labels' => $labels,+ o0 L( r0 Z4 s6 G4 [
'public' => true,( d3 X3 U B3 U* x
'has_archive' => true,
+ o6 O) B$ X) X2 j' x4 k; o* ? 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
) `; h5 V( j3 o. g, g5 r: [ 'taxonomies' => array('category', 'post_tag'),7 n4 W, H& V: V3 l8 K0 w# W
'menu_icon' => 'dashicons-megaphone',
+ Z! j9 m/ e/ B4 J 'menu_position' => 5,
1 s9 Y8 W( t- l5 h0 P8 I" V 'rewrite' => array('slug' => 'site-wide-notices')
% E3 r! j& E. B8 n$ ? );' R1 }+ E; c* E4 _7 v: y3 Z+ o( u
7 l! U( U+ l# V; H& _ register_post_type('site-wide-notices', $args);
; G+ j6 K# @# V, u }- Y7 I/ R. o& ~2 |" J0 j
```
. E2 h, e" I" Z0 p! G5 X" P y% h* l" F! u" z
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
+ W% U5 _! ~, Y. P* j& c
/ y1 I1 | B; f- r7 e$ r3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
% G1 f& _$ @- @% h8 Y
O' x- Y2 D- }- { c ```3 w& t; f4 P/ P
add_action('add_meta_boxes', 'add_site_wide_notices_boxes'); e! F* z5 ^! V6 p. f: ^7 D
function add_site_wide_notices_boxes() {9 R( ^2 i' k8 S& P
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
0 J( ?" r; g+ [6 a% N4 I# Z' f+ z _ }, F' ~4 e5 b3 R5 L3 n! ~0 O
% v' n- p6 ~+ Y) _4 R function notice_details_meta_box($post) {
9 `; C, n, R' H wp_nonce_field(basename(__FILE__), 'notices_nonce');
6 b2 a" Y8 P% Z4 E4 @5 b $notice_title = get_post_meta($post->ID, 'notice_title', true);
$ l( u" v& s! a- h2 D $notice_content = get_post_meta($post->ID, 'notice_content', true); S+ c0 n- v8 O; e. l" {) Y6 ^
?>* g% s! J: @. j6 t
<p>
! t$ K2 i: W k- x+ S$ G <label for="notice-title">Notice Title</label><br>
# S. b) m c0 p <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">8 p# T7 X1 ?1 D; F
</p>" f d% t; R2 }1 g
<p># J ?1 P( W7 M7 E v6 c7 N8 s( }# B
<label for="notice-content">Notice Content</label><br>
5 Y) i" m' |- y/ H! q <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
0 ]) `, c( t" P7 _. p# ?$ ~ </p>/ n3 y C0 x1 ]& l, Q
<?php9 M+ G6 i$ {# M8 E, c: X. X
}. \" G+ h% m1 l2 E
. S( {+ @3 C; v6 z, G( C) X+ Z( Y, ?) i add_action('save_post', 'save_site_wide_notice_meta_box');; D7 o; M9 Z3 v- Y) \
function save_site_wide_notice_meta_box($post_id) {: j/ o* h: \8 W3 m; ?6 R9 N% N9 c! ~
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ z% C8 t% ?9 S$ U. S6 M! T% O1 l return;
) Q- Q, r$ R" Q* ^/ w' D* K if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)0 j/ M9 X0 A: t/ u- u2 _2 J
return;
# B. X. C& F4 C. L+ e# W
5 v2 z+ X: D9 m if (isset($_POST['notice_title'])) {
^6 w- r* g- W& W* E$ Q update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
+ N1 d, v7 _+ V8 c }1 g: N! }2 b0 Q9 `4 i
if (isset($_POST['notice_content'])) {
& Z( ^6 t' M7 h# s' K7 p update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
3 I3 e0 j1 E0 G: P" {5 x) v; P }
6 q: M/ b; q, J, X5 R/ u# | }7 o6 z- P( y8 F x4 c; {4 N2 u; z
```1 p* @: a! I. q" |* S% d7 ?: k
: T+ E$ h3 c* L8 G8 {3 [ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
& i/ y* h8 E7 l/ p' ?2 b
. P0 ]/ p- y( P3 I2 o4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:5 A. B8 M6 q8 D+ J/ \, @
7 U7 r7 \7 q% S- X& Q& H8 g# e! D
```
. ~1 E: w, p$ S# w $args = array(2 S2 W' i7 S8 J n; l+ U8 f
'post_type' => 'site-wide-notices',
, t- w, H; _9 {; {& ` 'posts_per_page' => 3,
4 I$ _) ~# R' l2 Y1 M9 X 'order' => 'DESC',* a9 X! W1 u1 v# c: W
'orderby' => 'date'
5 A9 e. H. \# v$ u' f );2 h, D+ t$ G& R- a# ]6 z6 g5 C. ?
$query = new WP_Query($args);
g& r! b. `, @$ I Q+ m# \( ^ if ($query->have_posts()) :
6 m8 ]! M2 T& Q2 M( y9 T while ($query->have_posts()) : $query->the_post(); ?>% \; Q# }" j" Q" J! M7 e
<div class="notice">/ r; u' x4 q( G9 w8 |, O# `0 P; q
<h3><?php the_title(); ?></h3>
1 h; V4 {5 Y+ h" q# y2 V <div class="notice-content"><?php the_content(); ?></div>5 l& U! m. ^0 D" H% r
</div> J3 H: c- ~, ^% N8 }6 f" O
<?php endwhile;
6 J* t7 ]6 K; E& t% b$ z) G! P# G. d wp_reset_postdata();. a. M! N e; c& U, q, ~$ E
endif;
3 H6 X) w, L4 I$ d ```3 x0 F: V; k9 K
2 f5 V% ^1 p3 c
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|