|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
- _% x/ _/ [& `& s: a8 |# i1 x7 v: N6 k! G6 z+ Z1 i4 O+ R
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。8 R; Q0 B4 ?: [& o+ S
4 [) n0 G- }1 W4 d% I/ |以下是创建自定义插件的步骤:
4 I. L" I; j2 e" O' L2 q8 J; E4 z6 Z: c
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
/ E& q' c7 }7 h, v# V9 ]) Y
: k4 K& p) n& Y% m# }+ ? ```% S4 u$ H! V5 `; o2 W, h k/ i
<?php8 e. v1 d1 ~1 L- L _& j- b# R
/*
' A4 M7 C1 `! Z0 Y Plugin Name: Site Wide Notices Plugin
2 l, }' A2 }' T' |0 K, C# G3 y Description: Adds a new custom post type for site-wide notices.+ o6 G# z B z9 z& ]0 T
Version: 1.0$ m4 |) n( {* d( `8 t6 X4 Z) t
Author: Your Name
1 T, N' \: U7 ~! e8 n; Z0 z; H% W3 k Author URI: http://example.com
8 K% K$ m5 [1 d2 m */
2 v* M. ?4 m6 ^ l; F! D) p0 Z& c: a" l1 @
// Add plugin code here...# W' |4 B7 e0 A* k" Y
```" `2 o9 d% `: ^$ D
. K6 N2 _' R9 }+ Z5 `
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- Z: B3 p {$ G' M) r
1 I) \& G" t# ~4 C1 W Y2 d* O2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:! Y3 ?$ b4 V, q9 \
5 z6 t* m* M: M! ~- ?, p2 O ```
5 t9 F# E# D5 u add_action('init', 'create_custom_post_type');3 X& P9 {9 J% u# m0 x
function create_custom_post_type() {
7 \6 R7 [& S+ A $labels = array(0 {( |' y9 @* r$ P
'name' => 'Site Wide Notices',' k* }( n! w2 I# p O* p) `
'singular_name' => 'Site Wide Notice',8 K8 s: g) |0 t
'add_new' => 'Add New',; a9 b, x; l- N9 c$ x p
'add_new_item' => 'Add New Site Wide Notice',) F6 x& j5 Q% E. s4 N
'edit_item' => 'Edit Site Wide Notice',' W, u4 ~* R8 o8 m
'new_item' => 'New Site Wide Notice',: G4 w/ r8 R& ?3 E3 L& g5 p$ [- n0 m
'view_item' => 'View Site Wide Notice', l! s, y8 h2 M! F
'search_items' => 'Search Site Wide Notices',
. z* E% K3 G; b* t; t, I 'not_found' => 'No site-wide notices found',
y0 m9 H+ G7 C x( w 'not_found_in_trash' => 'No site-wide notices found in trash'
8 O# h/ a+ e4 D );/ P5 C) K& q' o4 g/ u4 w* W
8 s1 Y, i v2 x $args = array(! ]( ?( x* x8 j' G, L
'labels' => $labels,+ |( v: I4 b6 M1 V( _; {
'public' => true,
$ a1 K5 T+ M7 b6 i, i9 b( L" G 'has_archive' => true,+ w7 b9 k0 u+ c( W
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),1 N% W, J1 U9 {% }7 m. i% V
'taxonomies' => array('category', 'post_tag'),! ^2 Y$ d) r* S% f
'menu_icon' => 'dashicons-megaphone',1 @6 J7 \0 N/ O* d* T; @
'menu_position' => 5,, Q3 m( g5 T, P) E7 @1 h2 I
'rewrite' => array('slug' => 'site-wide-notices')2 B1 G1 Y5 t( ?1 o0 m
);* }2 ^! I, H# k- P/ j, d
. ^' _ \6 Z2 L' e register_post_type('site-wide-notices', $args);
! N g( x/ ^$ i4 n" u9 ^. v }
. f% h7 P7 @$ |9 F+ {7 w# D: A ```
- N; y% B8 t4 k' m! z' I7 Y5 ]& d V% v4 u
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
- L) f8 A m) ^, o* R3 e0 H+ g$ \3 W& z0 c7 {3 s2 ]
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
9 V& I a( G4 B; W# q
; F7 l, v, t* D" T ```& R; j# D' P. m; D, F* V. _
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');8 W- M3 a2 F" j% m/ c1 ^
function add_site_wide_notices_boxes() {3 v7 |7 R. G0 }+ [9 W+ B
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
; u7 @1 I* q5 B( v1 y/ z }) N0 Z; h2 {5 j. d7 T6 U6 o& t
! _4 Z2 N0 P3 U, M9 h- ~; O% p/ @
function notice_details_meta_box($post) {+ |% n' [5 N1 @, D
wp_nonce_field(basename(__FILE__), 'notices_nonce'); X/ i! P+ }4 H3 k2 z0 J9 W1 b1 H
$notice_title = get_post_meta($post->ID, 'notice_title', true);
0 r! `& l! u2 l& E% J) l; ] $notice_content = get_post_meta($post->ID, 'notice_content', true);1 W; M) x, v3 |3 V' s5 h A
?>. w' W; N- ]8 q( V
<p>/ O+ N5 P+ d" _9 h
<label for="notice-title">Notice Title</label><br>1 m9 G3 _) u$ M& l/ p
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
]6 |& ` G# a. n </p>
8 i) Z% A, U+ W( R) e+ k( ? <p>
) s) i E0 u$ W! _ <label for="notice-content">Notice Content</label><br>
7 Y& p7 {* G5 ?* V) y <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
- V' ~; t" h4 A( V L </p>0 j. y( i/ b6 N" Q; Q# |. L% D7 s6 N$ y
<?php
2 H H4 P3 u5 q" H2 E o }: l* c* C* W% U9 K7 ?, A
5 o: i- }( u5 H% _3 g8 O: y add_action('save_post', 'save_site_wide_notice_meta_box');
7 W8 c9 \# X( m7 Y function save_site_wide_notice_meta_box($post_id) {8 n- n- {3 l) ^: P2 y
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))7 @$ r/ T) X0 }0 s; {
return;
- k# L& w3 ?. F1 ~& i+ B if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) W+ G+ z" ?1 F$ i( l
return;9 o ]9 ]3 \* J+ y: i
U& h0 \. q+ t& e( n. d9 d if (isset($_POST['notice_title'])) {0 O1 c- w7 x7 H2 c' \ N- M
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title'])); I5 S( F% w6 K+ c( `# ], R
}
6 e# b' a- L5 y if (isset($_POST['notice_content'])) {0 a4 T5 F$ m \0 N+ ?1 G+ n" A! S. I
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
: F- N2 t" Q6 w1 V }' A7 S/ H% ?! r5 t* y( ?9 y& t4 P
}
: g1 C4 d1 o& L. m ``` i" s& \. ?0 B- Y7 ]7 v9 J
! V& ]9 e! C& L, j
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
% `/ T. j, \' z4 `* a
& K( N" P0 C) O4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:' C4 J$ s; G+ t6 J
; ~6 X8 |* F3 Y7 N) e- V ```
* S# D/ J, x+ Q2 K( o* K $args = array(6 n- a6 G* w* l9 p; k" ]! z
'post_type' => 'site-wide-notices',7 ^5 Q) [& Q" O0 H
'posts_per_page' => 3,+ q: D3 ~6 V* o2 |& z& [2 x
'order' => 'DESC',0 y/ |3 O3 V% H" |9 q! D
'orderby' => 'date'
! n/ I- z' g0 R* f- v: w* M0 a );
3 j1 U$ k+ _& t* X' U6 B $query = new WP_Query($args);
: e$ q7 A8 Q0 } q8 ^) f if ($query->have_posts()) :
0 Y* W7 k/ v, V9 b/ H& H while ($query->have_posts()) : $query->the_post(); ?>- j. ~% U0 A$ N1 T
<div class="notice">$ s, h/ @" L- [: ?% K9 w; Q
<h3><?php the_title(); ?></h3>6 ]$ E( p: k4 |) L+ O" ?
<div class="notice-content"><?php the_content(); ?></div>
- D/ \1 z* r* N1 i# x </div>
! h, Q% Q8 R% P& w2 y8 s/ i <?php endwhile;
& D* t& K, z7 w9 d6 S3 ]" z wp_reset_postdata();
1 E: \' s0 @; b endif;3 Q! g( s& g6 h) |% u# I+ N+ p- k2 |
```
. L( n3 r6 v5 H3 u$ @( q
: G7 |, t2 y* e3 Q 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|