|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
3 D0 a |, c( B/ J c w( ?) t8 W) n8 L( |8 l
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
2 y, a* k I: w0 ?7 R- z3 l5 {! S6 C# r* q
以下是创建自定义插件的步骤:
; O# V- H9 a$ R0 S s0 b. [. D+ K7 `( S5 D1 l
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:2 \. a" H1 F, }' K7 M: R
$ n3 ]# N* x* t) P! a( W4 }, Z ```
' R+ F* n; b4 m <?php
3 A+ W; |# S# ?: y' ] /*- h& k) w4 J& A9 C& W! `
Plugin Name: Site Wide Notices Plugin
" `5 m) T" }9 v5 v Description: Adds a new custom post type for site-wide notices.
* r l& H' N1 p+ V( B. o Version: 1.0
8 d4 Z" R9 t* H& r; v2 V+ W" r Author: Your Name
: W$ B* n! E2 _/ s( Y& k" a4 G$ g Author URI: http://example.com" G+ b" s5 H' G; L& {
*/% U6 |' P* d! H# K, B. V
1 w* f5 f7 K: y! C1 B; }. E
// Add plugin code here...6 z# a! t/ @6 }0 B
```7 s3 q% \1 T$ x! }. H
" G* j X7 x9 ^' K ~) i 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
9 `% k% G2 p2 f0 Y% b
$ l5 y' t) s, J) ~2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
S1 ]+ W' I" K6 a$ g) I. c$ Q7 ` m' k/ ~7 j! n) p) C
```/ _& ~5 Q- Z+ M- T( h
add_action('init', 'create_custom_post_type');8 x0 n7 q4 a& A* p! y/ d. R
function create_custom_post_type() {
! F; u* }" c/ y% ]8 Q3 R, L' @7 I, I $labels = array(
% r& V8 ^! _; U9 } 'name' => 'Site Wide Notices',
\8 I8 N; {$ ]8 j! b) ]6 Y! e 'singular_name' => 'Site Wide Notice',
. E# E/ p0 f2 [& f; m# i$ a6 v 'add_new' => 'Add New',
. e+ q$ b* r) s, i 'add_new_item' => 'Add New Site Wide Notice',! b* o! M7 a& e
'edit_item' => 'Edit Site Wide Notice',6 v6 g5 c( b5 g2 k* i$ p8 b+ I
'new_item' => 'New Site Wide Notice',2 x2 ^1 B. j* [$ @: K- D* [
'view_item' => 'View Site Wide Notice',
/ D; h0 s9 n h8 U, i 'search_items' => 'Search Site Wide Notices',4 \% D* a/ a5 n% ~
'not_found' => 'No site-wide notices found', v; s! C L, u+ g
'not_found_in_trash' => 'No site-wide notices found in trash'
# B/ J9 D+ d7 J$ @ _: O s );- w, {" z& F3 Q. ?: d5 e( g
( H* J, b/ U. i
$args = array(
/ d' s7 V* u0 j/ u3 L( H 'labels' => $labels,
% N" Q6 i" z: ]5 |) X 'public' => true,
3 h' b' A. L& L 'has_archive' => true,
2 F7 _$ ]3 E; {; \/ j 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
. ~$ M) N. A$ W0 S m8 |# F E6 f7 P z 'taxonomies' => array('category', 'post_tag'),
) x& v) P4 a, w+ y( }2 g* w8 y 'menu_icon' => 'dashicons-megaphone',
6 O: }& U) O* B3 m9 t) n 'menu_position' => 5,
+ v2 Q) L6 u8 f% A1 m8 q 'rewrite' => array('slug' => 'site-wide-notices'); U2 }5 E" U: K/ I; o
);
6 ~. @ y# c6 S1 c9 K f" R# T$ a1 V8 ?6 A; d% m
register_post_type('site-wide-notices', $args);* g3 E: \1 ]+ h0 r1 {
}
! H! W2 t2 L6 H% c1 P5 r9 n" y5 q ```
3 R2 f# i+ `- f1 G
) X# r& P* S) m9 f1 |! P 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。* [# `9 V, \" K7 J& ]- s, F
I5 n9 E ]2 w( k' ]& z3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
0 o3 g/ |$ j5 q' D; r
) |4 G2 ?# c: M( D ```5 [5 w6 c: o9 n2 [3 X
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');+ A9 Q- W7 R9 u/ _
function add_site_wide_notices_boxes() {
8 c5 D/ e8 f, z8 a# [+ i3 L add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
* x" F- P8 U' |+ w' A/ F+ ? }
9 P( Y. l/ r& E0 q2 U1 S9 O8 }$ K) a: S
function notice_details_meta_box($post) {
( W/ j i$ E# }8 v! `2 u) ~ wp_nonce_field(basename(__FILE__), 'notices_nonce');
! e9 m! f& |/ _! x! i4 K+ { $notice_title = get_post_meta($post->ID, 'notice_title', true);
* v2 z1 H, ^" w% [) N# s $notice_content = get_post_meta($post->ID, 'notice_content', true);
: ]% _- J y% t' p% W0 ]! w+ P1 k ?>
$ N' J1 y9 ^2 j) J# M <p>% y9 c5 @! L4 x3 y+ |
<label for="notice-title">Notice Title</label><br>7 a+ I. V _' o/ s1 V
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
$ y7 W7 }. i, F" T </p>/ o0 v" @, Y: }
<p>7 s% @$ Y M6 [5 M
<label for="notice-content">Notice Content</label><br>
/ S1 n2 g) m6 g* N) g% S( r& @/ a <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>1 A, }" n, o0 _
</p>5 c4 k& V' H0 H( B: _, |& k* `
<?php
; X& g$ L) M" e! Q7 j }
4 H! K) L5 [! B3 c8 b! ~
6 v: U7 H+ ~7 k$ H9 L+ d add_action('save_post', 'save_site_wide_notice_meta_box');
% G6 w! R1 q- v6 q, @% U function save_site_wide_notice_meta_box($post_id) {6 f6 ~0 [4 Z% l' |
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
$ G* F+ Y2 B$ v8 S return;
- r% L% _, k+ P$ J5 ^! B+ h if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
* D0 [7 }. f. Z5 C z return;
+ S+ o- x, P. ~0 `- t
% l7 n: |' s" d5 z4 m- q if (isset($_POST['notice_title'])) {( ~0 ~& _- H7 t/ P+ R; o
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));* B# j5 R4 y8 `! E
}1 q4 o" A. o* X; w( m8 P+ ^
if (isset($_POST['notice_content'])) {: n# E+ k2 A. z9 Z
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
; e- x& o5 ]% [- ^* ] } x4 V3 Q! P! S& \& ]3 j/ A
}% c) p- G0 D9 w, h
```1 N/ B) ]: A& [! N
$ s' a0 k/ c3 T" G$ I; Y 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
* m7 H$ v* U9 X" {4 E$ _
# F8 @- ~4 O* l6 e( A+ G4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:' F- u/ S! u& [4 H. b; ]; v
$ |1 _1 q/ T$ m1 x! T% v2 I3 K" R$ i
``` I5 j6 Q8 u. Y& ?
$args = array(% C. o7 j, P; t. _8 l# Q3 P. }
'post_type' => 'site-wide-notices',
! l8 s. t. T! b 'posts_per_page' => 3,
( W# Z2 x; V" t' `1 L 'order' => 'DESC',
$ G% ~7 f, k; S! C) ?' n, f5 n 'orderby' => 'date'
" Y5 {, |, n9 V+ N! w1 N );
& C, z9 p B2 e $query = new WP_Query($args);8 s' _9 D, O, f# }) A. k, b
if ($query->have_posts()) :
% U P4 r* g+ t9 E* z while ($query->have_posts()) : $query->the_post(); ?>" V6 e7 A2 t* @
<div class="notice">
% {. y7 v: J& M- H3 ]# T <h3><?php the_title(); ?></h3># L0 a4 D9 V. D- Z: P0 M. k% X. T5 U
<div class="notice-content"><?php the_content(); ?></div>
9 m9 W+ N8 k0 d8 x </div>. M2 y* q1 t+ C: }* O4 J
<?php endwhile;$ O6 O" ]6 F; @, E
wp_reset_postdata();
4 u" Z3 [# ~. ~5 w/ Q endif;' E9 ~& S+ h4 A+ S# Z
```
+ Z( q, r$ ~: D8 t1 c0 ?. P
& {/ e- R7 c7 s6 \% Z 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|