|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
2 C$ i: |8 @% l" ?! K% v2 |6 R1 m) s! K* a. x& c
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。# Q7 `- Y8 l/ t: u$ G- ^
9 V' p p3 ^- L. o {' l
以下是创建自定义插件的步骤:
; V+ l6 d8 Q& }/ v. b% Q$ M# }" q
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
$ z6 \& D. j# q4 _% `( z
+ L) S# T* c; U# ~% u2 L2 P; s ```1 ~& K* H/ t$ N) J
<?php
# a, |7 b) J% h" Z! u /*/ K) [3 u6 ~- t' ?1 G
Plugin Name: Site Wide Notices Plugin
8 d+ d \/ X& q* Z- z! D _ Description: Adds a new custom post type for site-wide notices.2 q2 G& {9 ?% t% W
Version: 1.03 E/ o8 {. M- H6 m- o# A8 X& A, P) U
Author: Your Name; {9 v5 ]% `: H5 z% ]1 u
Author URI: http://example.com c& K* H9 B0 W- I8 o
*/
- D i) F( R( s' t
, x7 ^' b4 B8 }& U4 Z& Z2 E // Add plugin code here..., m' Y% y9 `7 P1 d
```
8 J% o; W ]9 W
/ z' i! A0 e% i- R 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
" R8 m9 m Q+ k% u7 y6 [9 l/ ?# B' l7 m. R0 ~3 Z2 Z# c
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:6 Q, r( v, O* a& D X( d$ k! U+ j& J5 J
3 ^; V$ X3 ]3 v& B4 k3 a7 n( F
```# c4 \9 a- u8 C g* n
add_action('init', 'create_custom_post_type');
5 H' {7 F7 u1 S6 R: B function create_custom_post_type() {; g3 h3 A c: s" b8 }1 e3 L
$labels = array(
4 N! @3 U w' e6 Z 'name' => 'Site Wide Notices',
$ K: }0 Y, t* p2 i, }1 X: A 'singular_name' => 'Site Wide Notice',
, N" o$ _4 I! O+ }& ~0 r 'add_new' => 'Add New'," _# S- ~( L9 ]: x1 g U9 a# U
'add_new_item' => 'Add New Site Wide Notice',/ l& l# ~( \2 q
'edit_item' => 'Edit Site Wide Notice',
, W6 Y& t9 H: _% o( D 'new_item' => 'New Site Wide Notice',3 f) ~; C8 Z3 V1 S1 s. n ?
'view_item' => 'View Site Wide Notice',% _ A# l7 C. `# _: d+ X# w( a/ v
'search_items' => 'Search Site Wide Notices',4 S: m( s. R( E3 _0 O# B2 J
'not_found' => 'No site-wide notices found',& g5 K, s5 |/ p& Y6 d; [* V7 T. P5 ]
'not_found_in_trash' => 'No site-wide notices found in trash') h1 l* q& w z% b" ~! Z' h% u
);
! x$ W: G5 C6 [6 V# h7 g' T% r8 Y- n) q- S1 ]; V" i2 \; K' ~
$args = array(
4 a( K d |! {- `: X9 [ 'labels' => $labels,! r( M$ I4 K$ d" T4 O
'public' => true,( i: j2 G1 [ Q% |# E G
'has_archive' => true,% u% F6 h- u- D
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
6 g* }$ R1 | e }2 W 'taxonomies' => array('category', 'post_tag'),: g: h2 B7 F. f- P
'menu_icon' => 'dashicons-megaphone',; W$ l$ u5 z3 b' U6 s% V) |& X
'menu_position' => 5,
$ E. g R3 S* m M; D0 m" t* ~ 'rewrite' => array('slug' => 'site-wide-notices')4 D# i9 T( ^4 c0 D% `0 F
);* j+ x/ ^7 L- M
& ~6 P. K" \ g0 F, a$ M0 O register_post_type('site-wide-notices', $args);
8 h5 p3 c7 E1 ~ N) d* b1 X }
7 N. T( t4 x' w% Z ```( R) V, |# U. @# A& Q& z2 }
& F1 V7 V2 B' f$ b; s1 c 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。: Q8 k0 B6 B, k" @# h- _5 c3 [
. J# \- s) ]; h0 c; x$ h
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
1 _; n: w6 j1 Q! W, ]2 ?" {. M6 z6 d* M W9 e1 ~
```) S: y% B* ~, ?5 G1 E
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');! B: k9 i+ T' A' n
function add_site_wide_notices_boxes() {
3 r6 L; R- Z9 X9 R4 F( F: M/ T add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');) @+ W6 Q+ d* N8 M- l( `* a! r" [
}
+ ~# i/ y/ B) s0 l5 L8 @
7 \" P9 p, R4 ^* U6 f" Y0 ? function notice_details_meta_box($post) {) Q$ R/ q" E/ U* }6 p3 T j6 D
wp_nonce_field(basename(__FILE__), 'notices_nonce');, b- i/ V0 K$ ?4 R
$notice_title = get_post_meta($post->ID, 'notice_title', true);
- g0 B3 x$ a8 x# h6 w/ L $notice_content = get_post_meta($post->ID, 'notice_content', true);& e$ Z$ v5 o* F
?>4 A/ N0 a1 D8 }9 Q- c, ]9 g
<p>
2 P6 H& O" Q9 b0 Y4 b7 q. ^ <label for="notice-title">Notice Title</label><br>3 `) z8 {+ Q1 S5 @0 a* P# v) h
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
& j Q8 X4 H+ s4 Z </p>2 |+ ^0 x9 I2 }0 ^+ d! s$ V4 L
<p>
: `+ V; E4 W4 `4 R1 R) U <label for="notice-content">Notice Content</label><br>) d1 T' e1 c) y+ o& W
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
: Q: b" g3 j1 s) T( x: a </p>
+ X) `$ y* O# T( E <?php7 G/ F' h a U5 }4 ^# k, V' N
}
! U3 a1 |8 q0 s7 l9 k) U* J# F, u9 t+ d# J. d
add_action('save_post', 'save_site_wide_notice_meta_box');3 j- ?; `( r8 L; \& s
function save_site_wide_notice_meta_box($post_id) {9 ^$ F$ O( f, ]4 e+ ]; z
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
3 Z4 ?: x" K2 w9 ?) g4 V" k* [ return; l! `1 o5 ?7 n' y! g
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
* n8 L; ~7 ~1 p; I8 g. T7 W return;
: z2 D9 P6 s/ F; T Y( u$ x0 d# O& }
if (isset($_POST['notice_title'])) {
, }1 @& I% L) q9 M/ V6 ~5 a update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
8 C6 B0 Q0 s2 p" X }" ]( J6 o/ o' f- U( \1 k
if (isset($_POST['notice_content'])) {
P) p5 Y/ T. Q7 Y9 m; i) } update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));, ~1 B1 n9 ]8 N5 j3 s, J% A9 K w# x5 [
}+ H+ I f- o: |. `
}- l& j6 M, ^- M; O6 K
```# Q" l$ ?( ~6 G
! w, f4 P+ @1 r5 `$ o, z
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。% k8 I7 J- O, _" H+ ~
& \( q$ X0 d: d3 ?5 q4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:+ G" @+ T" Q) J9 \
# g, C: c, ^( z N. a7 h4 d0 M ```
: C6 C( Y, G* ~# D4 h- D* w $args = array(
7 i9 P# ]# B0 F0 V# s* Y1 |' E& R 'post_type' => 'site-wide-notices',
3 d' u" e, I. C4 ] 'posts_per_page' => 3,
! U; l9 _" ]! x7 z% p9 E2 K- N. { 'order' => 'DESC',
0 l5 `+ L4 l6 y8 F; P 'orderby' => 'date', ]( X7 p6 F$ S; |& k1 [
);
1 A# @! Z' ]9 X $query = new WP_Query($args);
9 ]0 D) s6 ]: O/ @ if ($query->have_posts()) :
5 z# Z: G p0 U9 N while ($query->have_posts()) : $query->the_post(); ?>: G; Q4 }5 D8 g; [
<div class="notice">/ z& u5 U7 b, ?5 s
<h3><?php the_title(); ?></h3>
% ~. E" [6 B7 ?. i7 m% | <div class="notice-content"><?php the_content(); ?></div>
5 T/ q" `7 s" O: Q& W1 C* B2 T6 I </div>0 ^ L' g% d, ~ U
<?php endwhile;
. ~$ D0 h3 a4 l7 r: B wp_reset_postdata();
' D; X* G! g4 I5 I$ i5 n endif;
% ?- R) j% S: \- [ ```
# ], C! ^+ G% D4 V* V) h+ o' A% P3 x! T& h4 [, r( P, s8 p8 _* Q% n; U) E
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|