|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?- D" D- P% r& U d6 A' Q2 }( w: D
5 X4 G8 a4 @9 q2 j# U; P5 `
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
. E5 X* q2 G2 F. t8 p% Y
) _% c7 A0 H* T) f6 ]+ J s) u; S/ K以下是创建自定义插件的步骤:
; h4 ?5 p7 }/ D5 T+ b" E& B8 v
, ?+ _' o N. I: Y# a1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
5 _7 |; F# A l2 L* m% [0 d3 U$ ]- r4 `/ B" s% o
```
& Y7 ~6 i; m9 }( C: v( u <?php7 X7 S9 g4 Z/ A
/*. Y1 q. l, H! _! V
Plugin Name: Site Wide Notices Plugin
' X( N$ r& L& J9 q2 A/ l& @ Description: Adds a new custom post type for site-wide notices. Z& V/ n* p4 y
Version: 1.0
# z* u) y. h R; ]1 d Author: Your Name3 q; n) e+ D0 \
Author URI: http://example.com
5 \" M& E. c9 K5 \( Q1 ? *// n- I1 F4 q( b. A
, j6 e2 i0 {+ W4 ]* p
// Add plugin code here...
, W+ ]$ g& C5 Z ```( g. N( }- ~+ ] _
, F% K# v. g0 D
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。' l# {9 d* _1 n2 p$ t( e0 {
* l' }$ E4 L6 K7 L
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
7 F, L! e1 G( b* w, D9 i! d3 ~7 M' l: p. k' E# S
```
0 O5 B& e- O9 h( P add_action('init', 'create_custom_post_type');
_, {% L4 i, e+ q& [! K! i function create_custom_post_type() {! o4 s% W- K( P$ f
$labels = array(
4 s ?8 R. Y* n& Z: Y' s# c* N 'name' => 'Site Wide Notices',
/ T7 ~6 b+ w v" t9 t" j 'singular_name' => 'Site Wide Notice',
1 ]& c! o% Z2 u, ^ 'add_new' => 'Add New',; P$ O& H/ j1 B; n! ?9 ^
'add_new_item' => 'Add New Site Wide Notice',
6 V N/ f" ?# F; ~6 _ 'edit_item' => 'Edit Site Wide Notice', Y, X9 ]4 l' j
'new_item' => 'New Site Wide Notice',; F% e# w u% ?6 b
'view_item' => 'View Site Wide Notice',
. ^8 D, a6 z' P/ d2 } 'search_items' => 'Search Site Wide Notices',. d( ]6 z4 W9 g8 n/ Z7 `
'not_found' => 'No site-wide notices found',
* ^% P1 k1 S o: b3 P 'not_found_in_trash' => 'No site-wide notices found in trash'. ?/ M3 u& U/ x! Y6 }
);
7 k9 a. g: ?: ^- i6 E' D# w9 b5 o# K, p# p' ^; W
$args = array(
" u; v& C* Z* G8 K# l( j 'labels' => $labels,
+ L( G3 g# F+ E6 U7 b 'public' => true,
- H8 `3 a1 }, E+ T r8 A# R8 M 'has_archive' => true,; e+ H, U* C5 s2 m) ?+ F! t
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
0 t1 b* N2 M. B8 F 'taxonomies' => array('category', 'post_tag'),; g! ^) i1 r; `% O- ]8 P5 a
'menu_icon' => 'dashicons-megaphone',
5 Z3 I! t$ Y: V5 Z0 V. S 'menu_position' => 5,
: y( i4 m. B; S$ @+ {0 |) A 'rewrite' => array('slug' => 'site-wide-notices')5 |# { T7 C9 X+ n& A; [
);( H$ c' ?& m) W4 ^1 F" C- Z; W
. Z/ ]8 C, Z6 X: x7 y
register_post_type('site-wide-notices', $args);
# a8 S: W9 K& _4 I }
" m! N: a( W* a0 S+ \' u# B ```
1 q$ _" Q5 Z& w& \: j; B# J! s- r9 ?1 s8 p z
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。" ]7 r7 N4 k9 ]- v
1 H8 a& P8 ^8 x0 Y6 s q8 t* \, E
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
- q9 l N. a! Y( Z) I2 M8 \4 V/ v
4 O+ i9 i! ]0 r6 z# {1 m3 \ ```2 J0 w+ R% h4 q6 d4 z( X
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
5 k! D% @" n- J1 V( }' g function add_site_wide_notices_boxes() {
& h# w: t( k: z2 o- O" A b add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 I& W+ f, C+ m- V; J }
" w: D7 N, I h) n1 R4 y' _: ~* H' w5 r- n, z
function notice_details_meta_box($post) {
) |, N; j G+ H. y' @& ^, ]3 E& K wp_nonce_field(basename(__FILE__), 'notices_nonce');
4 k1 o5 a- T8 S4 w1 p I3 g* Y $notice_title = get_post_meta($post->ID, 'notice_title', true);
! d# n' a) [# N* h. L7 P, S $notice_content = get_post_meta($post->ID, 'notice_content', true);$ k8 [/ d0 a: G d% F$ E5 W
?>
. b& l6 h- L8 t, e <p>1 L' Z) q3 V! p5 h
<label for="notice-title">Notice Title</label><br>* p$ L, V8 X( s" _
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
" X; Y. _9 M6 ^ </p>' H- ^: a' Z0 {
<p>8 v. A" ` R# o9 n- c8 @
<label for="notice-content">Notice Content</label><br>
4 {8 C1 }+ x) G! a+ [) Y# D <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>' d) {3 y M( H( G: c6 Q
</p>+ O' h% `* c8 J* q) l( {3 L
<?php
1 `" k' e( g8 N8 X' M" W }5 p+ R# {: C8 f
( I" K/ y' A/ m4 I# Q8 z; Y
add_action('save_post', 'save_site_wide_notice_meta_box');
M, _( F2 o! z# N8 L+ H function save_site_wide_notice_meta_box($post_id) {
: X6 s7 _2 ^ D X: u+ \8 c if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))): \1 [6 W( n% H: P' G
return;5 v: t5 S% e2 J- [; J! E2 j
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
6 c6 @. \# S! ~' s3 j' M% ^% P0 r; v return;7 k% X, O* {; p$ P4 S2 t$ }5 k: j
4 B1 o. s7 [8 M$ ^ O" q2 N, P+ C
if (isset($_POST['notice_title'])) {( M3 n) B& Q, E' B: P% ^
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));& f6 y) a* k- w) H8 @ k7 R
}
2 w; k+ _0 k8 N3 z/ K1 g if (isset($_POST['notice_content'])) {
0 E* _4 ^7 Z i9 q9 S n: _" I update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));( m. s4 I- @! `7 G2 A
}( r5 t" t! ~+ \) j. G" g
}; }9 I2 \2 J# U( Y+ E5 b5 A# \% Y5 [
```
% } z% u- z9 v) Q
* ^; e, z7 `9 P6 J7 U 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。, Z. N0 d% p% s% c' z) U# l
+ M$ i& i$ s. o3 R" X4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% C5 w. q, J- ], N4 e/ a7 F, t0 z9 L1 G' y( |0 W d2 h
```2 S& X6 |3 B; L
$args = array(
a- K* H- ]! o6 B- j 'post_type' => 'site-wide-notices',
0 h0 r, W# r; u$ O 'posts_per_page' => 3,# G. ^7 K) r3 P: q0 @9 w) c
'order' => 'DESC',& \" q7 A, Z; C8 P, p {: M- z
'orderby' => 'date'
. ?9 D4 [; _4 E: X );
/ N& C' ~% N$ }7 ?1 S5 A $query = new WP_Query($args);4 d7 e# G$ {/ ^. y, K0 e& j" d1 k
if ($query->have_posts()) :
1 J5 Q" N/ r6 O. O0 ?8 F/ h while ($query->have_posts()) : $query->the_post(); ?>
6 D# \% E5 }3 F1 l- y <div class="notice"> C) O1 r, N" U5 l
<h3><?php the_title(); ?></h3>9 l- Y* ? W, l- f
<div class="notice-content"><?php the_content(); ?></div>
" b, R" u. Q; V9 n, T) s' O& [ </div>5 d* v4 O5 r- f1 o, ~
<?php endwhile;
2 `+ U4 H6 V( R* C! L7 S wp_reset_postdata();
& J8 S( {1 n% F; T& T* E/ e) o endif;
0 X0 c& x, h/ y8 x3 L ```
; J* t9 b% {' A( l" [" |. w
# j/ i8 O( V- V! J* O. W! d 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|