|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?% j3 L/ h1 x% b" {" n& \0 n
0 \8 `1 s8 N& ]' c& @3 J- @
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
, b! k$ Y6 O; m) x& x v
/ V7 u7 j' `! @) ~8 L( L以下是创建自定义插件的步骤:1 p( [) J' Z8 X* c$ u- u
- F: s: g3 B- ~* t" j& r
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
% m$ E8 ?" k1 T+ c' r* O1 D5 z
& n l. [# Q N1 C! F ```' B/ l7 z" x( y8 A) i# h, Y2 v
<?php
& P+ `- [% ? d- A; l; Y* g /*- z$ W. ]) w- o4 C9 j s1 ~! Q
Plugin Name: Site Wide Notices Plugin
3 j3 H" J9 R( X2 J! |2 c# [' }) f0 j7 I Description: Adds a new custom post type for site-wide notices.
) g8 | E, h+ ~3 M Version: 1.02 W; E' I! |. a# I
Author: Your Name
6 s0 B6 q8 Q9 b, P0 O; [! B& [- n Author URI: http://example.com
6 V/ T4 X/ f ]/ u/ F */
8 l0 ~2 C/ g- |
4 B0 ^0 ^1 U! q- Z0 o! D7 h // Add plugin code here...
5 F! e; Y) E% q9 z0 A' y) I6 l ```
+ Z/ N4 j* k: r) @
9 H2 @0 r4 _' \( y8 ] 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
; V \" D% G" @, B, `( F; A7 F. x h5 l' u# M
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:1 P0 }& c' Y# Y. y
- a! j$ j @1 T6 a
```# z6 q3 ]6 C5 y
add_action('init', 'create_custom_post_type');. H8 \" ~1 j3 h* x' J! Y$ ~
function create_custom_post_type() {
$ z2 d y5 H- e. {7 p* o $labels = array(, P+ Q, I% I# P- x5 M+ f
'name' => 'Site Wide Notices',0 @0 }1 K# Z6 Y1 u
'singular_name' => 'Site Wide Notice',% b0 S' n; u1 }; m6 P/ e0 m
'add_new' => 'Add New',
+ @. a5 c2 x* \! P/ j& i1 D0 u 'add_new_item' => 'Add New Site Wide Notice',
+ ]0 I2 \$ L0 W+ ^- ? 'edit_item' => 'Edit Site Wide Notice',
. B+ ?% h. l& L: N, {# [4 {* ^! u 'new_item' => 'New Site Wide Notice',
0 K7 e! k2 e% \/ V; p4 I. S K 'view_item' => 'View Site Wide Notice',
/ ~. f$ l) Z9 l* T: \; u 'search_items' => 'Search Site Wide Notices',2 o% H4 }+ r: K* C2 K$ p
'not_found' => 'No site-wide notices found',
. z( L, Q: F* f4 r' K/ ` 'not_found_in_trash' => 'No site-wide notices found in trash'
5 W2 k }6 {6 V! O );
/ {3 W% [& T# u8 Y: R; e+ F, S" }$ U2 S; n
$args = array(
2 y9 w# S% U- ~ E 'labels' => $labels,
# }; b0 S5 Q6 E6 ~ 'public' => true,
* L6 j ^. o1 ~ 'has_archive' => true,
! C. h0 u/ }* C' ]* } 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),* t! F6 E7 R# G( E( U
'taxonomies' => array('category', 'post_tag'),6 S7 c) K. C; v% W: d
'menu_icon' => 'dashicons-megaphone',
2 e: u' M; {7 u9 G& T- v 'menu_position' => 5,2 s w1 z, V; m( C; `0 C
'rewrite' => array('slug' => 'site-wide-notices')/ Y, u- q% F8 v/ K5 F& g
);- H8 E9 X0 i3 @
9 v9 S: t1 z6 r% m5 H9 z register_post_type('site-wide-notices', $args);- g" b; [. }3 @) h6 T7 u, ~
}
1 G) c( C* j: O w2 T# S4 R; e ```% j& o9 b+ Z7 ?; H( S
( ^7 }- o% T' O# W# C* d8 t2 A$ P 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
2 m$ F: F1 g1 s! k- X) k% v/ L. x* j2 f9 U
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
[) t% F% e4 E1 N
$ b( c/ ?+ q! w' J2 e6 i1 o. N/ b ```/ g1 F6 D5 ^0 ~ S
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');2 _, g# j/ ?) a5 e8 U
function add_site_wide_notices_boxes() {: Z5 e1 V2 N( y0 ^8 a2 I
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
; e/ w0 A3 s% M. T- i }/ n6 e8 C! D# {
, o8 ]6 k2 W! ?7 V* w: c
function notice_details_meta_box($post) {
* w1 i' K& V7 e, N, e' q wp_nonce_field(basename(__FILE__), 'notices_nonce');" C7 J$ K* x& j
$notice_title = get_post_meta($post->ID, 'notice_title', true);
, w O1 X, A! c $notice_content = get_post_meta($post->ID, 'notice_content', true);
( H- A2 z. z2 d; R" H4 g4 S ?>
. l& U5 u# G2 L0 |6 [4 o. V <p>, V3 _' G8 m7 g- B( M
<label for="notice-title">Notice Title</label><br>
. h. |5 {' |% Q& e7 g: b <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">+ J1 y1 D& F: h* Y
</p>( j8 C/ A* z% ?2 w$ U2 T. h: r: W. y
<p>9 ]0 k2 b/ Y) n! @! _
<label for="notice-content">Notice Content</label><br>2 k$ z5 F/ R/ d( |
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
Z7 L% t N6 l. U) \ </p>5 y) I! C6 n% r4 }" D! l- y
<?php
! q {" H! L* d# G7 p! N6 A/ v }
2 U# A) v3 R+ M$ f( L. x( @
. n2 @3 h3 }2 J% z! `$ I add_action('save_post', 'save_site_wide_notice_meta_box'); M! v- Z8 |1 Q1 l* H
function save_site_wide_notice_meta_box($post_id) {' E( u/ h5 T* b
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))* x4 u0 [6 Q$ @) n
return;* v) L5 K; k4 i. d% t6 L. [! U" `
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)5 ?$ s3 ?$ ?. C& {( H# ^+ H
return;
2 {' A3 M# v" {
0 j4 w; Z& a. L+ F3 k( l x if (isset($_POST['notice_title'])) {
/ k( J N1 G4 T6 v8 Z update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
; Q" Z* ]/ S1 R% k, U+ y }6 U- K# }# e! N5 Z5 \
if (isset($_POST['notice_content'])) {" X4 \# W7 V* f( u- O
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
5 f8 G8 F# C: A% B) F }9 S3 Z4 g4 {2 x2 g( f I e$ j7 I
}
, o: t- S) Y, K3 J) H ```+ C( q3 y, v, K: A5 n/ f8 C! @% F
7 |: S- ~) W- _$ v6 p k 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。9 B6 h) {2 i# G
8 p/ Q/ _' q6 l) H( z
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:! V4 u5 e9 b) k1 I' Q H
/ N: p. o! {2 h; S) d2 p
```1 d- G4 g, F' I) E% ^( \2 I
$args = array(
' B* K# I T' ~8 o8 M 'post_type' => 'site-wide-notices',
- K; U0 i2 r3 V; W9 V8 } 'posts_per_page' => 3,9 q. r W1 b) k& G; q8 d1 [
'order' => 'DESC',
7 r- E0 ] S; _" A) `$ E 'orderby' => 'date'
" h+ O* ]. M+ L8 b; u- K: R) l );
1 @: q: K2 R% @% R $query = new WP_Query($args);
% {% v& C; H: i7 ~ if ($query->have_posts()) :
; {/ {6 s: b9 E( ~ while ($query->have_posts()) : $query->the_post(); ?>, D# B! d" ]9 a. ]% k
<div class="notice">' }( g5 C5 ~, D Y9 b+ i
<h3><?php the_title(); ?></h3>9 Z8 \4 o( ]1 ?% E C
<div class="notice-content"><?php the_content(); ?></div>
6 s; A7 W: ^7 E8 G0 R </div>
. F! v- G; i& F* j$ k <?php endwhile;+ D3 H5 ]# ` r6 a# K1 ^
wp_reset_postdata();+ X, d; N1 ^9 m9 p+ I3 M7 G
endif;
' p3 J3 ~7 ] l6 N! ?4 { ```6 V' W% p# Z/ w, N G8 Y
4 h* P3 E' I3 \ ] 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|