|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
3 S& H% f. Y) v3 s2 t7 n- e
2 C- x# H8 c2 r4 g0 P0 j8 J如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。! A$ n0 Q1 X) s% e' M
' I7 q. [; C9 G9 i4 w2 [$ x6 }7 G
以下是创建自定义插件的步骤:; _4 L6 {# b) w! y% V8 ?( ^6 [* m
. g( C3 Y6 L B6 J8 _6 z
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:' n c$ t! Q' s' H
) j Z3 x2 u5 c& ^2 E5 k& P ```
+ ^. _4 `, }7 i/ Q <?php* @, F/ d2 x9 b1 O) e' ~
/*+ D- H) U E4 l' J0 R' Q% ?2 }
Plugin Name: Site Wide Notices Plugin
) y/ y% C6 a, F) }4 a Description: Adds a new custom post type for site-wide notices.: X/ N/ |' W1 q" J/ m' R0 B3 `
Version: 1.0
4 H# x/ Z( o2 d' W- s- B Author: Your Name
' w D- t8 f8 |, C1 ]) c Author URI: http://example.com
1 Y8 @$ h. z/ v' ]- { */
' Y7 K9 _, F0 [& n1 ?: S' _
+ _# G* o0 P+ Z. ~ // Add plugin code here...
! E* _# ?) y' s* ]$ f# t0 q8 r ```- P, q6 k5 h0 s* d1 U
0 s) S: m. t$ M# c( m
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。% Q. A) l& u1 W2 x
: B# H$ ?# W& |# _# S: V k
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
7 h& f: t: C' s, u0 @/ p+ Y5 ]2 I8 b
```
) {6 g% ? @# E add_action('init', 'create_custom_post_type');
3 s& i, T' n& T+ h5 ~# N function create_custom_post_type() {
# _, U* k$ z! [1 i# ? $labels = array(
& ?7 `9 I3 r1 X: G) y 'name' => 'Site Wide Notices',; l5 B& t/ i! e* d! _
'singular_name' => 'Site Wide Notice',+ O4 \0 z1 h# b
'add_new' => 'Add New',' g0 c0 q- Y, O
'add_new_item' => 'Add New Site Wide Notice',
A5 s1 e+ L! x/ C0 O) n 'edit_item' => 'Edit Site Wide Notice',& H/ r" ] }$ {8 `! J
'new_item' => 'New Site Wide Notice',
0 o- u9 {& t+ A1 G5 M" j 'view_item' => 'View Site Wide Notice'," L. I0 _5 Q c1 y
'search_items' => 'Search Site Wide Notices',) ?/ _$ C( F+ L, Y
'not_found' => 'No site-wide notices found',
; l+ G; c, f( n5 c; { 'not_found_in_trash' => 'No site-wide notices found in trash'
1 b% m1 e; ~, P. q2 ^# U; T% @ );) @! }$ g4 ?# Q1 J
. t" S* O e3 c, k) l5 h+ N $args = array(
2 R" ^: g0 ~+ R" y2 _3 {% x; u 'labels' => $labels,
2 C( g6 ?. e! D% w4 {/ G: P/ m7 Q 'public' => true,+ p0 d1 h0 A7 {& x
'has_archive' => true,2 x, Q }) G, x5 A3 c2 B0 ~
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 U. H! [; ]6 V8 d" k5 O8 ]
'taxonomies' => array('category', 'post_tag'),
; b% V9 G/ \) A* W; H 'menu_icon' => 'dashicons-megaphone',1 M) o3 E2 K2 X
'menu_position' => 5,% R% l5 Y( A5 Y- a% q1 D
'rewrite' => array('slug' => 'site-wide-notices')
# p9 A& O- P. N );* k2 Z' Y0 _1 z+ v
" l9 r) Y) s8 T$ z! \# c0 a4 s
register_post_type('site-wide-notices', $args);* L5 l; V7 j) f' Y" q
}
. t3 u# l9 R& \3 X ```
- Z/ C' Y k w( t* H( P, C2 c+ S* n5 H8 i! C! x
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
3 x7 S2 I9 B5 t w( M
# Z+ r5 E# t! q4 u3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
$ n' @" l7 ~" S" q7 Y( T+ J( y3 _& n. I: F
```( ^& }/ u5 a5 M- p, v( g+ W( @
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
" t) J1 b) ^2 g- [( C- w; B3 z function add_site_wide_notices_boxes() {
1 |$ F3 Q" J% c- _: f add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' @( d* m9 p6 F+ ~! I% a }
6 H4 o& ]% L# d5 x) f8 N- {9 F7 J$ C# R8 u0 l; ?
function notice_details_meta_box($post) {
/ ? I: A5 L$ s7 K8 [ wp_nonce_field(basename(__FILE__), 'notices_nonce');. \& I! O' @1 W0 z5 S* N
$notice_title = get_post_meta($post->ID, 'notice_title', true);
# Z: C; n, v# W" }4 g $notice_content = get_post_meta($post->ID, 'notice_content', true);$ R8 e5 s2 e6 G8 G$ d1 W; u! B
?>
1 L6 a/ S" z6 y: Y1 G4 i; a <p>. j% l. H. b. t9 c9 j! c( S
<label for="notice-title">Notice Title</label><br> J! h% F( P; K3 ^8 ~* G/ E2 l; W
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">; U7 G. E6 H. m$ _) u8 C
</p>% k& @' I& N% u# f6 d/ q5 w# Q/ o- U9 i
<p>$ V5 r+ ]+ z* d/ W9 |
<label for="notice-content">Notice Content</label><br>1 V6 i/ M" `" g
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
6 h% r* r0 V) X5 b3 x </p>0 h% Z$ Y+ y" p/ Q! X" K/ P9 e
<?php
# t) h+ ]% G* d. t* ^/ g }
6 g7 x. z( d! x L2 C
* T3 [% v+ }9 O; l2 |6 j+ j5 O0 w add_action('save_post', 'save_site_wide_notice_meta_box');+ ~: f) z% o: X( B0 L' j& @+ c
function save_site_wide_notice_meta_box($post_id) {
4 b* N9 v* Q. k6 w" l if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))); J# M8 o6 ~6 y4 U7 n
return;( |) a2 m. |5 I: ]+ T
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE): \& w! v$ P% u" n- |4 j
return;
* F6 P4 b4 X; ?# i. Z+ l
9 n0 u, J6 t+ \4 e, H. t+ t if (isset($_POST['notice_title'])) {
$ ]9 Q3 w- M2 o( i update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));5 W& R9 c* X7 _$ T1 w3 o3 P2 _2 \
}
* l* k) U3 f7 [2 I if (isset($_POST['notice_content'])) {) ~/ @& a% _) L( y% X8 t! G
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));6 L! I9 o3 Z k! ^
}
8 I/ a: H5 T: v& o& L; b }: v& O: u1 {0 B1 p8 S
```
( v) n' K3 ~1 x5 O( b$ C4 g" K
5 @3 h! B: a6 G/ ]5 M6 w2 k 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
~) \+ o2 S. q3 U% x+ v" D3 I' s) X6 s& ~5 L7 G
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
$ N, d0 v3 s* R) H6 t7 K) \: M6 d
+ r6 n: V+ P1 Q! Z8 y' V u ```
0 {# N) Z$ S! F* v $args = array(
; A; W" \% n( g$ X5 d 'post_type' => 'site-wide-notices',9 F- m4 L# F. Z: n) Z
'posts_per_page' => 3,7 w% I# z' L- z0 u j/ ], Y( A' V& f
'order' => 'DESC',
% Z. w# t/ ?2 E+ v3 J7 j 'orderby' => 'date'
$ q, D3 f$ U; R& W );
9 @# e: g- p( Z7 | $query = new WP_Query($args);
" M$ f9 N8 V4 h0 A3 F1 f @- r) _ if ($query->have_posts()) :
3 r: A* j A, y& t$ M$ a while ($query->have_posts()) : $query->the_post(); ?>
7 M) o) P# N9 r <div class="notice">. c1 T! G Q7 C. b) @( A
<h3><?php the_title(); ?></h3>% O* @1 U1 Z2 k2 R
<div class="notice-content"><?php the_content(); ?></div>
& l# o* {3 e3 U- ~ </div>6 j+ Q% {& T: Z" M( P2 e
<?php endwhile;
, u0 a7 {9 R3 o$ `( P8 b wp_reset_postdata();& a i# W; h }- s( X
endif;
8 Z, L8 Y n$ i0 X: G# V" s9 l2 p ```8 o! ~3 i4 q6 ^
L0 y+ A2 t5 B; p, n0 L+ \& E7 \
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|