|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
! c) l' m+ \7 L4 j, W' D! h' x0 G) i7 g
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; m5 W! y8 Y& L/ @
U1 k- n# W4 H' \9 R- r2 z
以下是创建自定义插件的步骤:0 J& Q8 \9 U3 C+ p9 s
! j% \( v4 H$ P' G( j0 ?" p1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
4 m+ M* I% P4 k. f3 K. B( [
9 ^7 L# {! S) B& P ```
5 G% u" |2 P5 r0 N' | z& |7 T! E <?php r D+ s3 r4 z; O: F/ q
/*
7 d, i1 P* b. Q% h2 p0 i# ]* v Plugin Name: Site Wide Notices Plugin
8 i8 H$ u$ _6 B* h4 H- ] Description: Adds a new custom post type for site-wide notices.
! z1 }6 \. E5 ^5 t2 k- R Version: 1.0; e; b0 E# \0 V9 {2 q. _
Author: Your Name$ U2 X ]- X* R9 L. g8 n1 O, \8 X
Author URI: http://example.com
/ i& O' W' @9 ]; _: e */+ l. F) o. w$ v# t: M; i( Q6 F
* y3 U. l4 @( ^- X' W // Add plugin code here...4 M" ]+ f$ a. Z9 C. |+ u6 P
```
# r8 x& e; P, ?3 u9 q5 z& D0 i4 c% ?* h- o& } j0 P6 y5 k. C2 t
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- f5 |% V; C( L+ E8 m+ \6 Y
9 Q: N- R8 A7 I, U
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:" x# r" z( j Q% F
/ y2 Z7 T8 S( \6 d4 m4 W( q; r
```% D8 G. e# l4 ^& \6 ~
add_action('init', 'create_custom_post_type');
) T, c2 y/ A' k' {4 { function create_custom_post_type() {1 p* Y. S4 a: W7 k. ~ m
$labels = array(
; F3 R7 w* @" _2 F2 G7 Q 'name' => 'Site Wide Notices',- P7 P% z5 @$ t( N
'singular_name' => 'Site Wide Notice',# y9 V+ T7 u2 W/ ?0 x- l+ P
'add_new' => 'Add New',# u) Y/ I' Q. Z
'add_new_item' => 'Add New Site Wide Notice', P& Q* r: X! O( R, k
'edit_item' => 'Edit Site Wide Notice',
7 a) H( ^1 i- u0 d4 @7 M 'new_item' => 'New Site Wide Notice',& F. F9 L" ~7 [! |6 l+ Y
'view_item' => 'View Site Wide Notice',
; e( C4 @* @) L0 {6 } 'search_items' => 'Search Site Wide Notices',) i5 v& }5 ?2 F+ k% V3 ^( \
'not_found' => 'No site-wide notices found',
, F/ [: r; m: P" i0 M+ `9 S 'not_found_in_trash' => 'No site-wide notices found in trash'
8 P. w1 I8 \# K4 K5 s% I$ ]) d );- M3 Q* O' w: o; g! n8 N
! E5 ]8 q4 A+ r+ z
$args = array(; X: F) \2 p w0 z$ [; T1 B
'labels' => $labels,! _$ f% A7 b7 l! n0 j0 I
'public' => true,
: C- o5 C( W; \7 B( j 'has_archive' => true,
% |9 Y( Z# ]! @4 } 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),9 f% n3 `7 v1 }, x+ {1 `" g t
'taxonomies' => array('category', 'post_tag'),: }: f: d2 D" b+ ?1 c ~9 B
'menu_icon' => 'dashicons-megaphone',/ [% G1 Z' D5 H I! f5 t
'menu_position' => 5,
9 h/ a* ]- V5 W7 c 'rewrite' => array('slug' => 'site-wide-notices')
0 Y$ I) I3 U0 `6 G; A8 h );% Q9 Q) V/ ^8 z# v! i8 Y
: [0 e6 j" N+ z
register_post_type('site-wide-notices', $args);
% S2 r$ N+ s! `1 x8 i0 D V0 d% t" p }
E; }: z* k4 Q8 j ```& ~2 s/ F% R/ i& z2 w$ M
9 \* F- i7 A6 S
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。 b3 M3 w0 D: o& l. V4 Z G
! O7 `0 W! ? S9 J/ t7 V s
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:7 C8 Q9 u7 H7 A; ~7 L
5 ?1 j) \5 _/ M ```
* d' I. e& u0 |! T: h add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
, r0 V+ V8 ]# Q! k9 _0 b0 j; g function add_site_wide_notices_boxes() {
/ j# U6 Z) Q! T7 L7 Q/ r+ K add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');( D1 q2 [5 f1 M8 |' v: p, q% e9 t1 x
}. ?% u7 ]- S1 L. A5 p# ]
' Y$ [* ^/ L) }
function notice_details_meta_box($post) {! z, v G c" F6 H' F5 j
wp_nonce_field(basename(__FILE__), 'notices_nonce');
O- o- y3 O- h1 n+ i3 v& x- e' S1 \: n $notice_title = get_post_meta($post->ID, 'notice_title', true);
# |3 d: `- |1 c $notice_content = get_post_meta($post->ID, 'notice_content', true);
, F$ |4 Y/ I0 v9 C4 ^+ X* x4 z ?>& z5 T6 X. _8 a! U+ q/ G! c
<p>1 S$ X4 b5 X; f, c
<label for="notice-title">Notice Title</label><br>
. _5 ]1 Z( x3 d0 R2 @ W1 [( B <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">( s$ F, X% L" A# ^
</p>2 c. u( V* j- b) L
<p>" J0 n( z* w3 e& r
<label for="notice-content">Notice Content</label><br>+ Q3 V1 n/ d5 z; s
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>9 Z' ^! a" H A, y# _
</p>
8 ]* m/ F, X3 l# g3 _) r <?php
8 O% S, u! J5 q2 b+ G }
; o% l9 W: b5 z2 X& ~: r/ A7 F/ K+ G) }" R1 G6 @, E( z
add_action('save_post', 'save_site_wide_notice_meta_box');1 f. N6 s2 H4 B5 J5 b/ G# N& H
function save_site_wide_notice_meta_box($post_id) {, g5 ?: N4 k/ b A+ X
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))% m3 i6 d4 Y [' j0 H: p
return;( O9 z T8 ^/ `3 i
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
; j/ _* l7 x8 f. [ return;# t5 ?; m4 K: o( ^, |
% f1 x$ M5 N. S) A if (isset($_POST['notice_title'])) {1 n, @1 D* \! ]# O& K
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));1 p+ N/ p4 Y) v( }
}* R* ]% d# t4 Z7 ]8 s
if (isset($_POST['notice_content'])) {
7 x6 F# [) M% j7 A update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& m3 X: L% U+ X- N8 q8 u- ^2 S }9 Y6 ~' A6 _/ a3 O. ^! T
}
) X7 w: Q( r- l% L; a ```
" {1 B6 T8 j1 f# V, y& z8 v$ G! D/ ^/ o3 J; Z o% j$ _
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。: m2 ~! ]4 B. [6 n) ~
2 |. i. a0 ~5 z8 X1 {1 n3 z
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:" T; U. p `8 H# h/ @
! f+ i, u' }- t5 f5 c* ^ ```9 k, {( q; u# b) I: B
$args = array(
% y- E, C, S" Q! r' z 'post_type' => 'site-wide-notices',$ L/ ]8 e7 x8 B. ?% d Z
'posts_per_page' => 3,
, E8 r. i. n" N; y" M 'order' => 'DESC',
& Y6 _: l7 y8 r4 M1 |( r* o+ j: B 'orderby' => 'date'
; {/ s5 C2 A- V# }* V5 X2 Q' N );. d) o. P9 `: Q' ? k# u1 |
$query = new WP_Query($args);
% O9 E4 \8 B8 K/ E. l+ C+ h9 j2 ` if ($query->have_posts()) :+ f+ A$ y: k1 N2 d2 I7 S0 P9 ~7 i
while ($query->have_posts()) : $query->the_post(); ?>8 Y5 }6 u6 i1 l$ y" k2 ~
<div class="notice">
6 _: H' C2 c6 Y, ? <h3><?php the_title(); ?></h3>
/ ^4 l1 b! V5 K7 W8 e9 M% ~ <div class="notice-content"><?php the_content(); ?></div>/ W( u1 K$ }! B4 h1 |) J' L$ W
</div>
) M& M1 c& ?# _1 S' o <?php endwhile;
% Y3 }9 G+ C! T/ L' h/ Y wp_reset_postdata();! O4 ]. y- j. e
endif;
7 E) a3 Q( ?8 v7 F ```
& Q( Q, f [7 o2 [! t. @; Q0 z" t" z6 v. R
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|