|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
5 l$ }5 h+ i1 V) u1 v7 w2 }" p) t% t: h5 t
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# ^1 r/ @; |6 o- S: b5 ]. R
4 }% D/ e8 O$ i; P
以下是创建自定义插件的步骤:
' D, x e1 ?& x3 J# |
5 ~$ p, s; |" X% A' h2 ~; B1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, w( C: h- z7 F! f( m9 u# C6 P3 Z
/ e/ g1 X5 I7 Y0 d ```( Z9 n$ Z- `8 G7 z: K
<?php
+ N( }4 D0 k7 k3 S( Q) f1 `& T$ X) d /*
# q" y6 D* Q6 m9 h Plugin Name: Site Wide Notices Plugin
2 f# ^9 q3 z$ W Description: Adds a new custom post type for site-wide notices. i/ `. ~ n$ h! R: p# a* I
Version: 1.0
0 ~+ t. D/ o: p+ F2 N8 M0 x Author: Your Name. T. g8 J- ~- X* x! u7 K, z4 {
Author URI: http://example.com- k: b" I8 T9 d2 k
*/
' a- `. F, k5 D; H0 ^3 [8 {
9 f5 d1 O' v0 `1 r% n+ k9 P, _6 E // Add plugin code here...7 l2 Z( i9 c! ~. [% P C
```- V- a) G# q! b
( W }0 w7 s6 k& [; s% R4 S 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
% X- j5 c4 h* _3 P
: W! H5 P* t5 r2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
( {! N+ Q" p( d6 M, h6 C
3 w0 H5 i! U( f r x ```
% c: d% X! E3 c. Q% r0 o. V! g add_action('init', 'create_custom_post_type');2 J6 K6 j0 T' ^6 O% L' ]- [
function create_custom_post_type() {/ A) ^7 r# ]1 t; P" @
$labels = array(
$ X( u' n& e4 L& r% x) C% a 'name' => 'Site Wide Notices',- d- g5 y' t: D6 i: F B; I- n; t
'singular_name' => 'Site Wide Notice',
# h. u0 q- H! u0 @5 R1 l 'add_new' => 'Add New',
% t+ D p: S* m+ }, I$ \! y 'add_new_item' => 'Add New Site Wide Notice',
, c3 s7 j S1 U2 g! V 'edit_item' => 'Edit Site Wide Notice',
4 `( u0 Q. u1 l 'new_item' => 'New Site Wide Notice',. q8 W$ ]) L+ n0 V1 E. [
'view_item' => 'View Site Wide Notice',
* `$ d5 J' J$ l/ ` 'search_items' => 'Search Site Wide Notices'," _7 b$ A2 U1 O4 A, L. n
'not_found' => 'No site-wide notices found',7 F9 L( z- h6 i$ z- U* \+ R
'not_found_in_trash' => 'No site-wide notices found in trash'& |2 N0 a4 p( j% D$ u$ }
);& j5 ^$ {# m }
- J% J2 [! |& d" H8 q. Z
$args = array(
% a8 \& i5 K: Q* d6 [ 'labels' => $labels,
4 j+ g5 E3 Z2 ^) H; P! G1 g. k/ x- o" m 'public' => true,
2 P. |) Y: A6 }! Y 'has_archive' => true,+ G$ Q. q; U6 X, n2 O/ x) `
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
' Q6 k4 Z. d" ?* C! [ 'taxonomies' => array('category', 'post_tag'),
, B: J0 f9 h, S& I 'menu_icon' => 'dashicons-megaphone',
$ i8 z9 ?3 B: T 'menu_position' => 5,4 E/ ]& S* J" {
'rewrite' => array('slug' => 'site-wide-notices')
# g: _" }/ j f' v8 G6 l );
5 V% D* i$ b" \" W& e4 w O% q' {' v" H5 T
register_post_type('site-wide-notices', $args);- H8 V& `+ L5 ?, k2 N" h. {: m. D* P
}
) K" g R! n- \1 G6 {# e3 y ```
z+ ? W! x5 W8 N* ]4 T& M4 j
$ q4 b7 J$ N% Z6 _ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
% v# i$ U! p9 j+ F6 M
+ d V+ X7 l3 |8 T) @% s3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
1 }7 S, V2 ^9 |. R5 |) f l& D7 G: u7 [
```
4 l4 ?7 S9 W$ C6 @ Y add_action('add_meta_boxes', 'add_site_wide_notices_boxes');3 z V7 p3 S/ c8 p/ _$ ?
function add_site_wide_notices_boxes() {
" N( |( e g2 q, l7 T6 C6 U add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');2 {: W/ ~$ X) N8 H8 D8 U) ?! ?. K
}! i4 V! {1 M1 @# T- h0 S" g
N6 m" u5 L9 y0 u) y function notice_details_meta_box($post) {0 b! r9 @ |5 ^/ y$ J; e
wp_nonce_field(basename(__FILE__), 'notices_nonce');
6 r7 s5 S% u) p4 U4 K. b" l% Q $notice_title = get_post_meta($post->ID, 'notice_title', true);# b3 \7 W! M3 F$ [. t T
$notice_content = get_post_meta($post->ID, 'notice_content', true);
% Z! t4 @; F0 V6 |/ T, _. R& G$ m0 u ?>( R$ }+ e$ O# }0 X
<p>
) v6 Y8 \, w+ o, B$ [! u <label for="notice-title">Notice Title</label><br>/ b+ h& K( k. K6 m9 \
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
- H$ L4 X+ j1 ]' ?5 Z </p>
/ u, g- ~* a4 } <p>
4 g- g; a7 h/ z1 b( q: ` <label for="notice-content">Notice Content</label><br>
1 ~9 g7 ]: x. A) s9 g <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>4 @5 }# R. \' p- u
</p>3 h8 b s: t0 @/ {1 q2 I
<?php; M( e* }: | z9 n) S
}
* J% X8 N8 c. |$ f4 q1 Z! Q; ^' K; e7 W B: n+ ?8 Z; o) K3 V
add_action('save_post', 'save_site_wide_notice_meta_box');
* [4 J6 `4 x. s; g function save_site_wide_notice_meta_box($post_id) {
+ {) I4 g5 Z; ] if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
% W8 h* K; p5 f$ `* _ return;
& t- L; Q/ }$ Y, C9 I) C/ Y. ^ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
4 Y( v6 r" I4 [; ^3 L return;; [. l% [3 W. p N8 ^) R$ _ \2 a
5 n' m- t- X7 l0 B if (isset($_POST['notice_title'])) {! b) W8 p; T3 C6 x: ]
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
; ~. H: c+ x& v; N/ I }
: D% L& _5 O) `. \6 Y$ ]8 c if (isset($_POST['notice_content'])) {
$ ?& Q( O0 K4 \% Q: Y3 n update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));3 h8 } N: U2 {6 C
}
- H* ?, p( b9 s- h" } }
1 x$ G$ s9 J' z, s1 p! c: b1 l ```- A# l) R) `; V, j0 b
0 ^9 C+ C' \ i6 j- K. J! e! b 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。8 n* Y8 l( Q/ e0 {# f
4 P# q6 d0 J+ {1 N; \
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 N' a- N; d' n( {) Y
7 i! h3 w: K; S' Q2 L. b
```
& z, |! S+ @' M4 R6 ` $args = array(
/ h) r7 `2 @: z6 ? G) }5 D, ? 'post_type' => 'site-wide-notices',
$ G6 G6 Z/ n' z/ L& [ 'posts_per_page' => 3,
' h2 t/ e4 V9 h. j( Y* k: J* b 'order' => 'DESC',
# w v3 U# N- p, C; a: A 'orderby' => 'date'" F" g2 r4 Q+ h% Q1 V( ^
);
3 }& h. P% N" e1 ~8 {" ]6 L t $query = new WP_Query($args);4 i; e; [, R' h3 Y* a
if ($query->have_posts()) :( V8 B7 \5 {: G( p
while ($query->have_posts()) : $query->the_post(); ?>0 r5 A2 R8 Z" h4 d
<div class="notice">9 ^2 b/ Z* D; `' D5 ]; ]* @/ [ A: w; e6 o
<h3><?php the_title(); ?></h3>
; H8 p8 d, p$ ?- V7 | <div class="notice-content"><?php the_content(); ?></div>
. ]1 d4 t' }: d+ g% ^9 u </div>2 s. _2 k. W! n* c; S" z
<?php endwhile;! p* o" O- B% t, _6 ]2 Z- W' q
wp_reset_postdata();
$ G ]* m `- G endif;4 W7 d# n$ ^( s( {+ F
```; o7 V4 D6 f; J
- D! Q7 Q, C8 H$ `5 P
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|