|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
1 W1 l) g( ~6 z3 g# { i$ L& @5 J# V" E& m. J0 v" F
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
& p* {4 o; J1 E/ \% ~ R. {0 K$ u' f
以下是创建自定义插件的步骤:
8 y3 |! s9 r" k) }- |3 T {0 X8 D# g; Z) t2 E$ M" e" f1 _- i: G5 S
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
8 |! A9 [* K3 v2 L
% j0 g$ O' b1 @( b3 u7 w9 H ```/ X/ ^0 L+ f% l! U- L( m" `
<?php1 ^" Y: D/ m5 G6 Q# q7 e2 d
/*& z0 o+ P, i" N3 t p7 Y2 }% S0 h
Plugin Name: Site Wide Notices Plugin
6 q1 B+ z' ]/ \$ u. B0 F4 Q Description: Adds a new custom post type for site-wide notices.4 U3 N+ z! p. O: o4 r6 D: H
Version: 1.0
/ S+ I- g3 ^, n/ o0 y3 F Author: Your Name
' }; K$ h+ m+ \9 g1 B Author URI: http://example.com. v' E8 H2 d G1 w0 j* K a
*/
3 n2 m! y- b. k/ B9 I8 y
. S5 K0 v5 r. B5 q$ C* z8 B' S8 X // Add plugin code here...
7 Z9 R& R1 r4 g; E" [* X ```6 Y6 r" q% r$ [, z. Z
4 E; E9 `/ l6 O' `& ~ 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。1 U- Z% Z5 x- Q# m1 d
" G3 x* N! m# X T7 \: J2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
6 E1 ~& ^6 [" w
" ]. u8 B2 R! e; Y# s ```
8 u2 g% y1 P4 r4 u6 V" e( }5 d9 p" _ add_action('init', 'create_custom_post_type');
; I' s. I/ l. x function create_custom_post_type() {
; I% _4 K: {0 M8 O" ` $labels = array(& p" w# e; P3 g. A6 A
'name' => 'Site Wide Notices',
( t# v* J, `( R; ?# }9 G* s 'singular_name' => 'Site Wide Notice',4 V( r) t* l8 g
'add_new' => 'Add New',
4 i, V O+ Y9 v g* _ S 'add_new_item' => 'Add New Site Wide Notice',
% }2 }( w9 N9 S# G8 S0 k2 A 'edit_item' => 'Edit Site Wide Notice',
( g; f. `& L' y" I& u1 M* O 'new_item' => 'New Site Wide Notice',
3 g2 q9 V( h. T( j, E1 [2 ? 'view_item' => 'View Site Wide Notice',
9 F" L7 t( O6 H6 C2 n8 g 'search_items' => 'Search Site Wide Notices'," N1 p5 L' j$ d+ x' S
'not_found' => 'No site-wide notices found',
9 U6 t1 i7 b! ? 'not_found_in_trash' => 'No site-wide notices found in trash'
9 i& V+ N9 q4 c7 O5 r V% B/ m" n6 F" f );
! j/ t, F- V# ?. R4 C: l: K* M0 B
$args = array(
; C$ r8 j0 G8 }( R q) ~, g 'labels' => $labels,% Y8 K' w2 C6 t" @: a
'public' => true,
7 Q+ Y7 X% T3 [9 `; Q 'has_archive' => true,
5 |0 R' j2 s# Q2 f( @ 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),+ L! q' i0 l( T7 G' K0 o) w
'taxonomies' => array('category', 'post_tag'),) e8 L" e2 r* ]5 [8 M
'menu_icon' => 'dashicons-megaphone',4 H/ m) ?; f3 s7 ]# s# z
'menu_position' => 5,2 |. I9 k% ]* z; h
'rewrite' => array('slug' => 'site-wide-notices')
0 B9 ]. ]: E- G7 M );
~/ G3 ]8 b6 W) ^8 D3 ?
) a; g( [1 D+ J& }0 Z Y- o register_post_type('site-wide-notices', $args);
0 _1 x4 K+ s3 I }
" F# J- y6 H* [, z4 a ```
+ ~7 ~+ T# A U. o# G* j7 Y& ]4 L( F- j# R& Q% g4 R, U
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
6 X& u" C6 e; \7 \; c @
5 g/ r# l6 U9 {3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:7 Y' \% J: s6 x! n" v0 H' O
: f/ I4 z# m* j- a% T$ c ```6 Y& J/ z4 N6 l2 @
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');) D0 Z7 V! u" v0 [4 _% Q
function add_site_wide_notices_boxes() {# L( Y4 j6 c& j5 U- A, d
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
1 e8 a: Y1 [* n' P) v! N }% N' K3 G3 c+ z( `' _' u
1 @$ c7 E' j8 |4 B5 g& r8 q
function notice_details_meta_box($post) {
7 ?( ~- N: Q0 c6 G5 f wp_nonce_field(basename(__FILE__), 'notices_nonce');! C+ ]& ?, `! \: y
$notice_title = get_post_meta($post->ID, 'notice_title', true);" b1 `, S4 l7 s
$notice_content = get_post_meta($post->ID, 'notice_content', true);
8 L/ j6 v6 E3 O ?>4 ~7 j: t( F. D6 |4 U, ~
<p>
3 W) c4 d7 B# t. ?, Q <label for="notice-title">Notice Title</label><br>" c& c$ [1 |; z r3 ~
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
% y! P4 b4 X1 g5 S! \! m9 k </p>
: c* b# X2 v2 v( V5 C9 ~$ g <p>
* ~5 c& j, d1 f4 { j <label for="notice-content">Notice Content</label><br>' L* l* e' _$ J, J K4 u3 y6 f
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
X: R! C# f' H) V# f' N </p>
1 j7 I# U% V4 { N' F6 Z5 m6 D# ?; u( q <?php
g6 ] k( R+ |7 a" I; x }
! x/ t1 c. _$ W1 X
+ _5 b$ [8 ]# V2 b. M add_action('save_post', 'save_site_wide_notice_meta_box');2 ~- F, m6 d, j7 G6 ?5 z W
function save_site_wide_notice_meta_box($post_id) {8 d+ V: L3 j/ U& v# C8 k
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))- p3 p, [* z; ^/ e2 `' u$ C
return;( h& D7 g! p/ w3 x: _( ?* @$ d1 A' ~
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
; E5 a4 W. d! [8 I3 r: ` return;5 U6 q+ }2 g) f1 g) Q- B! t
+ _! C' K2 Z2 I6 l5 f5 k* B if (isset($_POST['notice_title'])) {2 `. f. u$ `, |. [( ^- D. H
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));; F; F+ q( r( \9 Y9 J
}6 L5 W( H- j6 Z' n
if (isset($_POST['notice_content'])) {) L- j0 [1 d; D4 e0 R7 t/ ]
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));' S ?9 Y5 \* z
}, f- C0 p O* y6 l/ I$ J1 t
}# X5 t/ ^+ e5 g: D" R4 W1 F
```" q# r% |* ` J& p8 m% Z
4 P! S! T6 E" C3 ]# o2 c" m 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。* o2 ^& K/ b: d
# T2 J) C |4 ~/ B3 |# u6 d4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:! S8 [. K8 E1 a+ j
4 u, m) e4 B; U7 `& h) B3 G ```' D' i/ z: A% B
$args = array(5 R. S! d( Q1 [2 Q/ n. K
'post_type' => 'site-wide-notices',' j( j& y& m* u4 D J1 Y1 m6 Y
'posts_per_page' => 3,$ Q' O8 L, t. P% h; x
'order' => 'DESC', E- m5 x. r) l) ?1 v l5 H
'orderby' => 'date'
8 _* m# k9 h% s/ Z0 o7 N );* i1 M4 Q! d7 Q3 h
$query = new WP_Query($args);& V5 S. m1 m4 V
if ($query->have_posts()) :' B7 v$ L0 G7 S" \4 @. s% _0 w" K( I
while ($query->have_posts()) : $query->the_post(); ?>8 O" H, _, N0 X3 o" W1 i
<div class="notice">2 i- d* ^) L$ w0 E0 i$ J
<h3><?php the_title(); ?></h3>4 h3 Y9 S- e3 r; ^5 c1 Y/ q
<div class="notice-content"><?php the_content(); ?></div>
9 z" H% Z. D/ n: k8 J! E3 {6 l </div>4 Y+ s- B& V- ]" u4 @3 f- f- k' H$ X
<?php endwhile;, w" f: f) G" |3 } I7 C
wp_reset_postdata();
( [9 l: n8 q5 X$ p# D7 ? @7 `- u, i endif;5 F( _/ q8 K1 \7 p
```
5 l3 ^! i/ X8 q! S" W
' z' g& G8 M' j; C* n0 B' H 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|