|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?, y- [9 w: A5 Q9 L
% I2 q- [- K6 N如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。7 P5 ~5 v4 F# D, n9 P- |( ~
9 M4 ^& t- Y# |
以下是创建自定义插件的步骤:
. x3 a; \$ K# R( H& {- x/ F# q* r' `/ r
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
0 U; i( O! y: ^5 F4 ^
; A# R! G3 t' D# J6 _7 e1 t ```
( T8 @/ e6 G1 Q6 H% ^( w# i, ] <?php
% F z; B( q. r9 p4 w /*+ l! b/ G7 j0 f" ?
Plugin Name: Site Wide Notices Plugin% L" {: o" O- ~
Description: Adds a new custom post type for site-wide notices.$ I4 W# c! b1 P, ^* l6 s
Version: 1.00 S" K1 b9 Q: N# d+ T4 e7 h# d
Author: Your Name
4 U$ }; O" ^% h6 o2 ?8 `9 Q( e! t% h Author URI: http://example.com
m; `, o. s4 n */# N6 n' ~6 ?+ v( b( x+ N! Q' ^9 r
6 k* M1 f; l. t1 K+ q: Q // Add plugin code here...
3 n$ s3 @: ?- ?! ^/ w0 p/ X ```
: n7 L& v. K& ~" Q% C
6 t; w( f, x! i* b! j( @/ Q/ q5 r, ? 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
# c2 u. u. X8 f& C: K) O5 K; c; w3 u `/ r
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
: T1 d0 d4 |6 D3 s% y, Y6 J; r! F
' @# e, Z W7 Q& m( J: d7 O ```
9 w2 [, ]" ] E( X0 L3 e+ x add_action('init', 'create_custom_post_type');
2 ^% P6 x1 k: f Q- X function create_custom_post_type() {
8 O$ M$ ^+ B- z $labels = array(0 C( Y7 n) t! g# @4 q% k
'name' => 'Site Wide Notices',
" r4 F: P4 U+ A @* `" b$ {# i 'singular_name' => 'Site Wide Notice', k. p+ T: ?- ~: [+ g$ F. s
'add_new' => 'Add New',
- ?; `7 @( M" {' R" T; c, r 'add_new_item' => 'Add New Site Wide Notice',+ H" P# p$ q) K
'edit_item' => 'Edit Site Wide Notice',
4 }1 L$ O& _5 V 'new_item' => 'New Site Wide Notice',$ Z- z5 L! ? L- P* k
'view_item' => 'View Site Wide Notice',
- ]9 R; S. o8 }' K* s 'search_items' => 'Search Site Wide Notices',
6 T' d; J2 q- @7 z5 v- O 'not_found' => 'No site-wide notices found',. Y2 p' Y9 a: |) d( Z
'not_found_in_trash' => 'No site-wide notices found in trash'2 `- L! ?/ @( d( y4 `# R
);/ q! I6 v* o$ S9 @
* L* [+ h9 x. S* e) p( f# w
$args = array(
/ C9 C. L' E( ?$ a+ D% A2 L 'labels' => $labels,8 X' t/ w( c4 y8 m; ~! s
'public' => true,7 `+ c3 B8 M4 s2 k& w f
'has_archive' => true,
* h; f: f1 o" u 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),% f; G* V9 L+ R8 n, E
'taxonomies' => array('category', 'post_tag'),% s0 C2 k' `$ [$ `8 W8 v' w h
'menu_icon' => 'dashicons-megaphone',& K% P' e% c! w f1 [3 k0 R. R1 |
'menu_position' => 5, G0 k) S: h- I' s2 S: S* c
'rewrite' => array('slug' => 'site-wide-notices')) x, j% y1 `0 W
);
h* M! P- Q4 R+ h1 _
! R. w" K! I' q% r; n- R. [% p0 i register_post_type('site-wide-notices', $args);
6 M4 L' A) C' A7 K4 a, D1 W U7 T } j9 e) u: P0 P- \% }" J
```
; w# |% c4 `5 k- b0 J% M% Q/ G3 `- G% [2 `; _, r
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
& B2 \* D4 {3 T: B. ^6 ~
; p8 a( {# ?0 k. |. K# H$ M3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:6 H' A' \4 f) P& r/ |9 S9 _
, q" j4 Z& ]/ S% S5 H ```. M/ @ r0 ]2 X- B
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
" n5 f7 Z9 l0 L/ T function add_site_wide_notices_boxes() {
' v5 G% @5 T7 m+ @0 A$ S add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');% W" Z" h: ~$ c# _; s4 ^, B
}* u% Z& A# G$ t/ ], m
9 ~' K1 b$ Q1 X4 s" h8 v& B. D
function notice_details_meta_box($post) {
+ w! j( r f9 j" M0 I wp_nonce_field(basename(__FILE__), 'notices_nonce');9 m+ m/ Q! g6 M# b w
$notice_title = get_post_meta($post->ID, 'notice_title', true);% }: m' N" w e- _& E
$notice_content = get_post_meta($post->ID, 'notice_content', true);
/ T7 t6 [* F1 i, N& U0 s, Z ?>
) k. _: n6 p# \6 G, O2 m3 O/ w. C <p>, x0 j2 f* V+ u: f
<label for="notice-title">Notice Title</label><br>
8 [6 g) m: @& S1 X. h <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
5 Z" W' Q5 J$ z- j" J </p>4 O' f) G3 f7 C1 p) P' C
<p>
) }; a0 |1 Q- U% b* h <label for="notice-content">Notice Content</label><br>3 C3 b& x( V$ A1 ?8 s# a0 b
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>9 m, X2 m5 [0 N4 k3 T
</p>
( @3 f7 @0 H. X6 f: K <?php! R+ y+ z2 l; }5 n" }* E3 }2 W
}4 F2 T& }' c3 n7 ?/ S
3 e7 j9 y% d& ]! ?2 \+ T/ P! C7 g add_action('save_post', 'save_site_wide_notice_meta_box');
G, M6 e. s0 t' n- G ^ function save_site_wide_notice_meta_box($post_id) {8 j |: r8 ]2 s- [) j, d/ o
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
$ }( x: y) e! N0 l' _% p* l5 Z0 e5 } return;
$ f) ~$ n6 |1 C' A. C5 q if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
/ i( s( M' M2 H: D' a6 ` return;
4 i1 O& j1 P. z& r
1 G7 `8 A p G if (isset($_POST['notice_title'])) {
% \0 b+ L( m: l8 p6 M update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
& T8 p2 Y7 Q2 _ }; e' l% k* G* H
if (isset($_POST['notice_content'])) {6 |& L% h: N m: p) s' ]
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
& u7 w. _. q/ q( l% l" k b% m; a }
! T" p2 d4 ]. a! T }
8 ?- c* X+ {0 X. E ```
+ K" n9 a" I; B6 a! ]4 Q$ h
4 q6 K* o- v' L: o% r! Q 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。" B! F `; f' i( r* t3 `
7 g1 j% d V( ^5 p/ w( g4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
2 R: X+ c/ f! s+ G4 [ |, _* Z( k) s+ B& `9 L/ T% e
```: u$ H# k, }4 E3 ?+ @0 j2 k. k
$args = array(7 b1 D A P$ f+ C2 ]* c
'post_type' => 'site-wide-notices',5 J$ |7 A+ x) B# `
'posts_per_page' => 3,, e) c' f8 n# X/ d9 o
'order' => 'DESC',
% `# k: i0 A. W4 b: J) f( y8 L$ |& f 'orderby' => 'date'
& L! i! H' I9 x( V6 S/ ]* Q );+ @% n2 n" _) A) O( J+ b, x
$query = new WP_Query($args);
0 d. e: t2 k2 B7 o# R( D2 A if ($query->have_posts()) :
9 @/ _ }9 ~* P9 ] while ($query->have_posts()) : $query->the_post(); ?>
8 M3 u. @5 p8 Y9 i( K* t <div class="notice">1 g" c4 i" F( P2 ^) ^, L2 O
<h3><?php the_title(); ?></h3> l4 F: _0 i1 E" f( ~6 J
<div class="notice-content"><?php the_content(); ?></div>; E$ [& R, D. i& f+ w8 Z& S- D
</div>6 E( T- ?, q& p3 Z
<?php endwhile;1 X* v2 M9 o' L- v4 |1 @
wp_reset_postdata();7 c1 {& x: I: r( D
endif;- ]0 l G$ l2 b4 H! N9 [9 ^
```
f9 E# i8 I) K1 K T. u; A6 ~! Q; c4 }
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|