|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?+ s, f2 u$ D9 A$ d# [0 R3 L" j
; \, a y# V) @4 j- Y2 ]. f/ t如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
6 N- i% r. r& V, w9 f3 J' }* U2 w
以下是创建自定义插件的步骤:8 ^5 v/ S8 M9 [/ R" ]" i
9 D- A( ?0 v8 M+ {* }1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
" a* x: q. V) S; t% m5 V6 C, C: c3 D* V' H
```; A# O2 I& H8 n1 n% N V' e- f
<?php3 f1 k0 [4 R# I$ X* j) _3 T
/*
: Z0 P; w1 Y1 @$ B$ Y1 k; q Plugin Name: Site Wide Notices Plugin4 H. {& S& O. S8 ~) E t
Description: Adds a new custom post type for site-wide notices.
x) |# \6 O c Version: 1.02 `) X: l8 d; X# {# _
Author: Your Name9 @" d4 s Z# R) J9 Y' I
Author URI: http://example.com9 R) O8 O" `- s) x( `% ?) \8 \
*/1 J* U2 f: |: m" v
# _2 z4 ]) I8 A B6 i
// Add plugin code here...
- H& Q( L! Y8 Q3 i! R& V ```9 e5 f0 \: {6 r0 m4 S
& S6 O3 e) w6 C5 M
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。5 @- _. [; Q" }% b$ Z# H) e) f
7 q: Z; M" z3 l& l) X2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
% Q2 d! \. g' c e! G! Q$ W/ C. L% I ^2 g
```/ x$ ^ [ Y4 |0 A# [
add_action('init', 'create_custom_post_type');* k7 g" Y/ N4 h- u; b
function create_custom_post_type() {
/ Z7 ?! P9 k5 X $labels = array(
, r- m# \9 m5 p" r8 a- A# w 'name' => 'Site Wide Notices',
+ _- l6 x1 ~5 j' d+ |/ t6 S3 L 'singular_name' => 'Site Wide Notice',9 U: h# ^' K: ?! X# v. W6 y( X3 a
'add_new' => 'Add New',
; x% @$ ~* \1 k$ I9 v 'add_new_item' => 'Add New Site Wide Notice',
' k1 e6 [: u; u' S: | 'edit_item' => 'Edit Site Wide Notice',% X7 @3 e" s% X: v: p' G% H
'new_item' => 'New Site Wide Notice',! g9 ? T2 g( }" q. G0 f1 L
'view_item' => 'View Site Wide Notice',0 ?4 o; D) z6 L( w7 t
'search_items' => 'Search Site Wide Notices',
7 M! g0 L0 f# I0 P+ I- k8 ~4 t 'not_found' => 'No site-wide notices found',
+ b* r7 C' _9 {2 p7 k; H 'not_found_in_trash' => 'No site-wide notices found in trash' L0 v% Q4 ~6 j0 L
);5 W# G1 k( ^& m X+ p7 n
" o3 E8 X) q, I( K) j( R6 B $args = array(
; { `3 X% C% W 'labels' => $labels,
3 C! @+ z8 e: t8 q$ ~1 k) K 'public' => true,
* D0 h O, E& Z U. B 'has_archive' => true,
; D+ M+ ]1 Y9 {- Q; [ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
! o- n' L! M8 \: y3 z( Q5 B- n/ i/ L7 q 'taxonomies' => array('category', 'post_tag'),$ O! }* m7 x5 n1 N( J
'menu_icon' => 'dashicons-megaphone',
) ]2 ~3 e! K6 N$ F 'menu_position' => 5,
* {# o" B# t |1 y/ y! K* _% M 'rewrite' => array('slug' => 'site-wide-notices')
$ u. \0 t! s& q" w7 h7 e( i& d$ h );
( w" W4 H+ v, U% O3 F' a5 \# b! L9 T3 e+ i& R$ ^
register_post_type('site-wide-notices', $args);0 h& ~& `: Y: W' H9 }( w
}
* a3 ~5 ^1 P' t* R ```
1 s7 g1 v d) I! e. M$ H
0 [; F- O4 y; X4 \) | 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
, }/ {- ?# a' N& R- y7 h. m# }! r6 j; S I5 M# ]. e' Q/ U6 ~( Z0 d
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
* X& [) }3 q6 B8 y {3 H. A6 l) J6 M! D0 Z$ S q3 g Y
```
; Y- H0 g% }6 L! Y/ P add_action('add_meta_boxes', 'add_site_wide_notices_boxes');& h1 {" j# f& a P, c6 y
function add_site_wide_notices_boxes() {
$ {6 t0 `' d7 t- G# ] add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' \& E4 ^2 O4 i d2 P1 F+ F8 b2 C }9 g4 P& a2 a" ^4 A5 A4 U" H
5 |% m3 ], h% g/ Z. \/ D2 v3 ^& W
function notice_details_meta_box($post) {5 I! Z2 K( `& `* {/ B
wp_nonce_field(basename(__FILE__), 'notices_nonce');$ v& c: ~! H9 P' b& b# ?9 t2 Q
$notice_title = get_post_meta($post->ID, 'notice_title', true);
9 c& P' M1 |9 g; t $notice_content = get_post_meta($post->ID, 'notice_content', true);
& f+ w7 X6 h) l& C6 W+ ?, b ?>& J; {3 |+ d7 T, D* \0 N. o
<p>
% Y+ R# f1 p- O: t5 Q$ n: d" A <label for="notice-title">Notice Title</label><br>
6 R% T# g8 W; b5 J3 \ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
8 G0 _4 r1 r: Y2 H </p>
" |7 q. t3 t0 U5 E+ w: ]! w <p>1 g- ~0 R, ^( A7 c+ z2 n* M4 N5 ?2 T* j
<label for="notice-content">Notice Content</label><br>* I0 A0 Q* t5 _, d% v, L0 V, ~9 ^
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
& v3 y6 `4 U, B </p>
2 g' n7 n' [3 X4 \6 N <?php
; w) R k7 ~6 z( Y" b }# N4 P$ U, m6 A
C" Q9 s: C5 ^# Q1 I4 p add_action('save_post', 'save_site_wide_notice_meta_box');
9 u% z/ w5 l: A+ ] F8 `% A2 M function save_site_wide_notice_meta_box($post_id) {5 ?9 D" j, n- S4 ?# b: G; P
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
2 v* O. y. L1 \) ]6 n; D) @ return;3 d, `" Y# C( \2 o( O! U$ A2 t
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
3 m( X& J# ~( ^' F return;
1 ?' u r- W) ~* ]! l) v* E }5 H! {
, j% x, y3 I: e5 r, F: X- S if (isset($_POST['notice_title'])) {
6 q* _0 k) N8 y( T, T, ]& y; C/ Z update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
- r5 L; Y- L4 {& ]/ i! Y: C }
4 `) u% M* t7 |8 R if (isset($_POST['notice_content'])) {
# K& c; U Z: o* @& k- I8 C/ s( \ update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));- }! [ @& \) o+ T( e2 _
}2 O0 |: g4 _1 u( V Z
}1 U$ K @ b$ I6 b+ X |
```
) J+ }* I% ]3 E# u: u$ L
5 [3 ?4 m1 P$ L# x0 F 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" }) a; t% D" [9 Q8 }; `9 M! T2 p; X2 u& T
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
7 P+ c" B: {/ P9 s( P$ U9 D0 _1 _
```
3 R& X b, Y5 x) ]- F $args = array(+ R d0 I! ]' i. q
'post_type' => 'site-wide-notices',
; n, |! K2 d, v0 A- b4 H. _ 'posts_per_page' => 3," W0 \1 I, E, M/ _0 K3 h4 f
'order' => 'DESC',* I2 C3 V" Q! w) t0 T$ s( z
'orderby' => 'date'1 @3 j1 `9 u+ y, a
);
$ ~5 `# U8 X. w7 [8 ] $query = new WP_Query($args);
5 F5 E9 u s8 b+ p2 |/ j$ D' C if ($query->have_posts()) :
# C6 z6 k$ Z& D while ($query->have_posts()) : $query->the_post(); ?>4 ~/ y2 n( _% |# B$ O
<div class="notice"># _* ~' v% j d. k
<h3><?php the_title(); ?></h3>. C, `) k0 V( I
<div class="notice-content"><?php the_content(); ?></div>6 _. M2 M4 n: v$ W( h0 l3 j
</div>
, c& S, U. U2 r9 n/ c9 a: L <?php endwhile;8 b. {$ x. q# R0 M: F
wp_reset_postdata();
7 r ^( a5 G& o8 K7 u endif;8 k/ s7 F- Y) C! M( o' Z
```
( T! f" R8 {6 _) a- I
) P7 k, _$ }+ v, C% W8 o 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|