|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?* ]% Z( {/ V# `/ p- x" X
6 f' Z3 f# H! {5 r如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。/ _4 T! s- F4 D t: F2 [
& t# h) H5 j' `7 {9 L
以下是创建自定义插件的步骤:
0 L n( W7 Y- D m) C+ b; R$ E; u5 ]6 S' R
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:4 k0 w9 a* v0 b( K
# m9 u6 N2 J8 `+ e9 j; I4 o
```
" ?' g+ N5 l1 K/ }/ [" j <?php
; ]1 Y p$ B. Z/ O /*
: U( l9 N% d3 V, v5 o9 [ Plugin Name: Site Wide Notices Plugin$ D! t- M9 i, j' G8 Q
Description: Adds a new custom post type for site-wide notices.8 j! P L; A* y" F9 c
Version: 1.0" J0 I( I3 @+ T7 T. D
Author: Your Name v6 V6 L# e! m% C; o
Author URI: http://example.com
?# J9 D* U- x7 E: W! g! r */
3 Z) ~3 a, \$ E* {$ h" Q j$ D n8 K8 H) l
// Add plugin code here...
+ @% e$ g) @" l0 j" t ```
8 p3 j+ F7 f/ \. |7 } Q/ }" d3 D5 k
, f( {. O1 ~5 w- o( q 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
# |2 z8 S, ^! k j' [1 Z/ J. J+ {4 `& j3 M, g
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型: U5 p- E- a$ L1 h! `: A9 V s
6 j% b5 X7 A, j8 J( H ```" e O+ C$ a% E5 H7 e7 ^
add_action('init', 'create_custom_post_type');
4 G/ {' x& x+ c$ _! Q function create_custom_post_type() {
% R4 ~ o& b w8 c $labels = array(
# F: ~4 `% l. n. F; d3 P) j 'name' => 'Site Wide Notices',: t! j- K, N# L6 ]( |! Z6 ~
'singular_name' => 'Site Wide Notice',- Q7 U' W, l+ Q6 q
'add_new' => 'Add New',
0 D E# R L9 A 'add_new_item' => 'Add New Site Wide Notice',
6 C: j8 c' {, e$ y- Q6 ?+ @- x" T 'edit_item' => 'Edit Site Wide Notice',
' f4 ?+ V3 k7 C 'new_item' => 'New Site Wide Notice',8 }% t1 u# G; A4 F3 M
'view_item' => 'View Site Wide Notice',4 O, |+ V1 E5 |& O' S4 |# T
'search_items' => 'Search Site Wide Notices',
6 F) |1 v |$ y1 } 'not_found' => 'No site-wide notices found',
" f5 s! e* h4 g" d$ ? 'not_found_in_trash' => 'No site-wide notices found in trash'/ O0 z! X. h; t5 j5 F5 p9 K: ^
);
& V$ ]$ f. C& y$ y+ l7 Y. }# a# [) O( K
$args = array(6 E$ x* q& f: e, J9 e
'labels' => $labels,
- c3 h" A8 e3 N# q 'public' => true," x: z8 `0 p3 `" c' z+ ^0 A
'has_archive' => true,9 h+ v+ f1 _) K) i) g
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions')," q+ Z2 Q- O7 @! N
'taxonomies' => array('category', 'post_tag'),. M7 N9 X4 A' O
'menu_icon' => 'dashicons-megaphone',
8 R3 w5 {2 i' }/ \6 z. O6 P, | 'menu_position' => 5,
% Y2 J; v/ f Y, S2 z2 j 'rewrite' => array('slug' => 'site-wide-notices')
" q" J7 A. _' D/ I# | b1 j; J );
( E3 m) D4 B5 r' J0 |2 T" A i; Q% |& c# b i5 e
register_post_type('site-wide-notices', $args);* F+ p& |/ O! G6 C1 o
}
+ l* B( O. V( O' Z ```
9 R! ~) @ I! x6 l3 A9 J+ `. x5 c( m6 v% T, L
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
1 ~. \- c+ p0 i6 T+ r G
9 `8 n' Y" K# g0 L3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
1 M, n+ U- p, U8 d7 a$ R& X( i% w8 R7 H" E
```
1 J+ L0 P/ S! q2 y m) K# ^" c2 U add_action('add_meta_boxes', 'add_site_wide_notices_boxes');* N D+ \ M" {- {; S {
function add_site_wide_notices_boxes() {
7 G- Q/ t. x f8 k2 K' M add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
! k: r. V9 T( x- B g }$ R9 h' `: {* c, z% S
6 D: c6 E- Y* w9 c& S' s. e
function notice_details_meta_box($post) {
7 w$ \* d- g' ^8 w" h wp_nonce_field(basename(__FILE__), 'notices_nonce');
5 d1 m2 _: \: c$ j [5 L `3 a $notice_title = get_post_meta($post->ID, 'notice_title', true);
0 i; Y4 u/ H1 V1 T9 f $notice_content = get_post_meta($post->ID, 'notice_content', true);
# z9 q, e7 ~" Z& A ?>
- f& b. a2 @1 m* y: ^2 W <p>8 e+ e# ?8 |2 L1 Y2 S9 Z' E4 h2 ]) S
<label for="notice-title">Notice Title</label><br>( v0 M6 n9 H/ z/ v# J
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 ?! e) j7 [* p3 ]
</p> D9 a: z1 Y [, [6 {
<p>, H% d$ W p& W! @8 R
<label for="notice-content">Notice Content</label><br>
. i9 U5 y0 n# B8 c <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>* Z) J: p+ K( j q' }; r* s2 n
</p>6 P+ g" o2 F$ y2 g9 J
<?php
: l; @" E6 ]3 [ }
4 L: i8 _/ [% n, E1 ^8 Q
; ?: A2 {. |& x5 D8 h add_action('save_post', 'save_site_wide_notice_meta_box');
' f5 S9 n! _+ n# ?' y1 @+ f) { function save_site_wide_notice_meta_box($post_id) {
1 v0 N/ r- m0 w; l& m; v7 { if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ Q7 f- r6 ~: k9 s& O- G; J return;8 u! N% I1 p3 O
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
0 V- b; @9 K' N5 Q. r/ |) i return;
/ _$ [6 h% ~3 H0 \
8 x! I" l+ |5 S; } @" S1 S if (isset($_POST['notice_title'])) {- b+ w* d# i/ }5 G
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));0 ?0 I, X% R" X$ p% R
}
Z) E) ]" o# @: |# L x if (isset($_POST['notice_content'])) {
7 j, \2 F( ~9 Y9 `7 A2 \ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));6 L8 Z ^' l5 m$ q0 ? y
}; _5 v2 J* O7 ~- a ?) X7 r
}, {' w! A) T( Z8 j) D& I
```
: u% Z5 |9 b& j$ _9 ]+ T9 { @ _
# d; p& y+ v+ [* P& J9 K 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
1 M2 I( c: s/ `( T
, B Q0 e& v! W% D4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
3 N1 l; ~! v: B4 W* N5 ] Z7 o& l/ N0 b6 p( g
```) `: R6 n9 _' @( N y1 c. Z+ S' {
$args = array(
3 G/ r8 I& B3 A 'post_type' => 'site-wide-notices',
$ d/ [1 X5 z" u 'posts_per_page' => 3,
! H5 H& |% ? b | r: _ 'order' => 'DESC',+ j1 O7 p2 v$ ]" N5 F, j% P7 _
'orderby' => 'date') v6 V2 m* ]5 s, {9 ~
);4 L8 a- z: E" F8 J% m6 r
$query = new WP_Query($args);* U1 P& M7 ~3 x6 S! V
if ($query->have_posts()) :8 z% n& t/ }; |+ x) [9 p' D+ b
while ($query->have_posts()) : $query->the_post(); ?>
* ?2 Z( s/ Q$ F) U <div class="notice">
' ?9 [: N( A# r1 U& |6 q% x* _ <h3><?php the_title(); ?></h3>
4 f, t m0 m' @ <div class="notice-content"><?php the_content(); ?></div>1 B0 _# P" A: b1 b4 R6 d6 A
</div>" s4 F" t+ L* {1 N3 m6 z1 \2 R* g
<?php endwhile;' V3 y, P9 a% A7 f3 A) y
wp_reset_postdata();" b" T, C4 U5 g" c3 ^: l! C( v5 Z
endif;
( o4 ~1 f% D+ ~9 m ```0 N$ F0 X/ W2 R1 N& m% `
& ] R V; w j# F0 m" o* x 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|