|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?% M: W) c1 a8 T; A" z
y- j) Y' Q" s; f% J如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
2 D& T1 l. f7 i2 h& ^. W z0 ]8 T! }1 T. \
以下是创建自定义插件的步骤:
1 m" ?- W& G. b, L1 i/ j' i+ o M
$ ]( P6 V- `; P' f* z+ O! _1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:; Z2 G0 g; P* P' N' x( w5 {( H
% }4 a6 w. m6 _ ```
6 Y" o/ v5 d) U7 _ <?php4 M" |, `3 s3 @9 V8 j& ~1 G- f8 B
/*
3 A: c! X* u: u) ] Plugin Name: Site Wide Notices Plugin4 H( ~7 ]3 c& E1 t" H+ K/ l
Description: Adds a new custom post type for site-wide notices.; g1 d" Y* V- m4 n
Version: 1.0( H$ I% _' d4 f' g( Q
Author: Your Name
2 s0 G$ W$ l( E2 t9 {3 m- _ Author URI: http://example.com
& d8 g6 f% [3 ~* a/ y9 h. b. Q0 d) V */: E0 p) q9 ` S' c+ s9 R
/ k$ s2 I- Q! I // Add plugin code here...
. F$ g r2 T9 d/ ? ```
O( B' Z: n# d" f& Y4 ^3 R* Y8 }: m& F) y; G3 T
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。' h2 b* @ R; ^. A p: R$ c
/ M# M4 G& ^% N
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
) K6 M8 a' i/ I& ~/ n: l& t: O% A0 c2 b4 b
```
) T$ p8 P( O* u# I* y( M. a0 K add_action('init', 'create_custom_post_type');2 t+ P d- P4 J# p& w
function create_custom_post_type() {
3 J, z$ Q' y6 M2 z! w. V- |- f $labels = array(, ?. @$ P( u' G
'name' => 'Site Wide Notices',5 g* r) e0 T. k J
'singular_name' => 'Site Wide Notice',
2 o5 `" N$ Z' j, j2 H' C 'add_new' => 'Add New',0 P0 \' N, Y' j: p' X$ U
'add_new_item' => 'Add New Site Wide Notice',
6 m- ~7 q/ m( U/ o$ c# e 'edit_item' => 'Edit Site Wide Notice',/ }" d- w8 @; L# N# H* F
'new_item' => 'New Site Wide Notice',
6 |+ t0 E" I0 ^* z1 h& f/ ] 'view_item' => 'View Site Wide Notice',
7 s! P8 [# k& U. ^/ H) R 'search_items' => 'Search Site Wide Notices',
! |; V5 ~) Q! h% A+ w! U! p! [. X+ M4 R 'not_found' => 'No site-wide notices found',) q3 X9 Y- s( p1 ~4 F$ @
'not_found_in_trash' => 'No site-wide notices found in trash'
+ N( Y G) o0 a3 K );
# g3 z( e6 C: a0 [% B
0 L8 i5 \; t9 T $args = array(4 }7 X j6 Y, }4 E6 g( X9 Y
'labels' => $labels,% w- k8 p. f6 o' c8 V
'public' => true,
) {% d7 v: d/ H, Y. V3 F4 o9 [ 'has_archive' => true,
1 a7 G- @7 a1 K) u! t 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),! r& {, Y# @8 } u
'taxonomies' => array('category', 'post_tag'),9 w3 F4 d) u0 j" L+ O6 {. _
'menu_icon' => 'dashicons-megaphone',( B& M3 |/ `3 R" N+ c5 Y" h- n. j
'menu_position' => 5,
7 O9 ?; j6 H. q) o; ? 'rewrite' => array('slug' => 'site-wide-notices'): q" n% y( \6 ~; F
);6 \- z9 l/ N) ^& m1 t
$ L l, ~- {$ A9 n* {9 n: _
register_post_type('site-wide-notices', $args);- P- F: C1 Z g L1 [0 n% D
}2 s4 d6 u9 q$ C4 V, i8 r
```
4 z% N3 o8 _- t6 H7 Y+ B y% ~; ] A, d9 ^$ k3 J. l# s
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
, n1 c, G6 O/ V6 l& w8 i
9 Z1 r( W1 j0 X7 D3 r3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:1 Y. d b. W* ^; m C! n
8 ?$ y9 ^( {( c' j! O ```. I4 i$ |% \8 ?3 Q( e: e8 Z1 L
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
6 `/ ]$ d x4 T" \% b function add_site_wide_notices_boxes() {% k9 o% C& T4 A( q
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
9 M7 D4 k. Q* T }
/ q9 E& |5 ]2 b9 ~4 a5 d" ^7 V
A& G- Y: ]- M! u) J) b2 u! ^ function notice_details_meta_box($post) {
6 ^% u, O* }- h2 H% H7 s' U! P- c5 z wp_nonce_field(basename(__FILE__), 'notices_nonce');% H9 B+ x4 d+ l; K0 n) `9 F
$notice_title = get_post_meta($post->ID, 'notice_title', true);: L8 w7 h3 s3 u
$notice_content = get_post_meta($post->ID, 'notice_content', true); v+ |$ U2 ]5 d. h9 H
?>
5 i) J o& O" Q/ P8 P2 e* u, ] <p>
' v! B5 ^' L. G <label for="notice-title">Notice Title</label><br>' E+ g8 v; p; f8 p2 ~/ h
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 u7 x/ s1 e. S0 U1 H
</p>
6 L: y/ `! D( a: @2 j& K1 n <p>! f& b2 A0 S# C) U
<label for="notice-content">Notice Content</label><br>
- G& p# t! Y8 A- d0 F; Q <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
" I' D0 |1 O+ Q `+ M </p>6 \% b& a! y/ _( O* }& R
<?php! W( p1 E+ a# m! Y- A) r+ E. r
}
- s0 F! S( t8 v+ L+ \6 p
. L s/ |/ y, ^0 o; }. X, K* x/ F add_action('save_post', 'save_site_wide_notice_meta_box');5 h. f! h& g" r
function save_site_wide_notice_meta_box($post_id) {1 y: f! K8 |( _. ]9 t! W0 u
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))+ ?9 o5 g: q3 j$ \+ C- \
return;
0 \( B& A; |6 C6 K if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)* T4 K" W) v$ C$ p3 l) e2 |
return;
9 M- D7 d3 |4 V. i9 V, y( b6 g B, z8 g) t S# ^8 y( ~
if (isset($_POST['notice_title'])) {! r" E" Z$ N5 u9 o
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title'])); p4 }, Y; {. z3 t' v j
}
9 c5 f0 r% _ q if (isset($_POST['notice_content'])) {) Q. [2 w: P; l; }, ? ?- X7 D- l
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
! Z! J2 S, ]$ w7 r! ] }/ U- z# P9 P( V+ d
}
) n2 ]' p( `5 i, J$ Q( S ```
' j$ R b0 B E- o6 q4 y0 ~
5 l* B9 r2 f1 { 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
1 _/ ^4 Q6 x( ` e- t E( ?! B* s1 C; f) D& x) G1 I6 S
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:, z6 o2 b' }2 ]7 m6 R" M
, S, n# A J1 z: [7 m ```" P5 Y% b; U" Q+ j
$args = array(. X6 f4 [. Q1 I6 O; ~" A) k
'post_type' => 'site-wide-notices',
+ t' X8 e4 p: K# e! U0 W 'posts_per_page' => 3,
! K: y8 U5 ~& Y- }+ m6 c. x 'order' => 'DESC',+ r4 I4 w3 i* l4 C# j
'orderby' => 'date'8 O; }6 j* R4 r3 D9 | G
);; @( w `) x% T: l
$query = new WP_Query($args);
' s7 |/ c, Y/ l! m s1 Y if ($query->have_posts()) :
# a( x) I' m# ?& u0 n5 M while ($query->have_posts()) : $query->the_post(); ?>
+ O3 C$ @. D T0 ^9 ^. s4 ^ <div class="notice">6 a, Z4 d Z; c
<h3><?php the_title(); ?></h3> \1 M0 U9 J/ L+ w4 f1 \2 c4 a
<div class="notice-content"><?php the_content(); ?></div>
: T+ c- o- O1 k3 v7 b1 f) E </div>+ s& p1 p e- ^# G" g
<?php endwhile;, f/ L" t" S. f9 Q
wp_reset_postdata();/ Q7 H' n0 F" D4 W
endif;
1 A: X2 I: P# B! n) { ```; l# j7 ~9 G/ ~) z% ?1 ]+ W) R! Q
2 |4 Y' [% p4 s, R" e
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|