|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
2 l6 P/ k9 G; x# H, U: C
p1 s; U d; b6 k: {0 D4 n' V如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。% ~& a: B0 @5 ~$ [6 _8 \
) i* V. T5 y6 o" i8 r以下是创建自定义插件的步骤:
; r; x3 {, w* J8 v
* N0 k$ W9 d' L) a9 X$ }1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如: } o5 C) Z& P/ P/ `' o
# Q0 I( _: L% E( G4 U6 F9 b ```$ u& c* ^+ p" K3 o
<?php
9 b/ y' m2 S/ z7 p /*1 U3 L1 N( J2 J; u3 T
Plugin Name: Site Wide Notices Plugin4 z; I8 s( G& m" z
Description: Adds a new custom post type for site-wide notices.: q+ V% U) B# R& Q1 I# Y2 R
Version: 1.0
1 c% J8 ]+ ]* P0 a2 B3 t. X Author: Your Name% y3 k7 \, M( J" l- b- }
Author URI: http://example.com
9 E3 u8 q' K( F2 k: J8 ^3 q */' d: ^8 w6 d! L& o" z0 q
8 i9 J6 G3 c; t- ] // Add plugin code here...
' c$ F9 s- e H ```
/ Z; U2 v8 ?6 o
7 D3 E4 l- f, m, z! [0 ~! Z4 k 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
7 Z1 X0 C! H: S1 l. {- W; `# `) [0 d" H% @ |
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
+ E% N# i2 W, ~+ Y' e! m' R2 z% z# _. `6 g
```; d7 l" [7 k4 `' n3 H4 Q
add_action('init', 'create_custom_post_type');* {4 e, m/ _* Z+ T3 T/ D% E4 M
function create_custom_post_type() {1 K, u% V; o) [) R8 s5 a9 I7 n& w
$labels = array(2 r# g# e+ R; Q& H6 V8 E Q
'name' => 'Site Wide Notices',: z7 G% t6 y4 n6 ~4 p, z$ v; |
'singular_name' => 'Site Wide Notice',
* b! F+ b* E2 X 'add_new' => 'Add New', N% {( G5 c8 W& t. J+ {- F
'add_new_item' => 'Add New Site Wide Notice',
1 s9 B& f2 \9 j4 T) F0 ` 'edit_item' => 'Edit Site Wide Notice',
. e( X! J8 t6 b2 A 'new_item' => 'New Site Wide Notice',
( a* g& x' E5 O4 ] 'view_item' => 'View Site Wide Notice',
; e! n6 q7 R, y- ?1 `' I5 Q 'search_items' => 'Search Site Wide Notices',6 ~# a8 r0 Y0 S
'not_found' => 'No site-wide notices found',+ p$ Z- J% b8 z _, H; G
'not_found_in_trash' => 'No site-wide notices found in trash' g1 Y1 G% k: D3 \9 A. x0 `
);2 M& `8 y& U7 p/ ~. Q' l c
7 E: y; U9 n- [3 B3 v" ] $args = array(
9 i- D) R9 ]8 m; z 'labels' => $labels,7 E/ ]4 I+ l( }8 d; O' B5 M: j5 x
'public' => true,, a F4 `1 o. N3 `- S0 D3 f* u
'has_archive' => true,) Y: r. w# ?! {
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' N- h7 T+ E+ ?1 z% I2 d
'taxonomies' => array('category', 'post_tag'),
& f) t$ \ | J' {2 E3 e, b6 M; x) U 'menu_icon' => 'dashicons-megaphone',
8 `7 x+ b; B5 H1 O/ R+ g- w 'menu_position' => 5,
G+ k: I* I1 \5 j 'rewrite' => array('slug' => 'site-wide-notices')$ w/ D: h/ l2 u
);1 S9 C. x; }2 B0 d
' k) e- j* J; ? j
register_post_type('site-wide-notices', $args);/ @# n& ~7 S- V* O7 r
}
$ \1 {8 u& [6 @8 w ```
3 Q/ A% M1 r3 d( o& F7 O; Z: w) I$ E6 S5 l9 t. ~
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。) t+ m6 D2 J* z2 }* T
6 j6 N) k- N9 u1 h8 M
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:/ Z6 A$ t' `0 p S! K5 S
, W: L, c, x: C$ L: F
```% d6 Q Z( {# T
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');! a3 W4 ]9 i" X/ [* w% ]4 l
function add_site_wide_notices_boxes() {/ }1 A: j" e/ g$ [6 y! B Z
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');4 A1 m3 c: v1 t! H1 {3 |0 t
}7 T* \( R0 L" ]* n4 }
# b% o0 s v+ Y) d1 @; Z4 f
function notice_details_meta_box($post) {
4 J+ i# I, T# \( L+ X2 U wp_nonce_field(basename(__FILE__), 'notices_nonce');; h5 o9 N. _" q' G3 l
$notice_title = get_post_meta($post->ID, 'notice_title', true);
' c$ ^( ?" |7 M8 k $notice_content = get_post_meta($post->ID, 'notice_content', true);% I) ]+ C6 G; C' m$ y/ m
?>
1 K" x# i6 m' r <p>
K1 B/ w. x% K8 N9 I <label for="notice-title">Notice Title</label><br>
+ M8 [7 e$ u @/ H3 k <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">- P i( l7 n. ^& p1 z
</p>- O }, R2 [$ n/ B) s
<p>8 u: [& {; A" V* f. W/ J
<label for="notice-content">Notice Content</label><br>) Y" z" D' Y5 l9 f. \& W
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>6 W! s: X# D k# K8 n! f: @
</p>) `) a6 y; M/ Q0 [1 h
<?php
4 [9 n1 [5 ?+ [; A3 w }2 ^5 t8 h2 W- z" u! N
: e9 }6 q4 U) [9 E+ K. C add_action('save_post', 'save_site_wide_notice_meta_box');
! s' [* y! I# K: ]& Y' X7 y& D* T function save_site_wide_notice_meta_box($post_id) {
) s! u* q5 Y, g- E" w' z" [ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
/ A; W1 m" G! C6 U" D6 S return;
1 M& `- h ^& n1 }) T8 t if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)& V$ m$ n" R6 u4 p' I' Z+ @# E
return;' z* @2 |9 r' a" r' o1 m
" w' u# B& n; R8 _: R+ g" R if (isset($_POST['notice_title'])) {) Y s6 o: }" d
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));& [7 u5 ]/ J' |6 Q
}1 ^8 n3 k+ h4 o% a; }
if (isset($_POST['notice_content'])) {$ D h, k6 P) @7 V& F1 G3 K
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));+ z0 S5 K* }& }# A$ C
}
3 N) \ {2 Z" f- b }
: |3 R* I5 D0 V4 w# H3 ?8 `3 d ```; J+ u. A$ O/ B2 A8 M
3 W8 j/ c$ Y1 Q& ~3 n; c" D 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
; ]- U% v# m+ x8 J X. O8 p" o
\/ M( w' o0 G9 `& u* b3 U4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:# q! y4 K$ o+ M4 j! I
3 b7 c$ [! ^0 T4 Y4 h ```
. y7 o" W+ S; d3 Y2 G( b V $args = array(
0 Z9 M8 h& v5 s o* Y 'post_type' => 'site-wide-notices',/ b2 f+ S+ |! d4 ^
'posts_per_page' => 3,
" T& Y* O x- c 'order' => 'DESC',3 j8 i2 o! k0 J$ P6 J' Q$ U
'orderby' => 'date'
6 t9 u6 v4 P3 j- T) E );3 s3 d5 L% j8 |) |$ B) ?: X; r/ ^
$query = new WP_Query($args);
" R: @2 X9 b4 h$ L if ($query->have_posts()) :; @, b @5 c9 i7 N4 j3 L
while ($query->have_posts()) : $query->the_post(); ?>
# Y6 {: Q2 g! {/ w9 C8 O <div class="notice">! L8 Y7 J+ j8 X. }" _0 A' N
<h3><?php the_title(); ?></h3> J ^' ~ _1 J$ m! i) P+ ] S
<div class="notice-content"><?php the_content(); ?></div>
7 i0 |. ~9 I5 r% m- Q </div>
" p& g. t, T- L# ^/ X <?php endwhile;
; r: v# B( z3 O/ r1 a' f2 e wp_reset_postdata();' Y3 a0 x$ _6 C9 ^( F
endif;% f0 k% G2 g% Z2 {% a
```1 F. l) [$ E; o, w% r7 |% W& {
& v; r9 S" {, ]6 J+ E1 w6 C4 M+ O
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|