|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
0 L1 t/ q$ |7 O1 I# _
h a9 N1 m( D3 f如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。" K+ x/ j0 D N! N# m. p! T2 M: I
2 a. y! `2 H/ t& u: I+ s0 p
以下是创建自定义插件的步骤:
; \+ T6 V% p* h B: i- T
7 G h1 N" l! n, i. |& K3 Z1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
2 E C& X8 ]; J9 }7 B# O! X1 c& w/ Q: \; M# j+ @
```4 P7 {7 F; _3 d" ]5 u- E
<?php- }1 L4 _$ ]4 _* |+ f" a1 `
/*- k; \% ^- f- n# J# v' X7 @6 ?
Plugin Name: Site Wide Notices Plugin
6 k1 K( |/ h3 O: T Description: Adds a new custom post type for site-wide notices.& L% U+ O7 R) U+ K8 K- q8 ~/ G
Version: 1.0 W+ H) V. c `" T! [7 J
Author: Your Name V0 i5 ~& s; T& c
Author URI: http://example.com
' n3 B4 A4 V* @- ?7 R */
" R& x: J8 l. P& U) T' B/ { s7 d. [ c+ C
// Add plugin code here...6 U, M( [2 X6 k* F. s
```. u- J" X& m1 ~# ^' h
0 U3 `& Q8 A4 f" X
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
0 i- G+ H) C2 R6 X+ Z- o+ s8 N. w3 Z8 C5 P/ {& W" O! l7 z" v ^9 H/ q
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:4 Q) Z& y9 z/ s: C" }0 q( e
9 g- N# |9 }: k9 H3 `
```- n! _- A! @' t3 r- X E6 h
add_action('init', 'create_custom_post_type');; t5 ~# u' y Q3 H0 k* \
function create_custom_post_type() {1 c! P4 C% v9 S2 J% {' W
$labels = array(
4 T( F0 f; c @5 q$ r; Z 'name' => 'Site Wide Notices',5 V6 s! ]7 w7 l' d
'singular_name' => 'Site Wide Notice',
2 e9 S) P- {3 p: a D/ b0 ? l( i+ j 'add_new' => 'Add New',
# E- ~) X. O. W) D8 X8 x' Y4 {, y! A! ? 'add_new_item' => 'Add New Site Wide Notice',
" s. @/ T% l2 ~- { 'edit_item' => 'Edit Site Wide Notice',
/ I5 A- i" x; G" y9 B9 n" @( z 'new_item' => 'New Site Wide Notice',+ }; w) G+ d$ o- z R" ^
'view_item' => 'View Site Wide Notice',
! I, O1 P! i/ S. d5 V# t 'search_items' => 'Search Site Wide Notices',1 _2 |; Q5 d" L# U# |( }
'not_found' => 'No site-wide notices found',' r" E5 n! b1 o- h" z, f
'not_found_in_trash' => 'No site-wide notices found in trash'
' \# S0 T1 R2 _8 n0 g7 u0 H9 j );2 S4 v V1 l, `7 _) m! _0 t3 W
" o" V# f: z$ h5 C k3 f $args = array(
9 `* p( `8 m1 m! { 'labels' => $labels,
. b: z9 G' H, m' g2 U) n 'public' => true,! |! N5 L. S; W3 J, U& k3 ]
'has_archive' => true,! o" q% a' P2 T' R$ Z
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
! R6 L' n% P: @) w6 n6 l 'taxonomies' => array('category', 'post_tag'),
6 W7 _$ A, G1 N; o 'menu_icon' => 'dashicons-megaphone',3 G* s9 }9 T7 @ K; g* s
'menu_position' => 5,
4 S2 u& [% x& u3 U9 ? 'rewrite' => array('slug' => 'site-wide-notices')
& U8 Y( C' S9 j );- S" ]; m4 ?* s% I( O) O5 X' a
( j6 @# V3 t1 x7 D% l register_post_type('site-wide-notices', $args);6 `) }. [5 b8 p9 v/ ^$ t
}
3 f' V; I" w& l8 {5 U+ |$ R ```
3 {* u1 p5 z+ J+ O& |; f; @
! x6 d2 v! n" N/ Q# O 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。! ?' m* V; v, T, }5 _) O `
- ]. ^1 r6 T: v2 ^7 O4 f9 E
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:& ~2 ~( k1 I' X; s+ ~2 F; o
+ L. g) W$ J0 U f
```
7 z3 M% t8 E6 B, N8 I9 ` add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
. o. L8 o5 Q2 V9 N7 n- r. l& i function add_site_wide_notices_boxes() {
& y& A! v# t+ L; G add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
o# n$ o3 N2 w4 T }
j x/ h) l. t, O% `/ a
) ]. o" X1 K2 ^" V9 { function notice_details_meta_box($post) {. U* I! v4 A0 [/ f
wp_nonce_field(basename(__FILE__), 'notices_nonce'); q" d+ @ L6 z7 S8 r( l" n
$notice_title = get_post_meta($post->ID, 'notice_title', true);% `7 F3 O: u! B+ v1 Y0 o3 ?
$notice_content = get_post_meta($post->ID, 'notice_content', true);( y, m& N8 e- Y" x* a- L: f
?>
0 ~6 t$ I) {5 v5 t4 |! L <p>
& v; i+ v9 k7 { <label for="notice-title">Notice Title</label><br>
5 m2 e: [+ i2 n) |9 ~- A6 Q2 l8 r$ n <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">! V( Q" [# N$ D) \; {1 j. i2 U! K
</p>* }% A0 p5 r* z V- W8 F3 H
<p>
2 @* ^: [8 M9 U8 J) c <label for="notice-content">Notice Content</label><br>
/ N. T$ k9 y# H ~/ C4 r# P: M <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>2 O& }" ]2 D' U
</p>* S8 N. a }8 E1 U- c( Z
<?php
% v2 J* v( F* q$ L }
5 ^+ b* [/ d9 ^; } X6 x# |7 h W2 R5 s' l* O: f& r. y
add_action('save_post', 'save_site_wide_notice_meta_box');& I8 a N: x+ z$ p5 b" y
function save_site_wide_notice_meta_box($post_id) {4 Q( _7 t* \. i7 y- T" x. U: S7 b
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ J* U4 N; r) b6 c return;/ B2 y3 X3 u% _5 q8 d
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
: R" c2 _( i% s( t return;! L% f; o, ?1 h9 f. F/ {
5 u8 y% n I# s3 f8 v* m" F
if (isset($_POST['notice_title'])) {
# g' j a9 z) P, L5 d% ^% E4 j" Z" A update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
; q1 |4 d, C7 T' v: G8 ` }0 Z* K) h5 e9 k( d
if (isset($_POST['notice_content'])) {3 w. a' C) E2 b! t) p
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
5 R6 y1 k2 T9 }5 @" l2 A }1 F" v" g& T# G% k' B" J
}9 `8 k6 I) |" b" i: U; h8 b
```, l! Y& C! `2 Y- R
- A8 L- Q$ I) d6 t. t2 N$ D 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
! { [( K8 J. [( K
0 J) O2 A8 c, I; Z6 }4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:! Q. S2 S( {& G" C4 r- |6 K
# g6 ?" L1 c$ n
```
8 [2 W6 [; _/ R- e/ s9 i) J( w6 s $args = array(
: y( }6 _2 ^$ h. h 'post_type' => 'site-wide-notices',
0 a; t( m, W+ S) j& q" Z2 P8 ? 'posts_per_page' => 3,
# K) e3 B% H$ B8 X6 C6 `8 J7 @' B 'order' => 'DESC',/ Z; Y( B$ A! l
'orderby' => 'date'
* x( w+ P3 ~8 r- u6 v );
1 W. b9 T0 u S$ D r- `, T* y $query = new WP_Query($args);
* o9 W9 G) ?: Y+ |) o" y; \+ P x if ($query->have_posts()) :
1 v2 b: \8 H( Z4 `! B while ($query->have_posts()) : $query->the_post(); ?>* E0 b: T5 P' H' r/ v% T
<div class="notice">
1 B9 [7 z( c) h& w* v( C <h3><?php the_title(); ?></h3>
- a: E1 L2 \$ b' p% L3 r! e# ], ] <div class="notice-content"><?php the_content(); ?></div>
+ {( `6 S" {. O+ l" J7 C$ b </div>1 F' W; j/ H. A& ]
<?php endwhile;
9 R7 G( C$ \4 _$ H9 v0 ~' F wp_reset_postdata();
9 y1 x8 m5 f9 z, E endif;
. t, J0 i, _. U1 q! ] ```* C$ d+ T( r# N9 S
* \4 R. I* q5 c2 J0 `; W* C
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|