|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
# J h9 F" n1 `) k$ J: f* X( F$ Z
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。2 v5 O6 Q. x* M, R0 f
9 o, p! V/ @: v
以下是创建自定义插件的步骤:
! w v0 p7 v c$ B1 Q5 G9 N \, Z% B! g$ Y
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
/ G( T# [+ ]- |% w+ y
0 S9 d& r4 }# [5 s/ l4 o8 Z ```+ J4 d) v9 B( P( t+ Q# K- K7 j1 ~
<?php
y: |' x+ b1 c) F, K" o* B /*
8 s+ X' l( ?9 L Plugin Name: Site Wide Notices Plugin4 B2 e& [8 X; ]! S
Description: Adds a new custom post type for site-wide notices.8 c' K3 h% c" O" b
Version: 1.0
# ?' P$ G) r- Z3 o Author: Your Name" N& v- A7 w# U0 n$ L
Author URI: http://example.com
3 G: ]4 x; z% S# c% a, ]# f) J8 x */% |' z" t& h( @+ [* \
* l6 W1 o$ \( X/ H& E& Y* R
// Add plugin code here...
# J6 D0 x0 I! t/ c3 x1 Z ```
% g; n: { _( y" s) Q) p/ p' \. u
/ Q, E' E3 E1 y. J0 i# _4 { 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。! Q4 D- g' X* A
3 q. X" D9 t) Y8 Q2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
W# m. A5 C7 W4 j
( {* W6 B! `/ r4 w) w ```
9 m. u! O7 @ ~0 o! O) K, W' J, w8 D add_action('init', 'create_custom_post_type');
! O1 L9 W1 r' u- }6 b2 a5 e function create_custom_post_type() {' G6 [3 s' y4 v$ s: ]" c: i
$labels = array(
& H6 w% z7 X; l. _ 'name' => 'Site Wide Notices',) z6 j/ g3 y: P4 A. `
'singular_name' => 'Site Wide Notice',
0 p! H7 a" a- g0 u& h( n' `$ | 'add_new' => 'Add New',
7 ?# w9 M! t9 K. Y2 b4 A0 @& A 'add_new_item' => 'Add New Site Wide Notice',
# N- c3 [7 Q5 o3 y- b 'edit_item' => 'Edit Site Wide Notice',; U) s0 m2 W! O( o
'new_item' => 'New Site Wide Notice',( F! s" i( N! c+ Z, i& |5 z
'view_item' => 'View Site Wide Notice',6 Z7 s0 t: G; l8 L$ L F. D. ?
'search_items' => 'Search Site Wide Notices',
1 G: [" G: P4 n+ P6 N 'not_found' => 'No site-wide notices found',/ r3 M$ x# x$ B l
'not_found_in_trash' => 'No site-wide notices found in trash'
2 H. L& o5 W/ Y' N+ N );$ ?9 d5 y& v3 }) k( S' V; C% P8 M
8 N) S) U: i1 W7 ^( O5 ]
$args = array(
$ D4 U4 O$ y, F1 A) o 'labels' => $labels,( f1 S, F7 T7 f- x' {
'public' => true,4 O& Q1 k" F8 }7 Q- U" e
'has_archive' => true,5 c8 v! O3 l a2 T+ T
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),8 A% Y2 a, W2 W+ {
'taxonomies' => array('category', 'post_tag'),
3 l& Q- L% a5 U0 Z& ~ 'menu_icon' => 'dashicons-megaphone',
- ?. [0 ?$ ^( Y r" o' ` 'menu_position' => 5,6 }# t' {) Z2 d' |
'rewrite' => array('slug' => 'site-wide-notices')8 N' V4 T) u3 U' m/ B0 B
);. ]. Y# D1 t9 ]2 y
8 ~5 y, f3 d! u7 r register_post_type('site-wide-notices', $args);
- v9 r4 q2 ?$ F s, L }
% H! U) ~3 w3 t! Z; R9 O ```& g# [( I4 W- i
0 D z- \; N' z: q4 j 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。, X1 ^5 O8 J: S
& n2 t% D% H* F3 W7 u, d5 v( }
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:: I h& T% ~5 m; q1 j) ^0 ?- i
/ {) I' M/ E0 O$ L% Z' ]* a
```
7 ~& t4 k4 H/ W) z5 Q add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: v% [" s! E* ~, w# w function add_site_wide_notices_boxes() {
# ?7 n& E7 e1 r& i) a add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
4 c; A- F% T; g9 Q2 N7 m4 e. Y }
% I& z H8 o2 x K$ J" _ V* t9 N u3 i
function notice_details_meta_box($post) {
# X3 g$ z; N" R- T( |) z wp_nonce_field(basename(__FILE__), 'notices_nonce');
( F5 x) u+ t6 @* p; M" r $notice_title = get_post_meta($post->ID, 'notice_title', true);+ Z( h" ^" j- U- a9 X
$notice_content = get_post_meta($post->ID, 'notice_content', true);/ L! p( z; [ }* M" V2 w4 m) v
?>- c5 q( F0 t2 [, k9 Y# F5 N: H0 ?
<p>
) {5 _4 l7 y! }. v9 s5 G9 U <label for="notice-title">Notice Title</label><br>
; u e8 m& n/ ^: h <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
Q3 `3 O m2 i5 W: u5 m& F </p>
4 W1 u" y3 w% t <p>- X o! x* u+ d _& k
<label for="notice-content">Notice Content</label><br>1 I3 n9 L( U6 P
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>' N2 B8 H3 |( \0 a% M" K7 a6 G
</p>
7 b' H, e9 a- M; j <?php0 ^% Q0 f2 l" y/ D4 Y0 t
}, Z+ J5 h8 } T3 |- d, w
& c) g( r. [) w9 E- H% W
add_action('save_post', 'save_site_wide_notice_meta_box');2 W; h. A; x9 N$ O5 c( O
function save_site_wide_notice_meta_box($post_id) {
' f) b' d# F/ f: z# o1 ` if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))$ P! [, ^4 @( _7 p5 F+ O
return;
) K# m, N6 ?! Z; W) J8 G: x if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% ~- k8 N( z- j
return;
q( }$ h* s3 ], T; |4 x0 m! e r; N2 c9 D+ |# G
if (isset($_POST['notice_title'])) {" T/ F" {" M: d
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));4 i' H* m! E/ z7 u0 v; O6 N1 g
}
' ^- \- h1 v$ z if (isset($_POST['notice_content'])) {; X3 v% E& P! f* [% N
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));0 p% k& x1 U+ J% n; t% t: I# {
}
+ i% R; v& X9 [9 W }2 e3 A# B$ U$ y) u- }
```
8 ]* P/ G) @! |6 C8 t) N, E& n7 [% ]5 K. w% X, B& [: G
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。, A; d0 o7 I. q7 O2 |
. k; c$ D# G6 ?5 q4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:* W! _4 M' x3 c# O; v! ]/ r
( U3 G+ U8 L9 @# x( l
```8 E9 m# k6 Z+ H+ w- L/ a8 p$ V
$args = array(
$ ^: L8 u" x+ [9 {2 `% d, @+ E 'post_type' => 'site-wide-notices',
8 p. e: y. Z+ H) G 'posts_per_page' => 3,9 T# p4 l( F* X* E' m* j
'order' => 'DESC',2 Q8 n2 i3 A) y- ]/ m5 _6 f/ }3 V
'orderby' => 'date'
+ e6 m% w) |. u i3 S2 e- P );
6 w& e* V9 e/ i) G2 e V $query = new WP_Query($args);* x4 E( c& S* A. i; `
if ($query->have_posts()) :/ T' h* Z; y7 E! e" i" a( c
while ($query->have_posts()) : $query->the_post(); ?>% r5 K' b4 K4 G; s! ~, h4 p
<div class="notice">
) b: v, S2 H2 j/ _/ x$ @ <h3><?php the_title(); ?></h3>
! l( {; ]* ?( P% F- d+ s <div class="notice-content"><?php the_content(); ?></div>
8 R, }) i; O7 j9 W ]) X, q9 _' @ </div>
9 a0 S' I+ _4 C- } <?php endwhile;, H' h! \4 w+ V1 Q4 U- Y& q9 f
wp_reset_postdata();( v% i/ f2 e. N$ [" J7 f( Q
endif;
3 ?4 q: x$ _3 F, P! M8 v# w+ m& e ```
( ]0 C" u' t% ?3 I0 f$ |8 L
) r+ P4 B+ T7 y: _+ p 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|