|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?7 i. u: [0 s; t3 J
& C. p& L& m. ^7 {6 `- Z( m9 c2 S如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
* b. r; N6 y( d: g" Q: W
5 g* D8 v: g0 T0 \8 e/ {. F以下是创建自定义插件的步骤:
2 I# @0 [, R5 i! w$ V6 ]$ ]; t6 w
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
* T$ T B3 h+ i6 W+ Z, n; ?# B0 C) `9 B) A" @( S1 F; u
```. m7 o: h( W* R6 `- M4 A
<?php2 g/ E! L5 V+ U1 k! p
/*
A9 O6 T O- t y Plugin Name: Site Wide Notices Plugin
' l1 Y, f) W1 n Description: Adds a new custom post type for site-wide notices.- V3 K% O+ h- [# _9 \7 Q3 c1 G3 i8 A
Version: 1.0
% G) w8 S5 W4 g; N3 ] Author: Your Name9 @( G8 o) ~% u( u! Y; W
Author URI: http://example.com& z* V! ]% K$ h7 _
*/2 d3 _) l' T- J/ _
, c, B0 L, s, ] s
// Add plugin code here...
2 q. A7 N* c: L' a ```2 p: d. b# j( F$ @& B+ B* e$ f4 o
7 T9 O( C. J5 K1 v0 q
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
! W c/ o' T" t
8 M+ c M2 A9 D, N% T2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:& B8 m* v. y5 ? m2 D
! `7 r. _6 _- v$ ]8 F$ ^* n! n6 O ```
: F4 e- \" y$ n5 N add_action('init', 'create_custom_post_type');& h/ y+ F5 X- ]/ t Z+ c
function create_custom_post_type() {0 E1 a$ {1 @5 J* [. N
$labels = array(
, i* r6 x- o2 g: u% x5 S) n 'name' => 'Site Wide Notices',) y! l, `- _- D0 V
'singular_name' => 'Site Wide Notice',
' ~2 {' F/ \3 G 'add_new' => 'Add New',1 k$ y0 a ^6 x+ X+ t3 e3 ~6 u
'add_new_item' => 'Add New Site Wide Notice',- q0 O" n$ ?6 }* y8 r( G) r* s. c! }: y
'edit_item' => 'Edit Site Wide Notice',
6 [* ^4 I9 O, O, d& r6 p0 s: y 'new_item' => 'New Site Wide Notice',. X) N2 F: O3 Z
'view_item' => 'View Site Wide Notice', b/ x5 I! @6 n9 H; D5 J
'search_items' => 'Search Site Wide Notices'," |% ?/ F4 O6 ^8 v4 P' p; A! Y$ L
'not_found' => 'No site-wide notices found',) a1 m* }6 r9 }$ D
'not_found_in_trash' => 'No site-wide notices found in trash'# u1 g3 Y% L& d+ A
);
# X4 T, w# v0 i# y9 I# X
+ U# H* U6 W- W3 m; o0 E $args = array(
$ _2 Q5 `5 K d9 F 'labels' => $labels,3 z4 r1 d* b" B, N ^# T3 P
'public' => true,+ [/ [. m) W G; F+ X" j/ v& F! X
'has_archive' => true,, t, U$ q7 g+ L. |
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),. W! r6 d5 n/ q9 z d6 Q
'taxonomies' => array('category', 'post_tag'),; x( T r% n5 H0 U5 b. |. g
'menu_icon' => 'dashicons-megaphone',
" l3 R7 }3 q0 x; A4 _1 r 'menu_position' => 5,
' ]/ U! a% C6 U1 d8 U4 l 'rewrite' => array('slug' => 'site-wide-notices')5 ^- s' z, s' j& B0 \
);# ^& N Z' D0 j0 A
: U n. i0 Z1 g register_post_type('site-wide-notices', $args);
0 l L8 I- Z. v8 t( Q" n. Q* |( g }
3 o9 `+ G& v- k- x4 e7 W- t8 o/ ?5 ` ```1 D) i( c% P& u5 C- s! s( b
/ n( c B, I& G. T' J. j- ?+ k 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。4 H0 ^* {3 p/ z/ ^* ?0 x
3 ~9 H' `" E3 h% S3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
) r" u" }) d" c3 j3 r. L+ \6 C( z( V' B9 f$ b! c% j
``` J- x6 e% W0 F1 H
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
7 T' @' `, e9 n' i* T2 \ function add_site_wide_notices_boxes() {- E0 E; F; Y K
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');; H9 y ^! v$ h
}
. m2 q( R) P4 p _! T+ J
& H3 d7 U+ a1 I function notice_details_meta_box($post) {
5 U, I- P: Z. ?6 s& C0 n# i wp_nonce_field(basename(__FILE__), 'notices_nonce');
, A# P; U. {# O! h |( c $notice_title = get_post_meta($post->ID, 'notice_title', true);: g9 X- W* |# V7 ?
$notice_content = get_post_meta($post->ID, 'notice_content', true);
( M; S% f2 E7 Z( y ?>+ e: K' \# [0 {9 b( ~5 p( {
<p>& @ i' z9 \) A9 I: i
<label for="notice-title">Notice Title</label><br>
: S% L0 V" F& F3 d% L; { <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">+ i1 N' e3 O2 d, |; U+ }
</p>6 f% G1 c1 j+ l: [( H6 b
<p>
2 ^0 [, y1 |% { E+ i <label for="notice-content">Notice Content</label><br>
5 o! Z9 q( g: }% d <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
. J) u/ [/ l" R r1 b' c3 f </p>
- X' \* m: n/ W4 l, U- _( d <?php4 V4 v2 | O$ s7 y$ @- g$ e6 M1 g
}
3 H# B: w# \: x; o0 a6 D8 |( W4 t
2 x$ f& n" e$ `& }) W' [. w add_action('save_post', 'save_site_wide_notice_meta_box');: j5 w' J, X4 R2 j- I
function save_site_wide_notice_meta_box($post_id) {/ W3 Y# M- ]0 x+ O
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))* _+ g8 ]1 r* r" v9 `6 U7 h
return;7 f: x- [, Y/ F. }. m$ Q
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) X: s1 |! o" ~% J" m* \; A |
return;9 m# R- D9 H7 s3 V# o( m4 A4 U
' r1 X$ V2 N9 ^+ V& @8 y
if (isset($_POST['notice_title'])) {( Z* v* S. r+ G4 S* M7 t
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));! l) X$ W5 {! ]* r/ W: u; T s
}' @9 U* u6 U( E+ `9 E( v
if (isset($_POST['notice_content'])) {
6 L2 r5 W s' i% F3 y$ Q1 ?' J update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));# M& i0 K/ {( |, z( z6 F- ~% K+ Y* c
}
2 Q/ Y, _, t ?) ^' N- _9 A }
1 V4 T1 ]% ~, K! ^3 w8 w7 S ```( k* ?, r- } \9 r
7 z9 B5 b- v0 ?3 S5 `
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。" l: T( \3 L9 a( b. k
& O. p9 _% m- O |4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 u- I5 x& Z8 x w" ?$ G/ k# Q5 E
! X: V8 ] ~, [3 i' [7 ~6 f+ } ```
2 Q" j! y6 p9 g. W: o( P" w/ q $args = array(: f; k8 x$ J) {# z* P5 [' c: A
'post_type' => 'site-wide-notices',/ z$ S. q& A0 x1 s) @
'posts_per_page' => 3,
$ N7 R0 u6 z5 E1 J- y 'order' => 'DESC',
- I. Q2 x5 `' T 'orderby' => 'date'' P# N# u- b7 I7 I
);# F/ G# w A7 m4 N0 P" ~& `* M
$query = new WP_Query($args);
. N& `" u* r9 A: Z6 Q if ($query->have_posts()) :3 ?6 i8 I& E) |& `! t( S' e
while ($query->have_posts()) : $query->the_post(); ?>
4 h- ~2 n+ q: ^: B4 T' k, g( S& }1 V <div class="notice">
; \: a5 q7 Z, K <h3><?php the_title(); ?></h3>, [/ {, |: L4 J+ a
<div class="notice-content"><?php the_content(); ?></div>
: j: [1 ^' J' S# _) O </div>" w+ X4 e; q9 p- B! p" D' I a
<?php endwhile;$ W4 m- _7 w. U0 J' n1 W
wp_reset_postdata();' z- k) c0 H3 I" t- T
endif;3 ^3 o+ O# {- S$ ]
```
: }5 W, X+ d' k6 A) I- U, ?0 m5 b) u B+ n# h- s7 L
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|