|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
# |. E; U4 H& j: P; i3 y) t
% M5 S6 P4 A4 E如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。- z& }8 m# A& h7 j* D
3 Y: D3 S: ?8 z9 b& \
以下是创建自定义插件的步骤:
, x o% e( o$ R8 Q# J
6 Y+ U8 ]% g9 {. n1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
- C4 |! e' l/ f$ p: H: S
7 B/ I2 Y1 }7 T7 [7 ^0 e; ^ ```
$ w. s, G! B2 I0 q, t <?php
5 N2 h. m' M- x8 s& c" V /*
Y8 ]1 [) |. c: @, v1 d Plugin Name: Site Wide Notices Plugin
. B. S3 t) Z. D Description: Adds a new custom post type for site-wide notices.
1 g, x# e4 p7 \1 e Version: 1.0' |5 b3 q1 s1 [) u; b& W+ r
Author: Your Name( {7 ` T. ^2 d& n: c& B
Author URI: http://example.com5 Q* S" P$ Q9 k& d. ^
*/8 u6 x( }. {3 s( ?' p6 k
9 X6 ^( P1 b0 U, Y6 H& Z% [3 C2 y // Add plugin code here...
# [* E# z% d0 K: ?9 A6 u ```
7 c+ o# C8 f2 h) ]6 c h" P7 x
# z: c: y: q1 @; y' m0 L9 T) b 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。/ c1 V Q1 A# c
. \8 g+ Z) |9 A' t4 o7 D# o2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
' V- S+ L/ W B G1 J+ g3 B# _ Z% N2 y+ |# f8 g) J2 O
```" I/ @0 p' B5 p
add_action('init', 'create_custom_post_type');
& Y# d+ g/ n c J6 Z6 @ function create_custom_post_type() {
5 ~+ G" n7 Z) [5 a3 j e. b $labels = array(
x% h4 ^# j, k/ O( j 'name' => 'Site Wide Notices',2 F J( n5 Q; D4 X' x: d# J
'singular_name' => 'Site Wide Notice',# P& |- l" A& ?. P6 \- v
'add_new' => 'Add New',
# Z. o+ ?% K; l9 m5 @ 'add_new_item' => 'Add New Site Wide Notice',
7 {' d" K9 d6 p I! e 'edit_item' => 'Edit Site Wide Notice',
0 A* e' z) V! D! M9 F 'new_item' => 'New Site Wide Notice', j- a1 F9 D- W
'view_item' => 'View Site Wide Notice',
( i2 I2 l' U; f( x$ ?1 f r+ D 'search_items' => 'Search Site Wide Notices'," t" C3 E& i; p5 u# g7 L: e
'not_found' => 'No site-wide notices found',
5 |5 m, @7 P# t 'not_found_in_trash' => 'No site-wide notices found in trash'( }5 m, i$ F' \6 n
);
4 y6 }5 d! t7 Z" N5 n, P; T- G$ D L' C
$args = array(
4 L0 r' M5 q' M8 J# j$ o, Z 'labels' => $labels,
7 M6 |# K I6 H" ]' } 'public' => true,. f, L' X$ K3 W2 I# z+ I
'has_archive' => true,
; d- g3 y: M" |, r 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
! M# ]) D7 \# U& O 'taxonomies' => array('category', 'post_tag'),5 m u3 J0 q4 _, S+ b6 @
'menu_icon' => 'dashicons-megaphone',1 K2 j0 Q7 w- H+ b3 o8 y/ m
'menu_position' => 5,
3 C* O7 {4 ?) M' B 'rewrite' => array('slug' => 'site-wide-notices')
6 V4 V4 x- ~/ n; R5 j );
& Q! D6 `3 O! n! P$ v) ~
$ W, D. B$ p/ o- B/ W register_post_type('site-wide-notices', $args);( l9 s8 N2 `% a( r- W& K' O5 ]
}( u, m! D- @- ]8 F. k
```# ]* T1 j9 ], \7 ]' c4 z
- h- K/ s& B" ^2 R9 r! ?4 A4 _8 } 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。! R7 ~4 I$ L9 h* _" m; C
7 Z. u/ G3 a8 G. ^ C: r1 @' B% `
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:- h4 a9 `" p7 O+ Q& s
5 M) s: R* G E* z+ A3 h
```
' N! d- d: A. k add_action('add_meta_boxes', 'add_site_wide_notices_boxes');% x# F5 f7 `4 S8 A# c7 e( E
function add_site_wide_notices_boxes() {& u: U) W2 d- v* S2 G( P' g* u
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
* I5 u( ?: b* T- X! L s1 i }
: J# W$ i" }$ j
# [( u/ S& S& L- F. F0 T& w H function notice_details_meta_box($post) {1 j3 [' ~4 ?) ?1 ]( i* j4 A3 ]
wp_nonce_field(basename(__FILE__), 'notices_nonce');
* F2 A# D6 u" \; `$ }# b $notice_title = get_post_meta($post->ID, 'notice_title', true);+ E6 g7 ^- r, v2 V& x; h
$notice_content = get_post_meta($post->ID, 'notice_content', true);/ _- O$ E% z: \
?>
" ^! j+ D1 X& K; x4 V' j <p>
/ X& P Z$ u7 u# o, T0 C% C <label for="notice-title">Notice Title</label><br>
: H8 r* [* C6 [( U <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">, S6 d* i% A' ]
</p>
; ~5 u& H! r0 @6 D$ H( d; \/ q <p>. e+ ~8 L. r4 b, u, _
<label for="notice-content">Notice Content</label><br>
& Z) H; j+ i. k% l( X/ S <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
5 A$ b3 o3 W$ G/ u, d- J( W& z+ ]3 \9 x </p>; e6 E, w- _! F- t
<?php+ r2 W, b( K% @, ?8 y2 W9 R
} p' P' ~+ I' s) n; [. v: @- l
; T7 Q6 N/ o+ y6 u. G6 [& T add_action('save_post', 'save_site_wide_notice_meta_box');
% r/ x' Z5 p# o. L function save_site_wide_notice_meta_box($post_id) {0 I" H! P' v5 ?' N! \; G# ?& p
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
! m9 Q9 V5 U/ W9 h1 T* N return;8 s! z+ e: f) g$ D- r
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
/ d/ \7 i$ i n& Z, P/ s% ^ return;
! F7 ^. F( Y3 X$ A! L
6 I( b- @( n, y* Z if (isset($_POST['notice_title'])) { b* v, W) H! g& R
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ j6 n; n3 _* L5 l$ n
}- l m* f+ |# e5 |9 d* r
if (isset($_POST['notice_content'])) {# N6 L3 }& `! V% m$ k3 ^* l
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));' h& a- B$ }6 K$ _$ H, ]% z' x
}$ n9 ]( z' t! P; E% T H$ I
}
9 t* ^' ?. Q$ l. c$ _ E W ```
+ S) I9 f: Z# D0 N, v% F& S3 U X% i" |, C2 S: t, k1 l
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
/ F2 J# w% ]# k) J! ~0 h$ p# d% o
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
" _( v7 m' J! n5 S% U: \) a9 o8 c# y, P0 ]
```, b5 n f+ x* _# K5 d# N
$args = array(
# e n$ q+ g+ @, } 'post_type' => 'site-wide-notices',3 N/ {+ O0 w2 u4 c! }, }1 C
'posts_per_page' => 3,
6 e6 Z* u* U5 N. [ W 'order' => 'DESC',
: f$ E9 f u8 K 'orderby' => 'date'
9 J7 w- b! i$ B8 t6 E );
; P8 v) |# ~; D7 D2 r* a& h $query = new WP_Query($args);8 V1 f4 y8 F! \* E, z; ^7 h/ X1 Z
if ($query->have_posts()) :7 M. E- ~' v/ c
while ($query->have_posts()) : $query->the_post(); ?>
; o! i! T6 c, j7 T <div class="notice">
1 M( B& N4 }1 c7 t <h3><?php the_title(); ?></h3>6 i' H3 @7 F4 X" h$ z$ U
<div class="notice-content"><?php the_content(); ?></div>. m. e" r# B. w: d& K7 J
</div>
$ R. N5 @" Z5 n0 P& O2 L! p4 W <?php endwhile;$ u- i% t! b- ]: B
wp_reset_postdata();
' s/ j% D4 W2 c1 [; u endif;
, O' w' R. g. e* d# ` ```
1 Z% j1 x, @) e$ H1 T; K
7 S$ X0 B0 Z3 [) a: v 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|