|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
~6 K* b2 S8 f! F# D" _
) r, u I" C% q- a如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
, O2 X8 n: {3 Y" _! c+ I6 v1 H" a9 w6 u& y0 F* Q
以下是创建自定义插件的步骤:3 X7 H0 ^4 o- ^ q
8 F$ D. E; U* C$ @/ x- F
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:2 P4 U) J8 L$ A' K% B
$ `& Q( a1 F+ t0 v! u: X ```; l( ?% \0 m" a0 \( T$ |: j
<?php8 \+ I, A& G: P; L& ?) ?- _! m6 f
/*$ B$ W+ {3 J" ]' B8 x2 G* o# Z2 W
Plugin Name: Site Wide Notices Plugin3 P& H% E' i; z9 ~3 w
Description: Adds a new custom post type for site-wide notices.+ H! ^. x5 z' B+ J2 m4 B
Version: 1.08 a! m: o9 } A. V
Author: Your Name$ J1 E$ `. c( F; c( F
Author URI: http://example.com8 a+ p8 T0 T7 L8 X$ \
*/
& r$ @8 |2 z) r( ?8 ]5 c
7 U& v2 i7 M* ]4 z5 W5 R" [ // Add plugin code here...: F% f+ T$ Q" \& |; Q5 b- q* T2 D) \
```- Q# z& v$ ] L) J
* V6 m/ P2 y, l. G! m 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。8 x: o( z; r# ]* y% R/ C9 U
. a5 g; B/ d9 L1 A& W
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
8 w6 q- O2 x+ x- {$ u6 J! r' s% H; N
```
( V) k3 N5 H4 p( S& X add_action('init', 'create_custom_post_type');/ D( f" _6 M# x H0 s% x( f0 v+ \
function create_custom_post_type() {
B8 i$ s1 b" r5 f& ^ $labels = array(5 p3 k% y/ V; y% _
'name' => 'Site Wide Notices',; U- Z/ W- `% |: j* D
'singular_name' => 'Site Wide Notice',- I4 B# Y" ?: x w
'add_new' => 'Add New',
% J3 p, u) _4 F8 z2 l$ n 'add_new_item' => 'Add New Site Wide Notice',
( m$ I5 r( W$ A2 \# L$ @0 l 'edit_item' => 'Edit Site Wide Notice',
" x& \) F4 x- C* k 'new_item' => 'New Site Wide Notice',
+ }2 p8 H |3 t 'view_item' => 'View Site Wide Notice',5 ~( c0 o2 H5 k( g+ a
'search_items' => 'Search Site Wide Notices',
" p5 X" c, A% m. m 'not_found' => 'No site-wide notices found',; P5 v- m+ Z8 C7 n
'not_found_in_trash' => 'No site-wide notices found in trash'
. d. Z9 u$ S( l# I5 a );
( g9 z5 d H& H5 A9 ~% `. z0 Q U1 V: x9 ~
$args = array(
" c( q5 A" @- P4 ?# T 'labels' => $labels,
7 \" J! ? r. e7 L Y 'public' => true,1 ]# q8 b; X! b! X) I
'has_archive' => true,
: i# z' q9 b9 i8 a- O' m) [" F1 [ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),+ Q; L+ g" R+ S* r6 s
'taxonomies' => array('category', 'post_tag'),
8 C c3 D2 ~3 X- m 'menu_icon' => 'dashicons-megaphone',0 T4 ]; ?3 S/ I
'menu_position' => 5,
. X5 I5 g( d8 c 'rewrite' => array('slug' => 'site-wide-notices')
p& K0 C9 z2 b: O0 |4 V );2 f; N7 i1 ^/ E! @: {+ T5 o
# K) [3 A" l* z3 f register_post_type('site-wide-notices', $args);) L) @3 H- r( G3 h7 s7 {
}
, o* [2 u, S; p9 u- e+ t ```
" V# K( A: W! n4 @1 c2 b$ I C# m6 D$ y a0 x1 S
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。# A5 v$ X: H( C7 l, D0 a& d; l. B
: Y* O3 ?* P2 c& K# @1 d* x3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:; h; p2 o* s/ w
) d% d3 W" G$ }$ w: R$ K- x
```. C, ^0 U5 s- t" w3 t3 M
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');& x, c9 Y8 ?8 |2 L. V; L4 Y
function add_site_wide_notices_boxes() {
" E" }5 X) Y* w4 `8 V, _" V add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high'); S# D7 Z$ {. G+ i. {' E$ P1 o6 g" P
}) J2 d3 L+ [; z; l' m( R+ Z* V
9 k J/ e# c- S" \
function notice_details_meta_box($post) {
$ k' v7 X3 ]1 q3 |& S' q wp_nonce_field(basename(__FILE__), 'notices_nonce');
+ f: M. v; e4 m' }0 M $notice_title = get_post_meta($post->ID, 'notice_title', true);
1 P( ? J' l' Q3 j $notice_content = get_post_meta($post->ID, 'notice_content', true);
4 m* N I3 |; ^6 u6 H! ~ ?>% v) H! j. S! ?7 q# U6 ^- ~
<p>
( H/ u% }. B8 ]3 j4 v* Z0 u' f( [ <label for="notice-title">Notice Title</label><br>3 \# ~: e! b0 T9 d5 Q, M
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
7 k6 q& I( x+ |* `* _0 s; F </p>
4 ~' A1 u! y: \9 \& @ <p>3 r) W1 E- Y+ a( [$ l3 M
<label for="notice-content">Notice Content</label><br>7 h. K, q9 F/ `5 S
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>8 Z* h0 C( |2 H- q8 ^6 T
</p>
4 L" p) d3 v9 ?) Z; w <?php
7 |# \) `) e9 ]/ n }! I( ^5 o: e) s! J L0 J
/ S3 p9 o' f0 O' e- y/ q
add_action('save_post', 'save_site_wide_notice_meta_box');) H7 P" _! _4 o1 E
function save_site_wide_notice_meta_box($post_id) {
; h% f" _+ y7 X! j1 s( H ?0 ] if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
8 C* M7 _0 [$ w3 |, \2 ?2 W return;, w3 Z# e& e3 L/ r1 V( _, g/ |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" C& m) n- g8 x& e5 G) h- [ return;
' g4 p; N6 |2 }6 C8 F+ B; P/ f6 j" ^ j" ^7 r2 h
if (isset($_POST['notice_title'])) {& d) |6 J, i3 h) e$ Q
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));' o& H' r# f% y! u* Q' F% ^
}
; Q# v' T! u* ^: `# r& O5 ` H+ R if (isset($_POST['notice_content'])) {
2 z3 I. }2 F" @( N" x update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
6 e' R" S: T+ A) L$ s$ J/ } }
1 O0 y3 `8 y& J0 g, E7 r3 y0 u }
( Z' z: V" z. |1 y ```3 O% Q( Y& ^# {6 T( B7 _
! ?7 d; I; h1 G
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
0 i6 \) S6 G1 T; G* n( V
2 y# q8 q* _7 s8 V. A+ n( L. u4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
( m+ v$ j( x9 }' I! e% d: [' j3 u
6 n+ f7 L: N# x- n; O ```
P) U1 ]1 C0 g. s |* U# s) X' N $args = array(
: y; P. g! K7 s$ i2 b6 N 'post_type' => 'site-wide-notices',
1 G! {2 E3 l% R( U 'posts_per_page' => 3,
& F# d/ G0 z5 H. @% ^5 F 'order' => 'DESC',
, x: m) e+ a7 J. T7 B6 k% x 'orderby' => 'date'
5 ?/ s! f' q( `5 G* Z) U1 o );, h0 m; r( ~, T% i
$query = new WP_Query($args);
1 y5 _# T; G8 y) n4 j0 D+ l& K O, w1 p if ($query->have_posts()) :
% T | r; H) i* A( u% Q while ($query->have_posts()) : $query->the_post(); ?>( N0 D; _6 ~$ |) k; k1 q- c
<div class="notice">
$ D: y0 h1 R( {: w <h3><?php the_title(); ?></h3>- |- L* C7 D, d2 h3 \# y! b
<div class="notice-content"><?php the_content(); ?></div>+ g; _1 Q3 r' r2 q1 L
</div>
: Y! I3 G7 @$ w( @9 H+ h/ c <?php endwhile;
& z0 C* @* G8 O! ` wp_reset_postdata();
# m) o d6 f5 P% _ \1 y1 R! Q' l4 Z4 j endif;0 u2 s7 K& O+ |% b7 H
```) M6 F7 F( }9 Q0 X2 ?% ^3 W
( T% T4 L4 z" { k0 B) W% p! s
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|