|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?$ W2 G: y3 u: \% K) L/ E
9 p& }4 ]9 p, e+ r- T% a- t J( a+ q; q
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。+ z. [# I" Q' z
! R( P! B. K" B% B L以下是创建自定义插件的步骤:
, e7 | T, x4 h }3 }) `9 C) p6 x# W2 n* d( Z& R
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:- c3 |" L2 r# {+ v- @1 u- D1 q
2 r. E/ r7 j; u2 x
```
' s$ H B1 p+ T, X, U9 | <?php
$ q/ Z# u! C/ X8 Q) V6 B: o /*
% D4 S3 O9 g- Y& }. d Plugin Name: Site Wide Notices Plugin1 R0 _4 O4 r( z9 n9 k8 ]1 x# k
Description: Adds a new custom post type for site-wide notices.
) ]5 T, D e6 @ Version: 1.0
) u- @$ @5 K8 `/ D1 Y1 W Author: Your Name2 p( O: }, A( g3 |7 ]. y
Author URI: http://example.com2 l& D3 Y6 j: G/ p' O# ?
*/+ P: e* E9 f: H/ J) ?6 } o! K
0 n# Y7 H2 E. }; `% S0 H, l5 w // Add plugin code here...$ r+ }7 @# t ]; B& [. K
```
& S8 l0 ^" o) v$ H' Q" V- x' Q3 L
0 D% v/ o. {) \4 B 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
4 @; [; C' N' i1 j, u, q1 x8 }# O1 i" h
# |) a/ Y. ]; |: A, \, g2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:! I+ `) o2 B% B$ V( d: r
4 Y" s) \$ n- e1 `* _ ```
* H% ]5 `1 K0 f+ s+ q; a; K add_action('init', 'create_custom_post_type');; Q- p7 K( Z8 y) I b
function create_custom_post_type() {$ C* e. W$ j5 Y+ Q
$labels = array(( f q3 A C/ m4 U
'name' => 'Site Wide Notices',
6 T2 P- F) f1 ]# W3 s# j. u1 e' B 'singular_name' => 'Site Wide Notice',1 Q2 G% Z" _0 @% U& x1 X4 t
'add_new' => 'Add New',
9 j q7 C2 `8 n" z" Y( {6 ] 'add_new_item' => 'Add New Site Wide Notice',+ A: ~4 ?/ Q2 [/ M0 _! u- L& S
'edit_item' => 'Edit Site Wide Notice',
2 k; @8 x9 |2 x \6 R9 g# A 'new_item' => 'New Site Wide Notice',/ m z) a* A' K: n, _- _
'view_item' => 'View Site Wide Notice',& u3 _5 F1 `" Y
'search_items' => 'Search Site Wide Notices',
' U8 P) C% H* |" i2 [& S. O6 E 'not_found' => 'No site-wide notices found',
6 [9 ^- y* d- c& N& Q/ F1 R 'not_found_in_trash' => 'No site-wide notices found in trash'
9 j9 A1 @/ E8 U" c- M0 r );
5 F( N; ?3 C- W9 b0 k1 ?
, E, d# x" U c: }8 e $args = array(6 w7 ~) Y1 @, T1 k$ R! _
'labels' => $labels,: d, j* @4 S! \" Z- ?# O2 V
'public' => true,5 g4 L' b+ H& D5 c1 r1 U8 R! K1 i
'has_archive' => true,
& a+ T( |! Y' Z5 a+ d5 C 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),5 }$ [/ q0 F3 C5 y/ H0 l. T( N
'taxonomies' => array('category', 'post_tag'),( s5 _- L! `, F) Q
'menu_icon' => 'dashicons-megaphone',1 \' r3 ~9 g0 ^) J1 o' D- g
'menu_position' => 5,
1 S: g% z( F2 r. } 'rewrite' => array('slug' => 'site-wide-notices')
6 H) q; T3 z' v/ { );
2 a, }( L x* L6 a9 V
- x3 ~, m( m8 V$ V2 g% [1 D register_post_type('site-wide-notices', $args);
) \' i* z; U/ \& L( e }% |$ b. z$ S, L0 e5 l
```. n' F* k$ j+ @$ _9 b
6 N( F0 [7 J/ b& p; s
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
8 X8 }/ t, |% ?" }8 T# r! }) J4 C" p& ?8 z5 y3 q( \
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如: Q9 k7 @0 ~% v- }0 r* e
: F4 d T% m: u8 i ```
4 X' \/ b. e+ L( h add_action('add_meta_boxes', 'add_site_wide_notices_boxes');+ V% W8 U; O4 Y' C9 Z
function add_site_wide_notices_boxes() {# @4 ]9 a; a) K- j
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');0 G d& N* y# @2 L
}/ f" T. ^/ Q; B
4 B8 x4 c; X( X b function notice_details_meta_box($post) {
" H8 n% K( Z0 j3 b wp_nonce_field(basename(__FILE__), 'notices_nonce');
3 L! N( W4 ~3 l( o9 i $notice_title = get_post_meta($post->ID, 'notice_title', true);; }& g' U2 s& B! V
$notice_content = get_post_meta($post->ID, 'notice_content', true);" L, b+ _2 K3 Y7 ]
?>+ a5 B4 \5 W$ C8 c
<p>
b- o( d7 N6 m <label for="notice-title">Notice Title</label><br>) u2 k8 L5 j" p/ q" r
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
! u( b" @. i) y ~0 y# M </p>
6 k! n0 C2 [! r8 R" H& a; a <p>7 D8 x+ q5 Q. a: R
<label for="notice-content">Notice Content</label><br>6 \; l1 o) z& o3 t" x
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
7 S- t- M4 Q0 ^ </p>$ r( q6 @3 p8 l
<?php5 \. \6 a8 G1 S. `6 s
}
9 b9 D8 ^* O; l7 a+ h }- j; R8 _
add_action('save_post', 'save_site_wide_notice_meta_box');
4 x6 v( \% |/ ^* J3 y function save_site_wide_notice_meta_box($post_id) {
3 J( U/ [- ^% v2 ^: t$ t2 C if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))): f( G2 [* X) e: Y8 |1 D+ ?" T6 e% m
return;
" x- h+ M; P5 l% C3 A if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE), ~" U, D: o! X; x m
return;
$ |8 c7 U7 d0 o d" F9 G9 T, E
# m, f) r7 B5 k' X if (isset($_POST['notice_title'])) {
) b3 k8 g/ O) m4 r9 d" \ update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
. g" u0 l1 ^) P+ J0 h" i$ \3 Q. s3 d }9 N" X: [& y& @3 N5 F' p
if (isset($_POST['notice_content'])) {
2 M# s9 p) t! C* a4 s/ D: D& u( ? update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) @; v# R6 |9 r) B. K) v/ ] } l" e! w Q! E# f+ m* ^
}
" b& Q3 E* z4 s c+ o4 r9 f, M& X ```
3 |2 o- H6 w/ s4 M' u1 p! q; K
9 }' U/ u3 a1 s2 F8 @- e/ l6 E 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。* B7 ~$ z8 b; Y- Q6 ^
4 R+ G& ~ H# }% T5 E
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
/ ?: l2 x5 C% C2 s! a! h9 P, R1 f$ p/ H- J0 B# n( J* M9 I |& ~7 M
```
~: R8 n! r2 i4 W$ [2 [3 @( c $args = array(
6 H! b0 m0 g$ Y. a 'post_type' => 'site-wide-notices',
9 E2 N) w( }% l 'posts_per_page' => 3,
5 d3 @/ f" A7 e# @2 x+ ] 'order' => 'DESC',, b( g& C; I4 A# k, d: R( h
'orderby' => 'date'& w; I/ | _ }- p
);
" b1 M" |6 Y) `& G! r' \" M& }( u $query = new WP_Query($args);5 f. s! p- w* M$ v: b8 J
if ($query->have_posts()) :1 C: d- ~8 W9 G3 g N0 n
while ($query->have_posts()) : $query->the_post(); ?>
# o6 x& f7 u6 _( n4 ^! @. b6 @5 k <div class="notice">' s% |7 K) Z0 [2 s' f; B! d. X
<h3><?php the_title(); ?></h3>
4 ^3 c% H" ?( R1 |$ D3 K <div class="notice-content"><?php the_content(); ?></div>
- E/ j1 v$ Y1 i d& N; j </div>
! E, Q9 v4 f1 ^- |1 H ^! } <?php endwhile;
- I3 N: B( t. U# ~0 v) W) S, f wp_reset_postdata();
5 r5 ?8 y# j9 F1 T$ H endif;
( B `2 E( q n0 z/ g ```
7 E' K: Y7 T: r" W( o$ L- {0 v, S5 ]) S6 t- Y1 j. n
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|