|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
1 Z3 h& ^# Q6 F3 f% T
2 k4 I" i$ Q' O! V如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。* q S2 G! a# F
2 {. g4 e# ]' X. W4 U以下是创建自定义插件的步骤:/ |' T2 Q, q9 x! a- E9 x( c$ p
: \2 ~, P0 f3 }1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:) e- Y7 i9 d- W* P0 h( }5 {: {! Y
8 l2 M# s% o6 p3 `% `- M4 c. j
```
$ ~# N6 M: v# V <?php
" ]0 _3 S8 b2 x! R) v /*
3 Y7 n3 w4 D; i# w Plugin Name: Site Wide Notices Plugin
$ Z$ G% h% T2 _4 t8 E! c' A4 { D Description: Adds a new custom post type for site-wide notices.6 B! q/ E2 R3 `/ w- a- @% q
Version: 1.0
9 B, D" n3 o# |1 g; Z Author: Your Name
1 y0 ?. T* g% P Author URI: http://example.com4 U0 B- s; {/ a; A3 `
*/
2 {1 R4 Y, |/ S1 W X6 H1 @/ @2 f# g; x) B+ x
// Add plugin code here...+ L8 c; t, r( A+ T) q9 v7 z
```
, v$ j c) J h+ R2 M2 ~6 b j
1 O/ X8 W, B# Z( q0 W- `2 l# `: V. E 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。2 U Y* m5 B, M3 l6 u' ?6 R/ |
4 E; \) \4 T+ r
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
, D! k$ ~/ B. K, ~9 ~1 B1 z% v5 X7 R) |
```% y! T7 \9 q- r* Y( V' o" _
add_action('init', 'create_custom_post_type');* [- i* O1 N9 w+ S
function create_custom_post_type() {8 A0 E; @$ N1 u, l0 W3 ]- p5 R' T" L
$labels = array(5 B% v- ~$ J+ H& M4 Q; v* g
'name' => 'Site Wide Notices',
; P8 O, n" K' v 'singular_name' => 'Site Wide Notice',
+ i% D2 c% ]4 X) y 'add_new' => 'Add New',, p4 @6 Y: E5 V% y1 e
'add_new_item' => 'Add New Site Wide Notice',
5 x2 W* e3 Z% Q" `& g* U5 C0 @ 'edit_item' => 'Edit Site Wide Notice',& C" G1 n' a' i& z( s& a
'new_item' => 'New Site Wide Notice',
6 u1 t9 i( m4 p Y2 Y" c( r 'view_item' => 'View Site Wide Notice',
- K4 ^% U1 b" I n o 'search_items' => 'Search Site Wide Notices',7 v$ H( ?! [' U' a0 v7 a
'not_found' => 'No site-wide notices found',3 r# s. n- }" X' X+ x) H
'not_found_in_trash' => 'No site-wide notices found in trash'
% ]" _- j' G6 s: K6 } Y( m7 { );# I' f0 b& B& C6 o
9 L3 @+ W6 B; D. o) |# e3 a1 H$ ` {2 _ $args = array(2 t, u# |8 E# O$ R
'labels' => $labels,0 I0 [2 V; O1 `0 X/ { f# M! D
'public' => true,- _* h" }6 d* k0 r! ~; t
'has_archive' => true,) L& W6 K) I7 p% o
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'), v k" G3 Q$ q( d- M- {& U
'taxonomies' => array('category', 'post_tag'),
9 [/ y6 z- F; |! t* B h1 y; N" Y 'menu_icon' => 'dashicons-megaphone',& c* G* ^- k! B' ^: ?7 R3 N- |
'menu_position' => 5,
! R& A% [/ {6 T 'rewrite' => array('slug' => 'site-wide-notices')
6 D$ t9 u) S# S, y- o6 M! i );
! C/ q+ @0 ?0 D% p- i
" J8 z1 E; F/ | U register_post_type('site-wide-notices', $args);8 j. b2 T( q4 f
}9 u+ [$ U( r5 |9 b* O
```) L/ L8 I* \" j8 U* x( Q! ^
) G5 s$ m: b2 ~" a) W
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。* u, r. o7 Q7 k/ l
( X) r: |5 z* q6 J
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:4 _/ Y* }' H! s; e; l# T6 A
0 k) B# j* J% w3 M
```/ \6 f$ P: o/ c; L- d- \/ x- B# }
add_action('add_meta_boxes', 'add_site_wide_notices_boxes'); b$ x/ N& j* Z- y
function add_site_wide_notices_boxes() {
9 t! A" H' ^* b7 d" G: k add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');" D2 l5 M" U' ~" C
}1 \9 Z& g% V- G. w4 n' n
0 q# h$ X+ }) a6 A
function notice_details_meta_box($post) {
# U' l9 W$ }1 ]) n1 A wp_nonce_field(basename(__FILE__), 'notices_nonce');
9 Z3 B8 \5 w! m( f% r0 u* T $notice_title = get_post_meta($post->ID, 'notice_title', true);
; p7 P% e: r x7 a $notice_content = get_post_meta($post->ID, 'notice_content', true);. k+ e4 p/ F! e1 v) D+ {
?>
7 \& H/ e7 A+ [/ b6 m7 R <p>6 G; G- S. W! R9 d# T/ d4 j
<label for="notice-title">Notice Title</label><br>
/ r9 K7 ]$ i3 w- ]& @0 @0 |! w <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 m; H2 {/ S5 C2 r0 G" [. r0 k
</p>
4 J9 |& |& x( n; E <p> j1 M& G d; _: N
<label for="notice-content">Notice Content</label><br>
' f& ]" F2 c- q9 f <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
, ?& {2 N7 u5 c( |8 y1 t j& g </p>
- m; v8 d& |0 Q* ?- \) P <?php
, K: c5 K) N" Q) d' v }
$ q) D# w0 k3 H U& Q+ k4 v3 X( T5 z7 }8 x
add_action('save_post', 'save_site_wide_notice_meta_box');. A7 z6 W# \+ u* m
function save_site_wide_notice_meta_box($post_id) {
& m3 E, D. \. v3 K. M Q0 M# Z9 D if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ n- s/ ?4 s9 q5 R& B8 h return;1 X3 _' ?9 ]+ c: ]7 G2 s
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
! k$ m7 Y7 Q% N( Y n; W6 n' ~ return;
6 {4 Q! D- u1 }* o4 m! T& L. C' R8 }- l' w2 C
if (isset($_POST['notice_title'])) {0 k$ y4 T0 _4 S) c) }- f6 |
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
0 r4 I! v/ A9 n& |3 s. P }# v6 u* ~ P& W; F3 a* K g
if (isset($_POST['notice_content'])) {
3 ?9 O2 P' k. G/ S; g update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));9 i4 x7 ~& K5 h- ]* a5 Z
}6 t( i0 `4 `1 a
}
& B: t" c% ]& B1 G8 x ``` J9 Y1 F. j% D
+ x7 _5 i& y1 e0 b% |3 h; p6 `
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。5 f/ {# C3 {( M' J4 y# e
; Q) n# _. q( \8 K
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
7 J d+ T+ L- Z# l7 M: G. t0 o8 { X) @ P, ]% I' c7 [
```6 v( n/ I" T7 i$ w4 d4 ]( [$ \( X8 c1 K
$args = array(& m. `7 z5 |! D! h
'post_type' => 'site-wide-notices',3 J/ f* \/ d! C
'posts_per_page' => 3,
6 d% p$ b1 j% c. C( l2 h) ` 'order' => 'DESC',: b% d/ F' X% K3 q
'orderby' => 'date'
) R5 j" {9 k/ d& w );
; _, ` j- I* d) U' ] b $query = new WP_Query($args);! v8 R z0 M- ?" _* e, b7 L/ w- r
if ($query->have_posts()) :
# J; w3 f+ y/ j, E" L( z2 m while ($query->have_posts()) : $query->the_post(); ?>
9 {0 C5 k; H' v" d& T! Q' N7 X <div class="notice">4 |, O+ t* v# o% [5 P0 f" R
<h3><?php the_title(); ?></h3>
+ @$ v5 Z; [0 D7 x5 ]' I3 O <div class="notice-content"><?php the_content(); ?></div>$ t% u) L: C) a7 t6 N/ a; i
</div>
$ S( Q; F; v$ O. F7 k: n$ i" [ <?php endwhile;5 a s4 u# I8 @: ?7 b
wp_reset_postdata();
8 \: K) O6 n: i3 I$ m3 u5 B endif;
" w: J: W" z' C6 k* } ```! h9 l5 X/ B) k' z7 r
% Y% C$ |* `/ f. ~5 T
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|