|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?* Q+ ^$ M5 z5 _/ X
% Z0 l" g# ^/ u" g- H; ]. L如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。4 W6 W: @ }4 s
$ \6 U& B+ E* K- w
以下是创建自定义插件的步骤:/ q3 w; W# V! H- \
. H% s. q4 P1 N/ \ m$ L1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
2 l2 w' K3 f& F1 q! ?/ T3 K; L8 Q" ?2 W1 J8 W
```% L: c. Y$ y9 U/ W
<?php7 B/ @+ x, C) p
/*3 s# i c3 I8 u) b$ r0 T- \) n
Plugin Name: Site Wide Notices Plugin
9 B; X5 |& p0 } Description: Adds a new custom post type for site-wide notices.
. _9 U4 E& p" M" V Version: 1.0
6 J- c- ^; E& K- f6 Z Author: Your Name
% K" K5 a6 _) n. v1 O8 V5 d# | Author URI: http://example.com1 p2 \" @8 ?: {) _: U- @; C
*/
- m. f. U, B4 N8 [+ A
; N% L7 H# S2 K // Add plugin code here...' |( O& {* d& q2 o9 a e
```8 p/ r; ^8 _ V, F
9 A: o: ^- Q+ P
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
+ l: R" Q0 ~ x! j4 X
M3 G$ C6 G) X) X3 ^$ D2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:! {8 Q4 t5 _0 k) l7 j% o& z
( f9 }/ G9 |0 j- M7 ^; P
```
; J1 Y+ a/ f l$ S# `# k5 M add_action('init', 'create_custom_post_type');1 S f: z. y" W+ J0 G/ i
function create_custom_post_type() {
3 }9 r' B1 D- P6 }* | $labels = array(
5 r# Y$ V8 k) P0 \ 'name' => 'Site Wide Notices',
+ K- k3 E" X2 y) A' f, Q 'singular_name' => 'Site Wide Notice',
: m c1 S, w5 m5 f- E! ?/ x/ @( ~3 o 'add_new' => 'Add New',( \) ]; ~* T2 m, _7 Y4 t5 B
'add_new_item' => 'Add New Site Wide Notice',
7 Q! s8 Q. @9 e/ L 'edit_item' => 'Edit Site Wide Notice', e/ i0 u0 c4 t, T" K& p
'new_item' => 'New Site Wide Notice',
. r8 u; J' Y( K6 u 'view_item' => 'View Site Wide Notice',
3 d" i4 T1 s& S* S+ K8 A' e 'search_items' => 'Search Site Wide Notices',( ?3 g6 l! r/ j y) e9 z
'not_found' => 'No site-wide notices found',' G' q# `' ~" Q) ]
'not_found_in_trash' => 'No site-wide notices found in trash'+ z' q, W$ t% U' G- H
);0 C9 @. N) [* ~3 I3 r% q4 ?" x7 l
6 [- j$ t! j @3 O
$args = array(
) Z* A$ u6 E X/ `# B! \" w0 r 'labels' => $labels,
$ V% X; K! a2 u 'public' => true,
; h' v2 S6 L6 I 'has_archive' => true,
+ W: |8 Q) U6 p# x6 d9 a 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
% w( m. T! g* O 'taxonomies' => array('category', 'post_tag'),5 d* ]! {/ n4 Y" p3 @8 H7 W S
'menu_icon' => 'dashicons-megaphone',
, c- z% A# G# G 'menu_position' => 5,& d6 R# s1 y5 {+ O
'rewrite' => array('slug' => 'site-wide-notices')
. p0 S( D8 o+ W );
6 O N& u3 L% L+ _' n. t* j) K& { v3 L
register_post_type('site-wide-notices', $args);# j8 q- C0 w; R3 Z5 `
}
; g( R, W+ o F; ~6 [3 j4 `& O ```
7 j I+ M. ^. ~* S- U U! B& \" Z- M e& W- w, I* ~; C2 v
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
4 \ x$ H7 p# l
2 z! r8 Q, K5 L. r. u6 D- b3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
! y4 Q; h5 J2 T1 ]; f! ~9 b8 G% t0 n0 G
```4 m" ^' @4 ` e! {
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');$ Q. j! V. Q2 ?* I/ E7 U: x5 a7 E
function add_site_wide_notices_boxes() {
- X0 U( v* ^/ L add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
4 n* O9 C6 v6 s1 } }
$ y Y, g \' k8 j
; [. s _+ ^* V function notice_details_meta_box($post) {
$ X/ m) u# _6 Y; J0 `+ y wp_nonce_field(basename(__FILE__), 'notices_nonce');, }( G0 ~' I! ?5 S( q$ b
$notice_title = get_post_meta($post->ID, 'notice_title', true); S: Z2 d" r* t7 [4 y/ C) Q8 S
$notice_content = get_post_meta($post->ID, 'notice_content', true);2 |6 c2 A; Q) s: ^1 P: M& a3 A
?>2 _2 j G; R$ `( ^ _- c4 {0 }! r
<p>+ Z* S& D# _, A" w
<label for="notice-title">Notice Title</label><br>
8 Y3 h4 _& ^. }7 y7 S6 J2 O( r+ Z <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">% ~* F7 K- D. z }, h
</p>7 m6 w1 P3 o \, Q+ s9 S
<p>
8 ]. g- U0 ]2 L2 m) J' M# v; Y <label for="notice-content">Notice Content</label><br>) D+ d9 t$ {* E7 ?3 H
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>9 ~% q$ k! ?7 l G
</p>& d% _2 S1 M+ X0 t
<?php
" N0 a. {, K- @0 V1 w }
! [5 N) }# M, R' q% m
; v) g4 a4 x+ V3 l- L: ` add_action('save_post', 'save_site_wide_notice_meta_box');$ W. X1 I+ j% U4 v
function save_site_wide_notice_meta_box($post_id) {
, s1 M6 u5 E# w1 [4 d, s if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
. I% c5 l9 E% C9 l$ W a return;9 ]$ h- m Q+ A( i; Y3 I' ~
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)/ r! a0 x- ~! i( D) k4 k
return;! x0 L$ i; ^) f) X0 l& U, D
8 f. W/ a: ]# m; L3 W if (isset($_POST['notice_title'])) {
; ^+ i; P3 c9 w7 I" }8 G9 H update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));; u, [; W W2 m) v: s2 q% D6 Z( p
}: K4 ~' e$ B% d) q8 Q
if (isset($_POST['notice_content'])) {
& Q1 M. m! f7 R5 f# R1 B; h8 U update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! E: y" w* {& H/ H( j* f- A" i }+ H4 ?! S) c" t9 k
}6 w8 c' J* }$ b4 p! d4 D, T
```4 Q5 K" G& f! C9 C. K7 r
3 P- l' z K& R- ^" O 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。' _0 ?# K2 Q( s9 Q% ~
" p2 ]. K9 i0 r! i4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:! h6 y6 r, F7 y$ b$ B0 v' }) J
$ b$ n5 X, K% N
```7 s3 N& u5 u3 K/ C% d) k4 z" b- c8 x
$args = array(
5 v8 |. u8 F% a/ d3 C7 R 'post_type' => 'site-wide-notices',
4 K( S5 X4 m+ {7 B4 e% D( |% r 'posts_per_page' => 3,
/ e; K; l/ J, a6 [0 ^8 v& K 'order' => 'DESC',6 \. B+ N5 K J7 W5 J5 y! n
'orderby' => 'date'; W8 q+ U8 O- i; r$ Q
);0 g6 q& c: L" l. d, y
$query = new WP_Query($args);
& q. |' I Z! l6 `3 o if ($query->have_posts()) :
' k' U* ]4 B8 h9 @5 e5 a while ($query->have_posts()) : $query->the_post(); ?>+ w* v2 t, E: c
<div class="notice">
- }6 V0 K! U! A( q& H7 b <h3><?php the_title(); ?></h3>
! i8 L0 M: G8 ^/ J H5 k1 _4 ] <div class="notice-content"><?php the_content(); ?></div>
G+ M* b% f L' r9 F* [. B+ R </div>
* x7 z6 s/ ~" e2 w2 o2 q# T <?php endwhile;
3 W2 J& y) V( m3 }4 Q8 [$ x y wp_reset_postdata();+ z) z3 z8 k! g
endif;
1 Z3 u. h7 a4 E ```
( P2 G0 I- o0 y' B9 j# S1 n, ?' o3 N5 J2 X. }2 o8 g0 I/ Q: }
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|