|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?5 H, u# O, t/ B: `
0 I. c. k! F7 K6 K如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; w$ A, @: Z' g# }8 N
' x+ X" |/ M( S) C+ V/ r
以下是创建自定义插件的步骤:
g X; _9 \% o% ?% ^* b* D9 t) ?- T( d* L) S6 g
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:1 ^, A4 y7 Q- g g* s a% [0 W9 `
( f# @4 ~8 c) m
```
8 _5 x' K8 |3 U# {3 w( V <?php- J; Q& R" C9 x2 l% j
/*" H C( ?: N0 [. _% j* i
Plugin Name: Site Wide Notices Plugin
2 ^, Y5 H/ l+ p/ w1 i3 ~! D Description: Adds a new custom post type for site-wide notices.
6 R* c3 | j# Z$ v Version: 1.0
% P/ _) J% Z; O1 V+ \7 V Author: Your Name# i: l5 U: J# P5 e- {2 H
Author URI: http://example.com8 x5 p! H+ u% S5 @
*/1 f. K; l* P5 A5 j
& |4 ]1 o3 O& f+ J. O
// Add plugin code here...+ I9 l1 p0 } d3 B
```+ H: `$ M$ e# x% k
& E' z7 j& r/ N+ ^8 _ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。& ~ ~% w% A4 L* h! z. l
3 M, \) a. p8 G Y4 n7 |
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:7 a) e3 V* x8 U! T4 h8 N/ H
$ N" z' S% r/ k: L1 E M/ L ```
7 }/ M3 h: T- s; C add_action('init', 'create_custom_post_type');$ h7 l8 g6 @* Y. b
function create_custom_post_type() {
; B2 {6 y6 `2 m( l- _, P4 ~ $labels = array(7 }: Q( T) o1 ?
'name' => 'Site Wide Notices',2 e* K7 R' W( v+ i0 R4 H5 }- r" b
'singular_name' => 'Site Wide Notice',
5 \8 ?3 G% {7 v" U 'add_new' => 'Add New',
5 {, |9 G# `+ q, p; c 'add_new_item' => 'Add New Site Wide Notice',+ }- o) J$ G3 F, K( B' M
'edit_item' => 'Edit Site Wide Notice',' s# E8 r5 |$ o
'new_item' => 'New Site Wide Notice',
* p+ T0 u7 r7 O' C3 o. `( u* I 'view_item' => 'View Site Wide Notice',
" v& x% M7 N9 k6 O8 q 'search_items' => 'Search Site Wide Notices',
" _. _( T' ]( g% H9 d. C 'not_found' => 'No site-wide notices found',
$ u+ t, g% ~: _+ P o* U% j- _/ p 'not_found_in_trash' => 'No site-wide notices found in trash'
& e3 H7 G$ C" \# B );
) z: [' U. p7 m. ^7 Y) L* v/ ?2 ]1 f# j; H3 O
$args = array(
' c, |: j! u" d 'labels' => $labels,
) h$ n$ X. T, ~6 \ 'public' => true,
6 `# g& R6 }1 e& Y9 A2 E7 p6 o6 X 'has_archive' => true,' k. p% m, l) s, R1 H9 c! ?: L
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( L* l' ~" v1 Z7 I6 W3 W' c# u
'taxonomies' => array('category', 'post_tag'),: _* A. E* W# }2 a! ~: O! D
'menu_icon' => 'dashicons-megaphone',
- \5 f& k' N- [& B7 w 'menu_position' => 5,
' Z. R5 ^9 v1 ^4 r+ V8 ^& B2 w* Z 'rewrite' => array('slug' => 'site-wide-notices')- U; U* |9 ~2 g: I }# I
);0 P1 G8 E3 d y u# q0 V6 W: ^+ y
/ v) i, Z1 B" |/ _/ p register_post_type('site-wide-notices', $args);
" K' @' D, J4 O. K }8 M; U4 g2 v7 _! ] c1 K
```# h, } i9 Q* {$ r# ?! E+ c" s9 V
5 E2 l! x+ w$ ` X7 T
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。% ~" O. q: d2 A, L# S, y6 o
8 z5 r; l) I9 a
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:' I6 ]9 D! w7 a
% G3 }, l/ M3 c4 L+ P1 W( a ```* w4 L2 ~' w+ I* t; [( g
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');! t, l" t+ q" w ^' j) }. g# u4 h
function add_site_wide_notices_boxes() {
1 Y D, G4 ?" f add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
# K+ U+ \0 f9 V9 N# G4 `7 v5 h5 N }+ }1 V, E* [- |* P
1 \: A0 X8 W' J/ x& t6 r; ^
function notice_details_meta_box($post) {
) m8 b3 d( r, R4 J wp_nonce_field(basename(__FILE__), 'notices_nonce');
' _" ]* M- c7 Q% f2 v* s6 W$ a& d# } $notice_title = get_post_meta($post->ID, 'notice_title', true);+ R6 f' b) V' @ o( c2 _
$notice_content = get_post_meta($post->ID, 'notice_content', true);
( T6 v. ]. y* F3 F0 a9 y* u' @8 x ?># G: e; B8 a5 k. ^- _' b0 @
<p>" i' t4 M' ]8 T( `% I, H
<label for="notice-title">Notice Title</label><br>
# h9 z R$ u4 B# N$ e6 \8 U <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
9 F$ [2 K3 W) b( E; n* e </p>
5 M- M7 k+ e0 p2 J) ] <p>
; q4 Z( K* J7 n+ Z* Z& D2 h. R <label for="notice-content">Notice Content</label><br>
3 c( Y* W- C3 e <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
7 P$ h5 [ ?0 ? </p>) u& J" j; \ h
<?php- d5 Y! E7 a$ P3 d! ?( ~% B) w
}
0 q( I6 }$ w7 z7 F( s7 E X! c: E; _; G% m6 V' A1 s ^4 v
add_action('save_post', 'save_site_wide_notice_meta_box');
. R7 {8 ^" L; N- d" C function save_site_wide_notice_meta_box($post_id) {
- K6 W( x4 p- ^7 q1 g- [ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ c0 [, A& w6 i. F* c, }3 R3 q return;
/ O# ^9 u7 ~' n% k& T if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% y4 E7 i3 ^& w2 F) q
return;
6 {! }: C# g% y, v* o+ F7 u5 T
9 j i; h/ ~5 B if (isset($_POST['notice_title'])) {
$ T. `% x9 \9 @! S, s6 p/ m update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));8 Q1 i; Q" \4 K! W! j; P# e1 Q
}$ S8 n' a" k( a y
if (isset($_POST['notice_content'])) {; ~& D! c1 w; {. z
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));) o. h h; G9 p U7 V
}8 H9 L# R+ P# P; C L
}
0 f+ N4 C% o; E! p ```! B* O2 N4 i8 S5 \. p8 S) ~" M
/ _8 l3 d6 W+ y& Z/ z# P( ]
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。0 u( s7 e( P; e
7 }9 \/ h( ]) M: F
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:: v- E1 y8 i# a# H$ D& \2 V0 X. u
( m5 C) H, @0 ~* G. p+ S ```' }4 }3 d# n+ J# P7 t+ d
$args = array(
) F; w* a6 c7 j3 ~& |" N3 J 'post_type' => 'site-wide-notices',
7 i0 b$ g8 G0 I% {& [, }! ~ 'posts_per_page' => 3,( ~. z; u4 J, r
'order' => 'DESC',8 i: j, c4 |. W/ U! c i u
'orderby' => 'date'
6 F/ G$ a0 p5 V4 _1 y4 M) ? );, C5 c- _3 e5 U5 C
$query = new WP_Query($args);; b# y8 u+ F+ x G$ L/ e7 d2 m4 _
if ($query->have_posts()) :' B/ A W/ `7 c: |! L
while ($query->have_posts()) : $query->the_post(); ?>' B8 d/ z3 R' \4 J" t& E" g
<div class="notice">( a! V# A) o; e0 s+ `
<h3><?php the_title(); ?></h3>& o: ~+ b# |8 x2 E
<div class="notice-content"><?php the_content(); ?></div>. K4 W* |. J9 s+ p8 S3 b
</div>
. t8 G) R: e0 V4 y. p <?php endwhile;6 H5 t. K, G6 V# ]$ L* O
wp_reset_postdata();
8 R7 ^$ `7 f1 Y+ a5 e, @8 d# J% Q endif;
# K& F7 e2 V) |: |$ h- R1 R ```
8 J* @$ \8 y, i( d# j
/ i0 U& b; y v4 E1 }, n* |0 v 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|