|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
8 x7 N* i, v3 N* v i
# C4 P- R4 @$ r1 {3 ~3 z! R" B" A如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
9 _6 d- q I7 d$ U- L9 J9 z |& V o* U' @* v2 ?; I
以下是创建自定义插件的步骤:0 B+ S# A* F# H5 X3 q4 C# a
! d, k+ S9 ?. a1 w! `1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:+ E! T5 B, g" N! v, g! B
7 G# P& _& H% X, ]( h0 }
```
. F6 C$ n8 [' \/ A& b <?php G9 _& ^4 t+ [0 |' X5 C% k
/*
- t" \; A+ v2 W4 p Plugin Name: Site Wide Notices Plugin3 X% a5 P" A @+ J# W& ^
Description: Adds a new custom post type for site-wide notices.4 w$ I3 v5 I' d- `( g3 Z$ P, |
Version: 1.0+ A9 i/ k0 Y {0 {* [
Author: Your Name& b [! V2 c% h0 c3 `
Author URI: http://example.com
* Q j' o$ ?4 X U */
9 Q& _7 \. c' C5 l, G |% C) W: L1 j* \
// Add plugin code here...
/ `1 d% A2 t. n$ x1 d& C: [ ```- c x- L$ @4 l3 g# J2 v F
& }! F+ r4 d* T 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
- k0 g- S# U. e, c0 n" u+ Q$ C* W; T+ h- ^+ {1 v4 V. v
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 X; D8 l9 G9 P9 S
" E7 N9 a% N6 J) Y! a/ k/ A) g5 _
```
! o: K) {1 O$ q- A- C Z- K, R add_action('init', 'create_custom_post_type');* z4 t# { d- A
function create_custom_post_type() {% ^% x4 H. A8 _0 n
$labels = array(
9 q1 ?0 l: |) h& w* J, D1 f 'name' => 'Site Wide Notices',
; e" I& ^. G+ F: V7 i: n 'singular_name' => 'Site Wide Notice',
' ^! H8 @9 i, M5 e: @# r 'add_new' => 'Add New',
* f8 |6 d; F1 F/ U 'add_new_item' => 'Add New Site Wide Notice',, o4 v. `3 a5 a9 D( T. ~0 W
'edit_item' => 'Edit Site Wide Notice',7 w9 h2 K" R2 A* ^4 M
'new_item' => 'New Site Wide Notice',
0 t& h4 A5 ^1 a3 [ 'view_item' => 'View Site Wide Notice',- _( [5 @' l3 g: _- l; X& V$ {
'search_items' => 'Search Site Wide Notices',& Z) y) u- K% G c0 w
'not_found' => 'No site-wide notices found',
6 j4 u: Q2 h' w( k 'not_found_in_trash' => 'No site-wide notices found in trash'- r5 B: s6 v, S6 S
);$ o6 y/ @+ \$ a3 D t9 z2 U
0 L$ N9 Q. {4 c# B7 v: j4 M+ V
$args = array(
2 R7 \! u6 ^7 V: O1 \; c" l 'labels' => $labels,
: o2 b2 I4 A ~! `, m, A' }1 O 'public' => true,4 Y% a, S6 \' z, }1 @" @9 _
'has_archive' => true,, k( h, ^9 l2 ^5 T/ R* D1 y; n
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),- J; L4 _/ {& x+ O( O: Q' t& g
'taxonomies' => array('category', 'post_tag'),
6 c. q1 D% u+ V% t 'menu_icon' => 'dashicons-megaphone',! ?6 T S7 A( s" [! N. H9 N
'menu_position' => 5,) A$ F9 o& T; c$ M* t$ X* u1 a
'rewrite' => array('slug' => 'site-wide-notices')( e/ H e2 S, D7 j
);) ~" y1 W' K* Q5 E9 N
# ?. r' g; B' b' O7 u! ~ register_post_type('site-wide-notices', $args);- J+ ]7 E! o) Z# x; x# b! c
}& V/ I2 v! _, g( b0 Z @' M
```
+ T7 [$ T) v9 U2 V
, \: D- P. x9 f1 g; k9 e 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
* t$ b; ~ S5 T2 J' v% z
7 X7 }9 t- E) |* x3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:0 B$ i$ e; B& L9 Z) y2 W
9 n) i2 |) t# u; q
```$ _) |; R# a8 k' T: Q9 R& @
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');3 X) ?1 V# ~6 W
function add_site_wide_notices_boxes() {
6 m$ H! E1 Q9 p" N4 F add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
" u/ ~2 r: s u }7 j: [/ x$ N$ x" Q8 P0 ~+ A! J
6 Q0 h) i' g! _) R0 a) l( r( G# a' Y function notice_details_meta_box($post) {
5 \6 E3 D. B' i; i9 W. U. G9 E; | wp_nonce_field(basename(__FILE__), 'notices_nonce');
% J: x6 m$ I9 t/ X& Z( m/ k $notice_title = get_post_meta($post->ID, 'notice_title', true);
* B9 y6 h8 i$ M- {' L; ]+ ? $notice_content = get_post_meta($post->ID, 'notice_content', true);
8 o5 X) R$ v( |( ~ ?>
" e Q& f) l8 @2 m7 z9 p" } <p>' U$ |$ K5 a3 @7 o
<label for="notice-title">Notice Title</label><br>
2 a. D1 {$ }3 z* I1 ` <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"> b h. ?* s0 \7 Y
</p>' J; J1 m. [2 w+ `
<p>
- P$ E- h2 h; b$ R <label for="notice-content">Notice Content</label><br>
! @: m3 U% i% f <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
* y+ {! ~( C9 f/ P0 {; L </p>
" f8 u7 J* C; U7 C3 v" t: o* J <?php
" Z! q. v( p& r: V) N }& l4 u# q2 Z/ G* D; E
$ q |/ i2 ?+ J7 I' x" N' P# v add_action('save_post', 'save_site_wide_notice_meta_box');* @9 @/ ~. W! j# {$ d7 ]
function save_site_wide_notice_meta_box($post_id) {7 \% G, X& V. }' c" R
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))/ q0 |9 h% z$ p# K. d8 w, G
return;/ \" A8 o' [6 v/ v
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
( h4 L4 k4 y$ l/ j return;
9 ~( c/ ?( f+ B) C# v% H* q) |9 `4 ~, k5 @+ @
if (isset($_POST['notice_title'])) { \, R( @8 Y) f# Y# M
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
! P+ E& Y, V E ^/ [8 A2 Y" \" p }
: H/ ^: L3 u% r, t! g if (isset($_POST['notice_content'])) {
8 c. H- Z# e& o7 \8 D update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));& O8 y% m, s% {% W p# E( l' p
}3 [% t# Z, z( |+ E8 |0 y
}
" x4 V* d; f8 ^1 p1 O ```
' ?6 b* G* ?8 i& t7 p% _% I3 o! I8 L
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
7 c) V: `4 n, j8 m; z1 a% l7 ]
5 I9 ~1 H, u% a" n j4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
$ l7 ?# a1 p9 ~+ B
; B$ e+ B2 |' W. n$ b- y ```7 T0 S5 k9 s8 Q" N. y9 m( V
$args = array(
7 J E" p$ f5 {8 J9 X 'post_type' => 'site-wide-notices',/ N, N! A- O& S1 r- f
'posts_per_page' => 3,
' Q3 L2 r. Z% U* G" Y 'order' => 'DESC',
8 ]+ I, k- c& h 'orderby' => 'date'
& Z# U- r. q; Q7 X );
* `1 F |# W! u6 E! t( s $query = new WP_Query($args);
( Y+ }# D4 \8 z9 v0 k) [ if ($query->have_posts()) :
# {. @& e4 K6 ?2 T# P, I while ($query->have_posts()) : $query->the_post(); ?>
& D- @( C, o0 L( H- z <div class="notice">4 y! K* l! U; q1 [. r$ D
<h3><?php the_title(); ?></h3>* c) u' @: R/ R
<div class="notice-content"><?php the_content(); ?></div>
; n9 I5 C8 X. w4 ]7 ] </div>8 x/ l' U% n! A: F
<?php endwhile;/ F( w( W9 O0 {6 {( n
wp_reset_postdata();
9 U9 D9 k0 b% R& r9 M: r0 \* G. X% E: n endif;
: l4 r" |7 V& ^0 _' N ```
- D E6 S3 Y% B% n& c9 B8 i7 E4 k4 p* L) W) M
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|