|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?* O4 S7 H) n$ T; ~
, z6 Z6 g7 Y3 F2 `+ t如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
: N0 C+ l9 `* B7 b& k
9 O2 N a$ _4 z" W% O$ D以下是创建自定义插件的步骤:" N4 C/ G0 y& Y6 G8 _: }, Q3 g
- s7 v- ~$ m" z$ T& C9 Q! d1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
) q! }& Z+ t2 D6 A, J/ u x3 y! ^1 y6 ?% c
```+ F/ L8 w y- z1 D, h7 o( h
<?php
! _6 I8 l/ O1 c. g, ] /*
( W: T+ C, m2 h9 N$ a! [9 M: E' q Plugin Name: Site Wide Notices Plugin
0 M4 k) e! [+ C2 Q% o5 { Description: Adds a new custom post type for site-wide notices.
9 K: |0 \# E) G& { Version: 1.0* c! w# m6 X% Z) E% k: x
Author: Your Name
* ^- n& h. w* b% b Author URI: http://example.com
7 E g7 d/ L/ |6 e7 X" ]; ~. `+ D */ \2 a4 N' q3 |! B$ D7 C: H) s% Z
- B! h( X' c7 r# X& G9 @% J
// Add plugin code here...
- `2 q* j6 X) h. k0 ?* d/ Y ```
( p/ A! S/ i; Z0 X; U
# A2 h$ N+ w+ P* O8 n Q 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。5 P1 J0 R9 N) _; P1 O! `9 s
7 t9 f2 X; Q, l e2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:+ E: D5 M% i) j9 D
9 y2 K! `; o6 [' `- A
```9 [. Y" o- m) i; s$ H
add_action('init', 'create_custom_post_type');' X! q/ Q5 @* e+ H3 \$ S
function create_custom_post_type() {" o( e2 Q0 L' E$ N( A8 [
$labels = array(/ ?4 X. z; C! |( r, e' n" j
'name' => 'Site Wide Notices',$ z- z" C' i: Q! v3 Z9 V1 e+ K0 {
'singular_name' => 'Site Wide Notice',
" o1 E1 k. c) s5 {9 n 'add_new' => 'Add New',
+ b) S- O# |" ~- r% H* W0 R2 K 'add_new_item' => 'Add New Site Wide Notice',
. V$ V& ~2 z- ]6 d# L6 S 'edit_item' => 'Edit Site Wide Notice',
6 {% L8 x9 y/ j. n5 O; i 'new_item' => 'New Site Wide Notice',0 O, a" F& p) q/ o3 v% u* w
'view_item' => 'View Site Wide Notice',
5 j+ a; ]( x3 Q( v 'search_items' => 'Search Site Wide Notices',7 j1 E7 j% {7 w0 T- I5 w
'not_found' => 'No site-wide notices found',0 O) M2 U- F- U
'not_found_in_trash' => 'No site-wide notices found in trash'( C! Z/ q5 S6 S" V
);
* s R3 L- ~0 _% K* D& ]9 o' f( S! j& O$ s1 E
$args = array(
# ~" u1 t; N" A: x7 c 'labels' => $labels,/ d5 C- H& j! B4 s% z0 p( m( H8 f
'public' => true,
- M8 V! N j4 [8 y 'has_archive' => true,; `9 T; y! }# b& b8 D- f
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
2 Q# X+ p9 y- F9 [ 'taxonomies' => array('category', 'post_tag'),$ w3 v$ z9 V+ T6 e( u7 C9 I
'menu_icon' => 'dashicons-megaphone',2 [1 P: {3 I) G) B: o5 B
'menu_position' => 5,# t* u: e2 L! q& l
'rewrite' => array('slug' => 'site-wide-notices')
. x. |9 }$ i* n2 @' b );: ]- x6 m/ A' Z( c q' X) N/ D
; u6 [; q8 _* ?8 } register_post_type('site-wide-notices', $args);2 \$ Q$ D/ c2 c0 v* m
}
z6 h0 O, z' L% }2 d" O$ f0 @ ```
' x+ E! `$ O" _: L
5 e& q3 h4 @+ h4 [ 在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
7 p! b+ k {9 ^5 W# R3 @; v9 o) W
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:# U: F8 O( p, q! V
" @6 ^9 v- H% Y- k H! P, @ ```
$ v1 Y& h# ?: o f1 y+ }3 _; m5 x add_action('add_meta_boxes', 'add_site_wide_notices_boxes');" }3 V8 Y/ c7 ^1 A
function add_site_wide_notices_boxes() {3 Y& Y. M6 j0 D
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');+ I$ F; y" o1 r: U5 G6 [6 \
}
l; \( S7 B1 p9 y: t2 c! R( W4 F( q+ ]
function notice_details_meta_box($post) {4 s o5 f; L& b! ~8 ]. v
wp_nonce_field(basename(__FILE__), 'notices_nonce');8 w/ e" r6 \& A9 B' k$ V) u {
$notice_title = get_post_meta($post->ID, 'notice_title', true);
' p; N+ M/ L, Q1 H$ ~' y $notice_content = get_post_meta($post->ID, 'notice_content', true); P2 M9 ?: L4 c6 O! }" D% |
?>
- D" ?1 |% I* B9 ^4 d5 X <p>' S$ b# G" l) a- R. V
<label for="notice-title">Notice Title</label><br>7 d% N8 W( B) {* |
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>"> n$ n; t( Q9 y* O* Y/ M" U$ \
</p>
; l- T+ W- i+ W) a" I5 B3 C" D$ h5 r <p>1 Q& Y: B# ]' _; L. Y
<label for="notice-content">Notice Content</label><br>1 o4 ]0 b O2 t' L. G* Q3 l
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
; l$ G/ t! Z- K, K: q </p> F6 z, b& D( |. k7 x& i
<?php
, u+ |) `% z3 N }+ D- _; L; n9 o+ i
# ]4 V! @' u% u- p ]" `* P
add_action('save_post', 'save_site_wide_notice_meta_box');
. S' _4 k9 \6 |- A function save_site_wide_notice_meta_box($post_id) {0 O5 Q" X. d7 _5 H
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))% M( S' B% U& @
return;" u% l/ Z [2 P; D; ?" P
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% h1 S; X3 G% N% f
return;
6 {, _4 D7 r2 m0 K2 z8 ?# _/ x$ E& o' y$ @$ n/ L# Y1 ~
if (isset($_POST['notice_title'])) {- I9 x! j2 l9 J8 q5 m' H
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
$ i% z( S$ r! M% g ? }# \. l. i7 Y+ y
if (isset($_POST['notice_content'])) {
8 r Y5 o3 H. k update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content'])); ?6 K* i$ P% z! o
}
( \' N7 ~# B& t. w3 Q3 N }+ B O# r4 e' K& y
```: @1 U) l7 k; ]* X) X
4 n4 N8 v6 s" a* G% Z/ M) x/ X
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。. [& e; k( P. ~; ?; g! X9 E
2 e5 t. r, L/ K4 e
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
2 Y. W/ {+ Q- \; e$ y2 Z
! B7 k( K7 [4 {& T3 j* d5 { ```
+ @& t' v6 a. b N) f $args = array(" a% i2 o+ F% t8 j
'post_type' => 'site-wide-notices',; s5 d, f) g/ H7 A/ r
'posts_per_page' => 3,' f6 @6 r- H' C& v9 x* `# ~
'order' => 'DESC',
. g$ O0 q* z# H- t: F; L+ D( H 'orderby' => 'date' E6 @$ a- ^* Z9 z, l
);8 z9 ]2 m7 ` [2 o
$query = new WP_Query($args);
7 }4 e1 G( d: \- ^ if ($query->have_posts()) : g i- x) N2 M$ j
while ($query->have_posts()) : $query->the_post(); ?>0 V0 S% h& Z/ Y' s; @
<div class="notice">0 ]" Z! n' ^0 B( O
<h3><?php the_title(); ?></h3>
, J' `- I# Z" T4 ]1 A% j+ b0 k1 v <div class="notice-content"><?php the_content(); ?></div># j! P; L# o( i9 i
</div>5 {4 A S! h! F( c2 P9 H S
<?php endwhile;
& ]/ [" z; [8 l/ `1 V7 V wp_reset_postdata();2 \' a# W8 W: r4 I# y
endif;
: ^) O1 Q, R8 ?* g ```
& X2 S& W( m6 f6 {/ ?1 @5 T) W+ f$ e- t
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|