|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
7 t& @+ ?" y' d- K2 M# l, O
3 V+ v- E4 F* D- c8 Q" D( b% E- N: t; L如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。9 |8 c _5 B" ^& Q& D: h
; q( L/ b1 ~& C) i以下是创建自定义插件的步骤:1 B+ E# n$ C1 a: `( x
% x, l: c' Q$ w! f5 k
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:; l) F: ?) z$ o1 y6 y
( s+ s+ R3 Z. _0 f" o: U# ?9 {
```8 M0 S* f/ H; _$ H4 T6 @ r$ t
<?php. |/ v( M9 P. I& U( M& r: b0 A9 L: }
/** S# p$ R2 P$ y, F% W. S3 o
Plugin Name: Site Wide Notices Plugin" k4 |0 X* [' M* }& V) g1 e' j
Description: Adds a new custom post type for site-wide notices.
" Z5 M p) i8 A6 _5 j& O Version: 1.0
8 a( J( t$ w& D/ Z. s0 h Author: Your Name9 M& U8 k6 s6 w* T" S
Author URI: http://example.com
2 Y, t# U ?+ |" h6 B2 t */
$ m0 I5 b6 x5 [4 p/ ~/ k& R) M
// Add plugin code here...
6 O6 w, y" B4 \$ q3 ^+ P ```
, {/ _& H1 }( y9 S1 p) W H
" }0 L- u% U3 j+ N" G% r 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
h/ z" L `! m5 U G! w; O7 X/ S' Y3 `: Q* w
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
9 f7 `) F6 c+ Q A
- i$ i( z1 z! T) P ```
0 ~! M: n/ {, Z$ ?! t" o add_action('init', 'create_custom_post_type');- _- a5 w+ v$ |6 Q
function create_custom_post_type() {
% z' v. }. |- R7 M! Y: d $labels = array(/ d* t" f9 q* r- n
'name' => 'Site Wide Notices',& j0 n: g5 n8 m; Z% i5 m4 Y0 B* M
'singular_name' => 'Site Wide Notice',
) ~( a9 ^1 g- \9 W: g3 W( P 'add_new' => 'Add New',
* v* r0 [% q1 Z8 P" H, a( U* p- f 'add_new_item' => 'Add New Site Wide Notice',2 z( L/ V) T. e
'edit_item' => 'Edit Site Wide Notice',; M( Y! c( X, y) m
'new_item' => 'New Site Wide Notice',& p& t9 Z) I( P1 O2 q
'view_item' => 'View Site Wide Notice',: p; F( N! s& B( L; o6 [
'search_items' => 'Search Site Wide Notices',/ F$ P) h2 w9 u
'not_found' => 'No site-wide notices found',, E" _, S B2 Y- e" a3 C
'not_found_in_trash' => 'No site-wide notices found in trash' P! @$ N4 Z3 N% b+ N0 x
);3 E6 `4 U6 [( U% E; I0 T
" j) i( l( j! ^0 ?2 |5 _ $args = array(
- k1 f! x/ V- n. ? 'labels' => $labels,- g" T' s5 R" y6 \) E
'public' => true,2 q! c7 [5 O0 ~2 e% a7 U
'has_archive' => true,! f' q. P2 r. m
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
- U7 t% l/ Y% M! b+ ]# ^: w- |3 s/ I/ ^ 'taxonomies' => array('category', 'post_tag'),2 m N7 e* F) u- q: \$ h" W3 u
'menu_icon' => 'dashicons-megaphone',$ Z* d3 S' T% H6 {4 M
'menu_position' => 5,
, ?% \0 ~' x* Q$ ~- {! s$ t 'rewrite' => array('slug' => 'site-wide-notices')4 s5 u4 X; I& `3 w* k9 C
);
c$ ~3 O) C3 L9 w0 Z8 M0 n+ \+ q8 G8 R' |" u
register_post_type('site-wide-notices', $args);6 l" z! S S" H. _( n
}
7 L% A: u( P0 B; c% F, G9 u ```
" Y+ ~- S$ I% e0 X2 o& W* E% T9 t( J! d6 |$ ?
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。 y( W0 J( c% |( D: |; Q6 ^
4 Z6 E6 H; \7 O6 E2 E, }, E
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:! W$ \3 V4 Q1 @) o/ [
) c- g4 o! G$ S k6 r4 C1 [! w" o& v ```
) A* j/ s( E* b( \' K9 P add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( f, k8 Y2 P* X7 U' N
function add_site_wide_notices_boxes() { P! P$ l. x- C5 P5 }' W
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');) Z6 H. m. d Z7 W
}4 t8 m7 {) V' s
6 d' b4 ^% K e6 f" Y
function notice_details_meta_box($post) {( H7 @0 _; J8 [0 [) p& Y: _ ?$ f, R
wp_nonce_field(basename(__FILE__), 'notices_nonce');# j4 o. U; y! B$ ]7 L5 I5 m+ b
$notice_title = get_post_meta($post->ID, 'notice_title', true);
1 J4 O, v2 g4 f/ O3 i; r( [' |# x) c $notice_content = get_post_meta($post->ID, 'notice_content', true);# j9 M0 p L- A$ J
?>
1 L; h2 }% w. g9 H' [' e% b( f <p>/ U% ^+ }8 Y9 \' u& ~/ D( _
<label for="notice-title">Notice Title</label><br>4 _+ D4 }8 K: w$ u
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">; x) V) [ {; a5 k& o
</p>
; W$ ^$ e, p% w; N% i* o" c <p>% I' [! R% f2 S" ]) L8 H3 Q
<label for="notice-content">Notice Content</label><br>: K& d' O) z9 J w! p! S7 u
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>& T# c- i9 P. }; }: B( M
</p>, W7 u" e2 O$ ~* @4 C- k
<?php" l* R* j. X& {, F- o
}
$ a6 z6 J, K1 \% K# L# i
" m& _9 T/ }' _; t8 | add_action('save_post', 'save_site_wide_notice_meta_box');
2 [, s7 A) B! B5 K- Y9 |5 f! B function save_site_wide_notice_meta_box($post_id) {
# @# ~- H0 b" C; ~( G* o: g+ [ if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))), ~: [. N7 w. l1 M" `
return;
$ o8 Y# A. O& n2 W# c" z$ y if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)7 o8 ?4 E! n9 Z; }# s% W/ q
return;+ H* _) Y+ ~2 ~' o7 o- G# `0 b
0 R2 Z; R. C- c# [ if (isset($_POST['notice_title'])) {; ~% ~) Q: z, I ?
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
+ U, v* }7 F4 t$ }* H3 P! I }
) X5 ]' v4 x% n) e U! p& f& n) B if (isset($_POST['notice_content'])) {
+ x# U" z/ W5 r3 N% i update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
7 z6 N$ x2 L7 O" n" Q }
+ N* P" h5 w4 o6 Q% T; z; u4 \ }# ]0 _# ?. W$ N: } ~; \
```0 H. l' s7 V5 x& Q. q) H2 P6 }3 n
5 q- Q0 m& H6 }5 V% \
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。5 Z& _0 d, L* ]5 p
5 `9 E0 p/ r) [& `- t
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
4 w5 |$ t8 q0 Y+ _8 H6 P
" ?( X( k' h4 k1 g- X ```
5 E/ W# b9 R u* z& @ $args = array(
4 X1 s2 [; y3 W1 M 'post_type' => 'site-wide-notices',2 S+ Y+ o5 O; J- x$ h
'posts_per_page' => 3,! l* C: w( K. M6 x- f
'order' => 'DESC',
8 O5 s7 ], a. Q' o/ i 'orderby' => 'date'
9 P% Z# ~; v* _8 H5 G( b9 u );
5 \: Z1 h$ Y1 G% ^# R3 q $query = new WP_Query($args);! s( H3 ^9 X, u K
if ($query->have_posts()) :
. d1 }5 i/ a" d S2 O8 Q- p while ($query->have_posts()) : $query->the_post(); ?>
, k% q1 b0 m; Q/ B <div class="notice">
4 s; _7 C: h' S" q <h3><?php the_title(); ?></h3>
3 a7 ~+ r9 i: I+ u" c3 n <div class="notice-content"><?php the_content(); ?></div>1 Q. P: O1 N# w4 b9 Q
</div>8 D7 _6 ?7 p# ^, _& ]" T
<?php endwhile;
4 u+ L4 l; B$ s' t# N; y! v1 S7 i+ N wp_reset_postdata();. S+ g% S! E5 J. D9 r" T
endif;
% J& R9 N) H+ ~7 X/ c) c# P ```
6 ?7 p2 t7 K3 v/ P
2 E; u$ `1 D( j# `, R# z2 u% V) r% h 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|