|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
, K8 f4 Y6 ]: ?# E
) {- v; x) G2 E, E G4 |如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。! k/ b$ X& G% E4 Y
% y, I7 O3 ?& t, g' j3 `7 w- g以下是创建自定义插件的步骤:- c7 ]* m2 D- G2 V4 n4 `6 h5 O# J
- |* [3 V) Y8 {% H6 Z+ t/ r1 V+ F1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:$ a+ Y. i% G: @9 x6 E# V% H1 z
. E( x- c2 b2 R5 e* N/ m
```
@% @6 a! \: q. w! ` <?php; b0 @: y" O( Z& ^+ z# c
/*% ?/ J) l* }$ I$ N1 r# K5 W% a/ M
Plugin Name: Site Wide Notices Plugin1 u- w, \2 u1 k9 a( A7 H
Description: Adds a new custom post type for site-wide notices.
9 e4 O; J, ?3 c Version: 1.03 K2 B! E0 E, d- }9 q O6 ~
Author: Your Name; F+ }$ G* q, Y1 o
Author URI: http://example.com
% X( ^# e# X8 l- l2 T9 Q */) g) z9 D! v& l6 j
# S+ _1 O0 m! M" w0 x* t# c
// Add plugin code here...+ c; d; c* t! y% t( n; m' H) A
```
1 t0 M) n4 T0 c8 D1 J1 `3 q8 w+ K5 e% S" D. F0 Y+ i
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。1 z! n3 L, \, {* z, _( R5 ?
) T* N ?9 E+ {9 k8 ^6 |# L2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
, R {% m6 r O: E/ K! k5 x" A- @# ~- O
```8 @7 C" [( c) U! ]6 n
add_action('init', 'create_custom_post_type');7 d. E) U& C$ w: u: l- D% P! y) O- T
function create_custom_post_type() {
, G2 W/ r' Y4 t. Y n. u8 X0 ` $labels = array(- [0 N0 b2 m2 {* k
'name' => 'Site Wide Notices',/ W4 g* w% R$ R6 `9 X$ v
'singular_name' => 'Site Wide Notice',. B8 s' k. }; |& P6 H% F* e
'add_new' => 'Add New',
; K4 Y! {) O* V0 `: @ 'add_new_item' => 'Add New Site Wide Notice',) W3 C: k% {& r
'edit_item' => 'Edit Site Wide Notice',
: w+ b. M" ~* y: m3 Y2 ~ 'new_item' => 'New Site Wide Notice',
@3 ?8 N) @1 I# U: c! T! e 'view_item' => 'View Site Wide Notice', U7 a. q* B+ }9 ~. K
'search_items' => 'Search Site Wide Notices',6 N2 v" _3 f+ Q( ~1 t! b; Z. D
'not_found' => 'No site-wide notices found',
* b1 i7 L5 U! d 'not_found_in_trash' => 'No site-wide notices found in trash'
$ ~' [& q5 u$ r );" A8 U; Y; m; E
: m+ ^! r+ A9 O# b% W6 P# \ $args = array(( `* f3 u" b) x. ?' h
'labels' => $labels,9 ~5 G. i' o5 D
'public' => true,
# [' ?7 e" a3 n: q7 i: X) M6 u 'has_archive' => true,
8 k6 {+ q9 G2 B$ N 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),2 }! k7 u. @& w w
'taxonomies' => array('category', 'post_tag'),
2 t- M; k& W9 u4 j5 H 'menu_icon' => 'dashicons-megaphone', w, t, L' ?$ U$ U; v8 T, \
'menu_position' => 5,+ J5 S7 C4 H6 X( A
'rewrite' => array('slug' => 'site-wide-notices')7 ?: X. }/ V3 U6 z& g
);
: k4 C& `" V$ U$ e6 m! }
7 c/ L# f: A7 H: ? register_post_type('site-wide-notices', $args);+ s2 N0 ?0 J3 e3 i h
}
: c9 ~! ]- f* u1 c ```
7 Y+ w" {; r- A6 T" F' I
" J u G$ `5 h( k% E5 y6 i 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
: X7 Z1 p# p7 [0 { f! o, v7 s$ ?+ G
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
' c! Y7 k' R+ }8 |+ v, d1 ?8 `% Y/ O- T" `( w
```
) T6 X" d7 l: ^0 n5 g- \, v add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
2 l- P# N! G0 t' {1 C% U function add_site_wide_notices_boxes() {5 F. Z3 @6 Y7 r: \: T
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');1 m$ A/ J6 b0 h8 K" _8 i
}1 r' d* ~ [8 c, q" @
* f* z7 K ^; D4 c+ C function notice_details_meta_box($post) {
' z3 m& k* s5 k$ o# n! }% \ wp_nonce_field(basename(__FILE__), 'notices_nonce');. X. r5 l9 d9 V! j3 [) z( }( ]
$notice_title = get_post_meta($post->ID, 'notice_title', true);
" G* ~1 i9 l% q& q $notice_content = get_post_meta($post->ID, 'notice_content', true);$ [2 v$ e9 N8 s: P- R' p6 @
?>
0 n8 q" s0 x, E" b$ c <p>8 ]4 _5 R: q! V, y( d
<label for="notice-title">Notice Title</label><br>
6 R; n1 t, a% L0 [1 X <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">0 O- W4 m5 B+ A& n% |2 R4 B; ~
</p>
% Q8 m$ b" D9 j" t" d <p>
' k9 e# k1 K* J; {# z; L, j <label for="notice-content">Notice Content</label><br>- v! q7 V& G h. z E
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
/ O" C/ q: n# N2 _$ D4 k; n </p>/ Z. f# R% a! N+ M/ x
<?php
. d0 x5 V9 X1 `) T }* I3 [0 d$ ?% C4 P0 C% s
' E, E/ f8 f: Q" z2 E, V5 _ add_action('save_post', 'save_site_wide_notice_meta_box');
0 `( x& z/ T# l function save_site_wide_notice_meta_box($post_id) {/ J Y6 m+ e6 ]* M" Z
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
i$ F2 ^" p$ h9 {/ E return;
0 X. q9 y0 S* z P if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% o' Y; s$ M# l
return;( d' x1 k) @- F5 O) i& `
8 ], ^! e( R7 T) a4 q% f7 Z
if (isset($_POST['notice_title'])) {
: T( f3 h3 j6 p update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));- t* b+ x, `. p) E- F
}( E6 a9 L8 G) v4 g& Q+ n6 c
if (isset($_POST['notice_content'])) {& Q5 K9 {" a7 f. l* P
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
8 |4 H3 V( k! h* }$ ] }
2 V! {; `+ W4 [1 N }
) r' E6 ^9 e* R( S9 e" B2 H( z ```
& `5 O( n1 B6 g( `5 q
1 Q% ~+ X1 d- {5 C8 d9 }, C 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
7 `2 A) \" F. @! D( e4 s. G* T1 Q& z5 m: u+ U9 H) x
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% ~/ q6 E8 x/ `
- P" ^2 c+ l9 X: a. y/ n9 S' r$ J ```
2 k& h7 C$ J; Z9 g% I0 p/ Y. O8 M $args = array(& Q9 x9 J/ I( l
'post_type' => 'site-wide-notices',
7 h$ T8 }; q+ E- x; A 'posts_per_page' => 3,8 _# |3 C' i6 c- Q
'order' => 'DESC',! p6 a7 G6 B: P& v: I
'orderby' => 'date'
1 \- T# c1 n( D2 U1 {, b1 l );9 B( T |, L1 O& T" P5 ?' M+ {; P
$query = new WP_Query($args);" N, D0 f2 q/ q
if ($query->have_posts()) :
) q( m5 E; {6 x* \; w2 R/ w while ($query->have_posts()) : $query->the_post(); ?>+ C7 b$ Y8 O, z* Q" _% D- N
<div class="notice"> ?. R3 ]; f$ s, o2 ?
<h3><?php the_title(); ?></h3>
" x9 t4 `6 e. O, H <div class="notice-content"><?php the_content(); ?></div>4 x: ^0 Z) {- X2 f6 K/ P `
</div>
( F2 Z- e( g+ U) V* V <?php endwhile;
6 F" @6 p/ e+ P: `5 {, K5 z wp_reset_postdata();
$ v: {4 O% t, m endif;
+ o+ u% Z) |+ `6 i0 W' n ```
4 w6 s5 W \+ i5 i1 n1 Y* ~
% N) l y! J1 X" u- i8 \- T+ N 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|