|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗? v$ G& @% p. {7 w& A
6 A- A( _: |2 \9 P
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
/ s1 X" W3 U* G! g+ J/ z n; C+ z7 J+ R$ A0 s
以下是创建自定义插件的步骤:/ L/ D7 z: G; s" N1 y9 s! H* v
/ y# K f z# ]* s3 I
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:/ R/ P8 W) V, \4 i
9 e) E4 W! E" O3 ~9 |4 D ```$ q: _6 Q3 J# `
<?php
- a' R- `" ], T/ i /*" R. a6 D% f+ Q- x
Plugin Name: Site Wide Notices Plugin* x, q. S9 N2 j9 g; H3 j5 F" g( O
Description: Adds a new custom post type for site-wide notices.
5 ]% g; R U" O4 P, t9 j0 Z Version: 1.0
4 D1 C6 `" A" V9 v, o Author: Your Name
. x2 g& K6 \4 V! n Author URI: http://example.com
3 p3 Y U1 W$ h7 d */( A7 [5 w9 q h
$ X @5 F, h/ a6 B" I- S // Add plugin code here...
% {" s n2 S, j7 P8 ?) h; h, e ```& {! u; z# J* M
% g5 o1 D( w$ | M6 l 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。8 R" X" `; B! j
) D- f5 i* p$ x: V0 a& p: G4 {2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
$ W0 G: X) M! G- Y; G' h! ]8 a) A0 _/ j0 Z; B; O- M
```
0 w _) |! S4 {* D add_action('init', 'create_custom_post_type');1 g6 [1 ~. \% ^# E! Y$ G
function create_custom_post_type() {
, d, |% ^8 Q3 ^8 j/ k $labels = array(
3 h4 ^ w" q, b 'name' => 'Site Wide Notices',( y2 ], P4 ?4 t+ |$ c
'singular_name' => 'Site Wide Notice',
3 n p" J+ L O5 {- z } 'add_new' => 'Add New',7 P% J* X/ k. w! r& R
'add_new_item' => 'Add New Site Wide Notice',* ^3 P0 K- j& N% l4 p7 i- S2 L0 k
'edit_item' => 'Edit Site Wide Notice',
9 t* P9 E0 _ r6 g1 O i 'new_item' => 'New Site Wide Notice',
+ \6 t/ l5 C/ {0 { 'view_item' => 'View Site Wide Notice',
" e1 o( ?4 h" L8 g& l% Q1 s: @; E% G 'search_items' => 'Search Site Wide Notices',& I0 z/ K$ v* c6 \5 L& o" {) A
'not_found' => 'No site-wide notices found',
# U2 y) Y5 _4 q* [ 'not_found_in_trash' => 'No site-wide notices found in trash'
; V* ?% n- E$ { );" H( ^# \! `7 H* o
- n( g& Q3 ?5 K' A $args = array(
% P L7 L5 ]+ e! s+ a& e 'labels' => $labels,1 r5 N9 {% @1 D* ^% X
'public' => true,
5 h8 G# W$ ^! Q7 R5 j8 R! } 'has_archive' => true,
& V5 b) ^/ J6 p- `4 r, Z/ s2 r8 g6 F 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),; c- @1 T8 T; r+ ?. T
'taxonomies' => array('category', 'post_tag'),
# `! o9 M9 P0 V: d5 ^ 'menu_icon' => 'dashicons-megaphone',4 D, \5 V/ _# \0 N) @
'menu_position' => 5,! A" k) S5 H5 Y6 p+ D- C2 B' g
'rewrite' => array('slug' => 'site-wide-notices')
9 |/ p J* `' X9 L4 e+ m; L );& Z) p0 _% q0 I0 z7 f3 {
5 W5 ~ R8 @3 s9 |- D' | register_post_type('site-wide-notices', $args); C" V3 d" s) |( {- \3 t! B3 V
}
; y' H8 ]" [2 l ```% z' E' v2 O& M. {9 m' O: T
# @7 q. K6 R; R, Z
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 |' ~8 p1 P0 S5 }9 }+ N- R' U
, C- V, C# }! d0 I* P/ d8 {$ s& d
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:; W+ ^$ R6 k! \! V: l
+ b4 p [2 ^. w* z ```5 ~# W2 U. |* H; W+ R) h
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
4 U$ o8 @) p! ^4 k) F function add_site_wide_notices_boxes() {$ D/ j* `5 b: ~ e5 N
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');/ e6 Q7 ^' t* }0 ]
}
& z! E) ?0 ]" S' B
3 o5 U; f7 }/ W5 l5 ` z function notice_details_meta_box($post) {6 L4 G( s/ K# L9 P6 a) t) N3 } p
wp_nonce_field(basename(__FILE__), 'notices_nonce');
# V( O) n- J8 M* r $notice_title = get_post_meta($post->ID, 'notice_title', true);4 i/ {. q% C4 Q/ k
$notice_content = get_post_meta($post->ID, 'notice_content', true);( |& ]3 \" r8 k. ^% y
?>! Y. V2 D8 F) h
<p>8 v9 t6 }" Q% g; y
<label for="notice-title">Notice Title</label><br>
0 @. n1 s$ k" p' U- N5 R: D9 L <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
# e8 G3 a1 z* {% M2 g- k- q! \# N( z' a </p>" Y; Z' F5 z* c8 d1 T! g; m; Q
<p>
4 M8 v8 f1 }, P2 k1 ]% |2 `! x <label for="notice-content">Notice Content</label><br>9 _% j- t2 \) X$ K6 u' C3 i
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
/ i& f) D6 a; S </p>! l5 y6 V; y7 w
<?php
9 S5 x+ h* @" q0 P, g }5 ]: I" a Y k" r% A" ~+ t3 D# Z' g
: }, R- `7 Q' j+ x add_action('save_post', 'save_site_wide_notice_meta_box'); s0 L+ ^" @2 U U7 k3 V. w
function save_site_wide_notice_meta_box($post_id) { V5 v4 m1 k+ P0 Q
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))5 S6 g- \/ k) e1 o
return;
6 M0 ?6 d; e7 H/ j if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) H. T/ T( Q2 K
return; v4 }- z2 S1 H
* v5 B' K, A9 h- ^
if (isset($_POST['notice_title'])) {2 w1 g3 Y, i+ p' ? x1 Y# y- `/ [2 d
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));3 x3 R/ e$ [5 v! B& U ~& Z) {
}! `& o# ~ b. J" M8 E: d6 G9 v
if (isset($_POST['notice_content'])) {, J# ^& o5 E2 e; g
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
- p$ j, l3 R5 }0 N }* f7 G% H5 J, M8 j# z
}4 J( A# n1 V8 C5 _+ _; E, I# s
```
S' u/ v- }- B4 z$ z
+ F1 N! j4 M. o; f- P- F 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
3 h0 u% ]0 S) f" K3 y4 O$ Z) I5 o7 A0 O. |8 O T6 _2 f
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
. P% O4 i- k* N M
1 l: ~' J( ^* D0 m. z: { ```
' K: y( A. [4 P9 N; ^/ b $args = array(& P6 D& y J9 { C7 a" B2 Z
'post_type' => 'site-wide-notices',
& b! `. l! k& U 'posts_per_page' => 3,. E. S+ i, X" O
'order' => 'DESC',( @* N* h2 E) q9 m: Q
'orderby' => 'date'
: g* U Q) z& N- O );
# f% Y' a: X* x' \; Q O- ~ $query = new WP_Query($args);
# k8 N' V; w& q: x5 b! G# F if ($query->have_posts()) :5 O- g, n9 l% C% Z! x6 g
while ($query->have_posts()) : $query->the_post(); ?>$ |( t+ g3 v/ }5 K; H
<div class="notice">6 q. g0 `* ~" G, U$ V; g5 ~6 `
<h3><?php the_title(); ?></h3>
) M7 O6 {4 H4 g' f/ }3 A <div class="notice-content"><?php the_content(); ?></div>" }' {/ _% Z) ^( h9 A, @
</div>0 R: l9 m& d- O; ^( Q
<?php endwhile;
, M H) f2 P0 i wp_reset_postdata();
. M( R1 R( R2 r( w @- T& ~5 c endif;
: k& a- S ?, ?% W ```
# R$ }1 ?5 M1 K. `
( b& t% G$ g! E. U6 a 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|