|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?: U( \: S2 u8 S% {5 {
/ [/ W; v+ K- ]( b5 u如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
1 R" @8 o3 h% P3 F" O8 m
0 {2 u5 b+ Z4 ^6 P, m! I' _以下是创建自定义插件的步骤:# D5 Y; t, @# M" m; o
, |% R0 |! R& l" H1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:! i6 ]( t2 x# ~4 s
$ s G8 R4 q! G, j6 ~% ~4 ]! j* ^
```
6 G! k' B9 W5 T) y <?php6 j) ~: V' q" P9 U" k0 F4 U# N/ u5 M& p; Y
/*
, [, Z1 F( t1 u0 ] R3 u6 o2 n Plugin Name: Site Wide Notices Plugin
+ Z7 ?9 M5 x, i2 y. l9 R8 o. V Description: Adds a new custom post type for site-wide notices.
- @- x2 o1 I- }: j0 N. w! N( w Version: 1.0
c' A% T( B" [) a Author: Your Name. p& i& z/ G% Z! b7 C9 V9 a3 ^
Author URI: http://example.com
* V' ^) T/ T" {# R X, D* {. l */5 ^& M; v8 `6 ?, c
h3 m, T9 m' f" @/ U5 M0 e
// Add plugin code here...( D- ~1 f# _* l; i2 q5 y" V
```# Z1 R8 q. G3 a0 C9 q: c
% o; m/ M! x1 [
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。6 X. }7 y+ o3 P2 v8 E1 _
8 c& t! W) |$ m" P+ R9 b2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:7 W1 x( n3 S9 j
( R+ @& c% k% k, \* x
``` Q; e6 _5 J) T0 D" S
add_action('init', 'create_custom_post_type');& a; u* H- `3 v2 } y$ n B
function create_custom_post_type() {* s. ^4 R W. I/ T
$labels = array(
0 V% g& |5 U5 e 'name' => 'Site Wide Notices',+ K, R) A! X) W' {- D
'singular_name' => 'Site Wide Notice',
" H0 R2 Y) @6 |. w. m8 Z" g 'add_new' => 'Add New', F* E: s+ u3 r0 J
'add_new_item' => 'Add New Site Wide Notice',
8 J0 Z4 j! B7 M1 U; H) t 'edit_item' => 'Edit Site Wide Notice',7 ]0 _( Z7 c8 ^. y6 `
'new_item' => 'New Site Wide Notice',
5 Y+ g$ E G: g6 R 'view_item' => 'View Site Wide Notice',
, ]& a2 c. y# ~ 'search_items' => 'Search Site Wide Notices',3 Y8 d- t( y8 S F. I# z
'not_found' => 'No site-wide notices found',
2 T' V* J- J9 L( s% Q" t, ] 'not_found_in_trash' => 'No site-wide notices found in trash'
2 Y! d$ ]7 K" P7 { ^9 R );6 D- ?5 |7 F4 \, Q' A( |+ Z
- a( i8 X, d2 y $args = array(
, e0 e' I& M3 h3 _ 'labels' => $labels,
; ?1 F% J8 v* o" ^7 @: M& e$ g 'public' => true,3 x: x- B) l8 \' A) |
'has_archive' => true,- c6 B# z# K* K5 E9 a% ~- v2 a
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' ?; V0 d( ~' K& `- m( i
'taxonomies' => array('category', 'post_tag'),/ t9 f7 ]# L6 B7 b
'menu_icon' => 'dashicons-megaphone',3 h) A& `: P" Y* u: l/ [
'menu_position' => 5,) R$ i8 R+ [6 c, R0 T
'rewrite' => array('slug' => 'site-wide-notices')
7 `5 g( D$ A6 n7 n- @1 v# w! Y );
3 Y c! S! J* n2 l, c
8 t3 i# o' j& v7 E; W; V register_post_type('site-wide-notices', $args);
9 q$ y. }& w& @, s- @ }
! i& j0 P, G" L* y ```
( f5 Z' b8 j# Z. ]* ^
% b9 k. {! J2 \ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。/ ]* g5 z9 O) M8 ~
- W5 h: i( O8 ?& s2 d
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:3 n0 B( F4 g) v0 Q2 [3 @7 D
% h0 B h* O; o6 T, [0 h4 x2 F
```
1 y4 D' v- f& e" V! H add_action('add_meta_boxes', 'add_site_wide_notices_boxes');" I9 W9 k2 E# l
function add_site_wide_notices_boxes() {, W: c# g$ P. k( a; b# ?4 E1 u3 h" r9 g
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
5 o! U4 V; M% ~$ H8 n9 r2 s }
+ A5 z& U/ i8 R$ o$ `4 I6 s
* e- |/ H2 c, g8 Y8 Z- Y* c function notice_details_meta_box($post) {3 E* s8 \2 K; s3 g! t3 s
wp_nonce_field(basename(__FILE__), 'notices_nonce');# H" q9 Z' N% x
$notice_title = get_post_meta($post->ID, 'notice_title', true);
3 S. H) z r6 T5 j; K2 b! G $notice_content = get_post_meta($post->ID, 'notice_content', true);
" `2 q1 u& l6 Y) T& ^; Y ?>
: J% t7 o9 p X <p>
9 z/ ^8 R2 {/ s. X9 G <label for="notice-title">Notice Title</label><br>
5 n, j" X! z g6 g3 V; a+ P <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">& ^2 ?8 v9 W. c% w7 R
</p>% `* f2 n$ v# [+ K, |& m# D/ ^
<p>
- O8 g: A: ?0 M( J: K( f q0 F <label for="notice-content">Notice Content</label><br>
: _- q* y3 U0 [; l. a3 D% Q <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>1 }2 r# Q1 }4 i3 J L7 d& |
</p> P- `( I8 z1 c5 k
<?php; n2 h8 h7 J( f* R5 c
}7 A" [. J( F. X8 ], ^' [$ e
4 n( K' P: R2 `: f
add_action('save_post', 'save_site_wide_notice_meta_box');
4 q7 M1 ]* q7 z7 Y; f function save_site_wide_notice_meta_box($post_id) {3 F8 R2 p( W" {- s& G
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
3 A! Q& Y0 l( d t9 e# k return;
2 E" K1 Y/ }0 r& i, S1 m1 V1 H if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)/ G) j* v! h: W
return;
5 w, f6 \' w; n d& j ~- h, [/ T2 K
if (isset($_POST['notice_title'])) {/ h" d1 Q* X2 n' n0 g9 F
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
$ ~ S0 ^$ c2 A. q" f* I- W- E }
7 l# k& f1 f4 y if (isset($_POST['notice_content'])) {
3 m9 o3 v4 r7 V' Y update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
5 N" ^6 r, t+ l }
* O# V( Z* n4 r! p! p2 y1 U }
0 z+ O. E" F- C# i$ e% `5 v ```2 X% m% X) A" G6 l& k, _
+ K4 z4 K3 r( T# }1 g, I+ e. O 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。9 j {: e+ e5 |+ t3 }. n* I
% ~, O: O( W0 E- N9 P4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
# |2 J8 X& S, Z: [7 N8 w
$ e8 e, j! {8 H ```' c0 r, w& \) `. P& e9 n; Z" i
$args = array(
8 p1 \5 D& ^! l 'post_type' => 'site-wide-notices',3 g2 D- ^. W& b: D- Q
'posts_per_page' => 3,9 N1 z# l& w0 [, [% B/ @) f
'order' => 'DESC',% S5 K& I' R* I( R$ s# E2 T+ ]
'orderby' => 'date'
2 b z6 k$ A$ D2 d: O9 K } );
. ~4 H7 k; _* g, w/ l $query = new WP_Query($args);
. R! |% c9 w1 j if ($query->have_posts()) :
+ Z4 @5 q& z# Y& P5 Q while ($query->have_posts()) : $query->the_post(); ?>
; j: L2 C8 N% p <div class="notice">
6 ?6 J7 i4 y$ |0 W% l <h3><?php the_title(); ?></h3>, Q' W4 I; C4 G: k
<div class="notice-content"><?php the_content(); ?></div>- p( [* A5 Y, c3 m+ x. R. s
</div>% Z9 E7 b% J9 E) r. F" |
<?php endwhile;( R5 b* a8 c/ Z9 d4 b3 ]- n D
wp_reset_postdata();
+ s9 e% q) I7 e: k endif;
, }' z D* t+ L* M" i# h- l ```
6 B8 I" P7 D; h5 ~+ S5 V, G" v2 X% e4 x1 d; k; |
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|