|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
: @3 k; m8 t! V. S6 u
" \6 v3 T$ P. X如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。 o0 ^9 B' s9 y8 N0 h
, A! q- b% R& R0 `以下是创建自定义插件的步骤:2 T$ ?3 W* s) f& [+ x5 R u
0 i: W/ u: E$ q+ Q* t/ }0 |7 i3 Z/ C
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:/ ^& f( G! t) o. m1 T
1 G0 P% a# \/ { ```. b# F2 ]1 h. r& [
<?php
5 {$ D' Q) i6 p! E; U /*% z6 x8 k( u4 M5 J' b
Plugin Name: Site Wide Notices Plugin
- n# `3 @8 s. ~5 _0 S Description: Adds a new custom post type for site-wide notices.( U `9 ^- y7 j7 h* p; x/ h( `* Y
Version: 1.0* a8 |7 P- L4 O% d, I3 S) _
Author: Your Name5 K. }$ X1 K# G' s; }7 o4 c. e
Author URI: http://example.com" R5 k4 T c; }, r5 I7 \
*/' n# s( o# u* F9 N% d& ~- ?
& H; M5 D* z+ [( M( i+ x
// Add plugin code here...
+ p6 K8 {9 [; {4 [ M$ v ```
6 Q; V! n% c& p5 _5 D. X. O( x3 V
) _1 i1 ?: I! r0 n! | 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
4 ?$ Y: w+ s9 G0 a2 S. Y
N8 u3 ]; H# l6 |2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:/ H2 k6 ^7 I5 g: U* C7 N. J
4 i$ ], i! s# w7 t ``` \5 m3 q! f/ P* ~) y$ V. [) n
add_action('init', 'create_custom_post_type');% w- Y& k) @4 h& A" O! ~$ c0 D3 W
function create_custom_post_type() {
0 @) W$ O% R5 n8 F9 k2 H% U, Z $labels = array(
8 m& N# A G" Q2 O# g9 k0 d" k8 c7 p 'name' => 'Site Wide Notices',
* \7 t/ E( T8 }2 y% U% b( p 'singular_name' => 'Site Wide Notice',
+ f* ~; Q: Y/ W 'add_new' => 'Add New',' r8 d: P& M, {
'add_new_item' => 'Add New Site Wide Notice',3 B& v) U$ T- W: u. ]+ E4 L+ Z8 S. j
'edit_item' => 'Edit Site Wide Notice',
2 ?5 j# L# \, \0 o7 @. e6 k7 [ 'new_item' => 'New Site Wide Notice',( Z( w5 b$ B5 u. y/ h+ W% b
'view_item' => 'View Site Wide Notice',
$ S c/ }7 ]2 l+ E+ G" Y1 X 'search_items' => 'Search Site Wide Notices',
; R* G1 |' ]7 Y' B) Z/ h4 R 'not_found' => 'No site-wide notices found',
: Y; V9 [- c# b3 d 'not_found_in_trash' => 'No site-wide notices found in trash'
/ |# q6 \5 V! U# F );. J/ U& x* h! r0 H2 j& U; s
- p. c/ Y, i4 G D" {+ f* ]! u
$args = array(
$ s! o. u6 |, o4 e! c 'labels' => $labels,
+ b4 n2 z, L s2 P: r0 X* ^ 'public' => true,
i9 w y% g& R9 @5 ] 'has_archive' => true,0 V7 H+ U5 ~/ B2 q( \
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),0 ]' D3 Q& I& t% \9 g
'taxonomies' => array('category', 'post_tag'),
7 [' m* d$ {* Y, r4 f6 j4 j7 _ 'menu_icon' => 'dashicons-megaphone',
( Y% F: g! B; } 'menu_position' => 5,0 @, C/ i9 A! @( ^4 r$ P
'rewrite' => array('slug' => 'site-wide-notices')- Y0 Q+ w& Q _, I/ h: j
);7 Q3 b/ \' J" y0 S* s: w" I; T9 I
4 |) w- k8 Y$ F" H* O$ O/ n1 E register_post_type('site-wide-notices', $args);# L; Y% e6 }8 J8 e0 K
}3 @0 @$ ]6 b7 q5 i+ I' B: }8 I1 j& a
```2 _( i: _# U8 w
( ], j5 C' L( ~) C" b
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。0 f6 {# W/ ?% G1 N0 K; S
. F7 g8 b9 ^$ V5 E' W1 S: ^
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:; _ X6 L# o, } K
6 b! I# Y7 c3 Z I+ J4 V2 @ ```* o$ f5 N1 H, ], _# @8 B4 B) P
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');4 l* k7 @, T7 s0 X) u; R* o
function add_site_wide_notices_boxes() {0 h2 M( B0 o3 T: B6 M+ A: O8 w
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
+ d9 ]0 |& V/ G }/ e" m1 u1 v- A" v
; K. \. h) }3 Y5 P. t9 N2 P function notice_details_meta_box($post) {; V! O% Y( ]9 Z3 J4 Z/ n. g
wp_nonce_field(basename(__FILE__), 'notices_nonce');1 d5 m8 N: a0 {1 g) D
$notice_title = get_post_meta($post->ID, 'notice_title', true);
* c& o/ I- @( @5 q, Y $notice_content = get_post_meta($post->ID, 'notice_content', true);6 T9 C- N2 @$ _
?>
; H. s; E1 @: Z* ? <p>1 j) K. |' d: s
<label for="notice-title">Notice Title</label><br>9 ~- f0 r5 P4 ^7 v4 m
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">9 ^: F2 d' r. h& x" u+ g
</p>' s. O# b! Y6 B0 ^* }9 S
<p>9 k: h% ]8 D; R; \" ?
<label for="notice-content">Notice Content</label><br>8 L/ o: h% t3 l2 ~
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
, I8 E0 @" ?, M </p>9 u C7 C; f6 ^2 s! z& W
<?php
+ X5 ^# l$ j8 u }
5 C# P/ ]+ i- H+ h9 h+ S9 M
1 _ z9 J4 @0 {: V; K) D& y) z add_action('save_post', 'save_site_wide_notice_meta_box');* V+ s2 S) f# m1 R, Z8 r) e, r
function save_site_wide_notice_meta_box($post_id) {$ B8 ` \7 ^" h
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
* P3 P; m. F4 U- A return;
4 h0 @( X9 o+ i/ w& @ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)9 T, [- e( d( I5 `% Z/ f6 F% ^* O3 G
return;3 t! f% k: d) Y9 ]7 R+ U
, [2 D3 t: A8 D6 s y, B& a
if (isset($_POST['notice_title'])) {# D+ E# c- f, b
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));; d9 Q9 q- g( g& r8 S
}0 V7 N4 q J, ]0 x; x; G! F6 X
if (isset($_POST['notice_content'])) {4 _# P r9 k* \, O- g
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
8 R3 m6 {1 @; {3 q1 j& l3 w }
e! O% B+ }. i1 a% t }# h5 u7 P, I( z9 p: e( ?
```
6 ?' z# f! W4 p; L- C3 e1 T& P5 l, [0 M
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。; \3 W5 ? A+ L7 v& C6 V
2 K% c' D1 s8 l! d8 H4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:6 v3 H- ]- u$ D( w- c
' z1 R- r$ ^& X' h7 Y, Z
```/ D! {/ i) ~3 g5 }- j7 f
$args = array(
6 V9 S2 K# S# |, C5 L+ K, B 'post_type' => 'site-wide-notices',. f' n' w+ ^" F. k3 @; ~2 ?
'posts_per_page' => 3,3 z0 q3 B& c/ e3 L1 Y& s
'order' => 'DESC',4 \* S7 k* P9 z1 e
'orderby' => 'date'! w2 i% p: t/ d( f# O( N
);& y- l! l5 H& \: m; f2 ?4 u
$query = new WP_Query($args);2 R! H0 P* c+ V4 b# n4 ]3 J( w3 j
if ($query->have_posts()) :0 L* L2 G2 ], C4 I: }2 I* C0 G
while ($query->have_posts()) : $query->the_post(); ?>
) o8 S# n7 x) k' { <div class="notice">
8 g8 |( m' K, y! j. c1 S <h3><?php the_title(); ?></h3>$ k4 E4 [& o( [' \% `; j. e& D
<div class="notice-content"><?php the_content(); ?></div>
1 D5 Y! q+ f, p; ] </div>
6 w5 y7 j, Y% j/ [9 P; b+ \+ S <?php endwhile;; w& I$ F6 `+ |: ]
wp_reset_postdata();
9 C" x. d& _8 }+ o, \( _/ i5 _) g7 H endif;* b* |' y. g$ ?5 m
```
7 b2 h' X% ~; [$ h3 Y: ]7 X
) ]& r) T, t" s/ N 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|