|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
2 r0 i( b. D4 w; U" e3 f) z1 f8 Q2 Y* H( P% X H+ s
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。+ h. K/ Z- V j4 h5 @) `
$ A( }5 S: _. X$ `
以下是创建自定义插件的步骤:
; {- I( ~& }: p& U- M& J; ~9 I5 S3 b' x( o% L
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
. ~1 e: e. b/ f+ |' z' a4 ~
. U5 M# M2 k' l6 P& o+ S- ^ ```
, y1 p$ |' R" _1 h) o <?php
' u/ [. r8 n: a$ A, L( Q! h& P& M /*
' E5 S- y! {1 r4 \5 G Plugin Name: Site Wide Notices Plugin1 T1 s# T! F8 D$ f
Description: Adds a new custom post type for site-wide notices.
4 G( V H; p$ T3 \ Version: 1.0
# L6 J! _9 ?$ Q6 i9 s Author: Your Name
: ~+ ]) e3 _8 o+ e/ m Author URI: http://example.com
/ k9 P1 F, |" g. Q: a */
- M- m1 E' z" J) F1 f; c8 K. h0 Y1 s$ G! ?
// Add plugin code here...
2 f1 H* L% z6 B0 v5 v$ M ``` E p( i' C+ F1 R. r* t# o
5 d6 C) d* R2 m s 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
& R' r, | D+ k) l7 W! k8 A E! E. e3 o7 c1 q V& L/ M
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
K* m* P* G- N7 k% j& D+ I* v q2 O
7 b1 _7 ~, n1 Q* X4 O3 V ```
' a( F/ O" }- W3 c8 @) K3 T) o add_action('init', 'create_custom_post_type');
' i) N( A0 y* A. U2 G* b function create_custom_post_type() {
5 R5 G% M4 p. M4 O, e; M $labels = array(
0 d: y4 Z2 E! V# U9 R/ Y! A 'name' => 'Site Wide Notices',% b5 s3 F: E+ ^ T( N+ G/ C9 _
'singular_name' => 'Site Wide Notice',7 a4 M K1 o+ K% m
'add_new' => 'Add New',
6 j) U% {" P3 c+ P 'add_new_item' => 'Add New Site Wide Notice',
) r7 \5 \& [- d' u8 g: N5 h 'edit_item' => 'Edit Site Wide Notice',
- h: u# u. v( t5 w* x3 L 'new_item' => 'New Site Wide Notice',
! q4 j5 r/ v6 S( d 'view_item' => 'View Site Wide Notice',
! k$ I4 Z# \1 C! t# G 'search_items' => 'Search Site Wide Notices',
6 f8 t% R( t1 ^: y; N* {! t 'not_found' => 'No site-wide notices found',- |- I4 W) k! y5 l9 m: w
'not_found_in_trash' => 'No site-wide notices found in trash', Y* Y; s0 F6 U2 p$ w$ f1 [
);" O- ?( N5 N# F7 [, P2 }
t* b: s% X) }; m% o1 V: [3 L
$args = array(* U1 s8 G, W5 x; u$ [
'labels' => $labels,
1 v4 K& N q K: Z4 h: o4 a 'public' => true,# G/ [ F5 ]! o1 a8 E) R' T
'has_archive' => true,+ _& M4 h9 X, [% H4 [6 U+ g5 K
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),/ z# V" R# {# x- _7 x2 g
'taxonomies' => array('category', 'post_tag'),
4 O4 W5 _# c* {+ j2 \, A- B 'menu_icon' => 'dashicons-megaphone',
- x0 @- v& W/ V n; c 'menu_position' => 5,
1 j* c0 ~" y2 T0 M) A: m# A 'rewrite' => array('slug' => 'site-wide-notices')/ w6 e8 A2 F+ N3 a
);( d: V( i: x+ S% Z' X
" [" y& j$ f4 C- M& q# R register_post_type('site-wide-notices', $args);+ [4 U& t) R& b' Y' ]: I0 I* ^
}* h9 {3 D/ t2 A- L) U- n
```: k# P" j9 ]7 d# X+ J7 V
8 F4 O0 f6 u! [7 Z# e% R5 t3 M/ v
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
& [5 t+ o2 t- a1 @/ T0 Q: V( |6 u2 v. N
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
/ i) z6 ~4 j) g+ s+ u- i# X! ?
3 N# h1 D! r/ P8 w) A. T- m2 U ```! [7 |9 X1 I( Y! M# c
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');8 g" k( |- Y9 b3 M- G* P j9 V
function add_site_wide_notices_boxes() { `: Y' r, \5 O( X, z
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');9 e b$ h+ s3 j, m
}# Z; V) S$ W# O# S
2 C' S( {: M$ ^1 b' x5 m; E function notice_details_meta_box($post) {5 {( Y8 f$ m# a$ ~9 b
wp_nonce_field(basename(__FILE__), 'notices_nonce');. U6 G+ _, R. N) Q5 `
$notice_title = get_post_meta($post->ID, 'notice_title', true);, z! G; ~% G d8 q% P( z' a! c
$notice_content = get_post_meta($post->ID, 'notice_content', true);
! R9 i) k. f- p1 C ?>
& e5 M$ ]! x P6 W/ J <p>0 g }* G. e% ^, k/ l4 ^( ]
<label for="notice-title">Notice Title</label><br>
" f' G$ r1 l9 R8 g" A. T, a <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
8 W9 o2 d0 n0 r: }' M$ r1 ~ </p>
, P" C+ m1 b! P! {1 j, { <p>; l: B# ~ S2 G0 A
<label for="notice-content">Notice Content</label><br>7 q# D3 i R' x* n# u
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
8 x0 k% o& O! X+ y: I& ]3 b; d8 j </p>- F$ k* Y- |- U* h
<?php
$ t7 y5 Z; B, V1 |4 j }- \1 F' n: W+ P* K# {
_; `5 b! A* T4 z; ?
add_action('save_post', 'save_site_wide_notice_meta_box');
$ v5 F/ c8 d8 B9 M H function save_site_wide_notice_meta_box($post_id) {) Q# u" w/ e& k
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
% }3 l) J+ o3 w; n, k return;+ w8 E+ J# n3 Q4 ~; P
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE), @+ T9 m5 D( u7 o* p/ u- d
return;) Q5 m. q* Y) S( N' ]
/ u, {$ ]5 O6 d( G# [
if (isset($_POST['notice_title'])) {. X. f; t8 L0 \* V6 L6 O6 F
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));( n/ M' F) J% ^ ?% }( c `
}! M. r! w: S1 B E4 b$ |9 c
if (isset($_POST['notice_content'])) {& ]8 L6 E! t: ?$ x% Y
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
2 u' ?4 w! B4 W s6 j" L" c* U }+ K/ G7 X0 F* C! [6 x' M6 ]
}3 K. o6 c; y7 \3 D
```% m6 g6 i' K7 u
, B/ H% Z, r* f7 A- \ a8 N
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 k+ l- P3 J r3 Y
X2 Y* S4 k6 {4 g: z/ e% o4 h n4 J
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
, J# e! F. W" x+ L2 G" d( S) b% `/ \2 {
```: u4 P9 U8 T7 Y/ T
$args = array(% f5 g) ~8 o4 j) T) T; @
'post_type' => 'site-wide-notices',
, ~: \9 w' n/ ^8 y7 Y4 o" E 'posts_per_page' => 3,4 H) I$ e% o& A4 U% O* d9 U) h* Q G
'order' => 'DESC',' c: Y2 L$ N* ?3 d6 ~1 U5 D
'orderby' => 'date'
! ?/ l; J1 @; O) ~2 y/ I9 { );, s2 P/ J! v+ W) ^4 y
$query = new WP_Query($args);9 A1 L. ]2 \0 U. M I# `1 F
if ($query->have_posts()) :
% P: N( V6 x `! y( l while ($query->have_posts()) : $query->the_post(); ?>
) j" _& }* X2 I$ A- M- P. X' D <div class="notice">
5 J7 d+ _) h% q/ K <h3><?php the_title(); ?></h3>4 }; V" U6 W2 n) U
<div class="notice-content"><?php the_content(); ?></div>
" a3 x" q* ], \0 C: F1 u% h </div>0 |+ {) y7 Z, G8 E a, o- m2 c
<?php endwhile;
3 J |% d5 O* X. X2 s' ?( O( ^ wp_reset_postdata();: ]$ F1 L, @% O! [5 M( A
endif;
6 J% p: O( D0 e+ X ```
) s1 w5 e: }) p
J% U* Y/ r |* k3 J0 i 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|