|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
1 ^% ~0 O. i, q3 S2 I. n; j0 b9 m$ r) c2 }: W
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。5 N: q5 |7 k4 B, z6 E3 |
, v4 V' G* V6 \2 o) C
以下是创建自定义插件的步骤:
* q- T6 c4 _& a) r3 c! m+ T5 Z9 Z( g g) N
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:& C$ S) j! X6 k4 K' F# I4 N
: j( P/ c% `4 C( I7 ?: z2 Y4 f/ a ```
+ B5 d! x) a7 v/ O1 D <?php; G* f' i" P* @" y
/*
, v4 d8 U* @# U1 C; r4 x1 ] Plugin Name: Site Wide Notices Plugin' {, o$ X. u- h1 i0 Q
Description: Adds a new custom post type for site-wide notices.5 _5 j. f, g+ J1 T8 o% p9 P
Version: 1.0/ C4 d7 J) K4 y& T8 a
Author: Your Name6 r; G, F2 T X! H- x1 ?; v. E
Author URI: http://example.com8 s' j' Q/ W: p+ W' _! m; s2 g% Q
*/+ l& t' G7 A3 s( b( A0 O4 I
% M0 _ I0 l$ _7 X( B // Add plugin code here...
6 k$ s2 ~8 [( L3 \ ``` j1 e( q7 V) C3 r: T# N
# Z6 m6 W+ v) ` 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
) F0 n+ D2 `4 V: {( p2 B' r6 |$ t5 \
$ u- t* p9 a# \8 t2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:; Z3 r& L+ R2 W. C+ N! a; Z# R, a
/ a; U: I9 s. K# i$ z
```. J! T: k8 H# p
add_action('init', 'create_custom_post_type');
; s1 x' D' S' K function create_custom_post_type() {
6 g% A) H3 R0 G# p7 ^& ^ $labels = array(' b) w0 A! }, t( U; n
'name' => 'Site Wide Notices',5 x' L* P; M, D7 v7 ]: A
'singular_name' => 'Site Wide Notice',
5 X/ m& D! T2 x# z4 z# J5 c 'add_new' => 'Add New',
. p$ M' m" R) o/ e: ^; S 'add_new_item' => 'Add New Site Wide Notice',) h" j: I6 ]( n% X/ X }
'edit_item' => 'Edit Site Wide Notice',
+ y+ P8 Z% l% ^, v4 K" }7 u 'new_item' => 'New Site Wide Notice',0 G7 D& g3 z! D/ U4 ]- U9 F- Y' n; J
'view_item' => 'View Site Wide Notice',
k' k1 O; K( T 'search_items' => 'Search Site Wide Notices',
. [1 N& r5 _* T2 E) s6 {$ p! B 'not_found' => 'No site-wide notices found',
! O- h' j- B' c 'not_found_in_trash' => 'No site-wide notices found in trash'
+ h, V2 ~- F7 g( a/ _ );
+ u! w, ? `6 u# Q3 R, j# v9 J
+ {) X3 w! ~' ^; `/ \0 q- i" v. V $args = array(2 r1 l$ h. q3 c2 e, r7 z- w+ T% I3 }
'labels' => $labels,% q+ e t6 O5 x. Z
'public' => true,
" i$ v L% n) `7 z 'has_archive' => true,
. h- P; E( h3 H) g1 P 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
, W8 _7 ^2 Z- ` 'taxonomies' => array('category', 'post_tag'),* ]1 p# ^6 p7 g3 v
'menu_icon' => 'dashicons-megaphone',
% M1 b$ @8 \, K. P; z$ Q3 D 'menu_position' => 5,
- R2 b8 u! O/ H7 O* z 'rewrite' => array('slug' => 'site-wide-notices')
; l2 e3 n. O; u }, F# D );
* e$ I |, `( I$ x
! S R/ p: F, t, v register_post_type('site-wide-notices', $args);' o4 T/ `1 b6 N* H' M
}
$ r/ d: x+ B8 F; G7 u* K, z3 B ```
9 }& c, W' I1 ^$ k- S8 Y/ d+ m% d9 }0 L, G4 k
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。6 q! p8 `# A. r: P& A/ k0 M0 Y6 v C
% [! |6 j5 `. E. i
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:5 l5 N; ?$ ~5 \% `, B" V: s, T
3 C0 B. V5 _- {& c+ s
```; Z: T& g: R0 P& |" y' S
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
7 d7 Q, [3 P2 c( j* t5 ~ function add_site_wide_notices_boxes() {
$ G/ w- `6 p, {- y5 {6 _6 N" C add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
6 b1 e5 Z f5 s' {$ R. y }
+ @5 O' v) I+ v% r! ~
: R1 e7 @& k& Q& _ function notice_details_meta_box($post) {, x7 P6 [( c8 N0 v
wp_nonce_field(basename(__FILE__), 'notices_nonce');0 [0 J; \' G ]
$notice_title = get_post_meta($post->ID, 'notice_title', true);$ e1 d5 |2 y) r
$notice_content = get_post_meta($post->ID, 'notice_content', true);( Z: U$ N* @% c8 R
?>! @- `. o' U0 f' Q J
<p>( X" s) E% q3 H' L6 v0 D) f
<label for="notice-title">Notice Title</label><br>6 R7 C f8 i$ s4 K
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
; o/ `5 D/ a B: H </p>
) j8 {; T' _4 M& s3 q( G; i) Y <p>, k, I9 p) p/ F) e& H( N V
<label for="notice-content">Notice Content</label><br>) ^) t4 G+ @; l: i
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>5 C( k) M4 c- s& S
</p>
. `8 t6 h6 l' ?* \ <?php
+ B* t2 m' N8 e- L5 S9 `8 h% s }
1 K. O1 I# O9 C$ |/ P6 k" A) |
add_action('save_post', 'save_site_wide_notice_meta_box');
# F1 g3 w7 Y" X5 N function save_site_wide_notice_meta_box($post_id) {
; T; v8 `9 Y0 l: i& j if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))6 Q" N' h0 x4 i' A5 i
return;9 c) T" e, E/ d: |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
& O5 o/ Z" \, G& S return;
6 X9 }. Y" n) |; B% x8 T1 @7 S' f* I7 y3 ` a8 o
if (isset($_POST['notice_title'])) {
# R. y5 B9 B; @, k. g1 Y update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));. Y2 ^+ Y9 U, ~
}
, U' o U9 W& V9 ^3 Y1 i( ?9 r# l: s if (isset($_POST['notice_content'])) {; k" f! f+ X, R0 K3 k v
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
6 c$ z9 M: q( f/ d: o/ ^3 ^ }
% j2 _3 K; r( y6 K0 Y }
9 w( {6 a9 j; t8 Z) x/ B% x% p4 V ```+ `% r3 \9 \# b- T) s
/ C4 Y1 n/ C( m% O 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" W1 T# h( @% ]! L/ u$ T2 E k0 j4 c
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:* }8 L) L7 v _! P9 `- J( a
, Y5 K; E- f6 H0 v* {0 J ```
! b/ W, Z' S7 C" O $args = array(
1 [$ K8 M% Z8 e, m. ^7 K" w% [ 'post_type' => 'site-wide-notices',9 R8 s1 h6 ^' d L& ?% ^
'posts_per_page' => 3,
5 O/ N' Y! P! \; T* W8 M1 { 'order' => 'DESC',& ]( o% H2 f5 a6 b5 D6 I( t
'orderby' => 'date'
# f8 {: q& l; i9 a) g0 ~7 i0 g );
, }! _8 D+ S; _/ e% a $query = new WP_Query($args);
+ J' O- }( O, L if ($query->have_posts()) :
" i. A9 k0 x& R9 q" {1 _1 \ while ($query->have_posts()) : $query->the_post(); ?>; d8 Y$ H+ o( N1 L( V
<div class="notice">3 z1 `, j+ w! H( E, \
<h3><?php the_title(); ?></h3>
4 G, `! `4 k: _9 Z7 t9 u& [ <div class="notice-content"><?php the_content(); ?></div>
2 r/ @6 y% o' T* B4 Z </div>( X+ E- z' D, B6 D5 ?! g; k
<?php endwhile;9 B7 G4 \' ^9 b0 A6 Q
wp_reset_postdata();, s& D4 x D4 k8 a8 A' J3 F/ l! D
endif;8 e7 k* ]- _- g4 N
```
; ^- a9 M7 N! g
" d, `1 n' \# |" C7 J% t, Y 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|