|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
2 t% e5 f! r" @/ |! F: N& j. z# s6 Y' r/ W7 f
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。( F6 C; N. F5 c" K7 e- S' P: _
. g1 u. V* Y. v% v) L8 n
以下是创建自定义插件的步骤:6 M: ~# v" A: c4 T/ u/ q
+ l3 j9 S1 I3 R3 f2 M$ J' _' x1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:, E$ v! X) I! r t' [& g
2 t, n: R; l- P) a% _ ```( H% A! X9 \& U9 h2 C4 N8 z6 i
<?php
' V( B) X& ~& G /*8 y9 W% M6 E+ A
Plugin Name: Site Wide Notices Plugin% s9 n. D. |" n9 E
Description: Adds a new custom post type for site-wide notices.) N+ q8 p3 u5 a! S, ?
Version: 1.0
; Z" X) E+ ~. H3 g+ E# O7 ? Author: Your Name
- z: h. D6 X. c8 J, t: N" \ Author URI: http://example.com
! J! A4 l( @2 N& z0 i* d$ X$ f/ j */
! O+ m% O$ Y( ?% A4 d% N: u
( d9 m8 W( @' Q' D% { // Add plugin code here...) A; K7 o' j6 W2 Y5 t9 W4 N" U( k- w
```
: H5 M' k- T( w6 |" I5 f6 z/ t6 Q3 p* u
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
* J; C$ L y: L2 \* e. k( T1 N c% }7 W; X! @" [# U5 [
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:! K- J" v( b5 U% |) ?
0 d) ^$ z* z* R/ h
```
' O0 H; t8 l% R add_action('init', 'create_custom_post_type');1 z B" |# H8 F: }
function create_custom_post_type() {, \# V' ~5 M4 ~- v! j
$labels = array(
+ P6 S" x+ Y4 Y/ {. l) Z$ k; v 'name' => 'Site Wide Notices',5 r" i2 n% C$ `2 u ^- R6 H: Y
'singular_name' => 'Site Wide Notice',
' }4 }# U, X+ Y* t 'add_new' => 'Add New',- ? p/ I) ?- e
'add_new_item' => 'Add New Site Wide Notice',# a( D. e9 s3 q( U7 u
'edit_item' => 'Edit Site Wide Notice',
$ b, v& e1 l+ ]2 ]/ D- {: H 'new_item' => 'New Site Wide Notice',
$ _& ?0 }7 I- r" r# i( Y k$ U 'view_item' => 'View Site Wide Notice',% |8 D" o$ g1 X) z: Q' g) C2 S2 ~
'search_items' => 'Search Site Wide Notices',
& |: {6 Y( a7 u 'not_found' => 'No site-wide notices found',* o& c* P' b, o
'not_found_in_trash' => 'No site-wide notices found in trash'9 s8 b. T" q& N8 q, ]
);
! K4 h; l7 ]! M$ e) X* f7 [
, Q, u7 f8 e' T0 e- z; b5 s) l $args = array(4 V- ^: B- `8 M5 Y G
'labels' => $labels,
$ Q3 c& W8 d8 D 'public' => true,
! |% ^: X' F! u1 j 'has_archive' => true,* t& I4 L4 \. _# L! [! z7 I
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
$ o3 u3 }" T" k! l 'taxonomies' => array('category', 'post_tag'),
0 V1 N7 K1 g+ d' G% y0 e, _ 'menu_icon' => 'dashicons-megaphone',
\5 M3 u1 k5 v4 _ 'menu_position' => 5,) w# Y9 Z1 a8 p r: N
'rewrite' => array('slug' => 'site-wide-notices')& m. w5 g1 C. x2 I( w
);
% M+ S# b/ v" s7 N g8 Y; g) x
" X- P! K( K/ t; _& W4 z9 _" z, V register_post_type('site-wide-notices', $args);
: |/ z/ F; s7 ?& ~ }
& m- { n4 {5 D" q ```. O5 |( J9 b+ V7 P
# ^! {# r6 q" B- M
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
: z ^2 ~) D# S( k
; H" u4 \2 D3 m+ U1 k& j3 Y3 K3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:# S+ ^4 G. \" u/ u& @1 t4 E3 l
% D7 ]$ ~( O: E
```" ^' {( R$ j0 V/ d6 ?8 q9 j
add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
7 e2 y) m* T3 [& o function add_site_wide_notices_boxes() {
K) a! @4 l: T- N' W+ B1 V+ Q add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');+ k4 O5 v# e& C8 k/ W5 Z# i
}
* M- G' T" N2 Q8 `
9 ~$ A# _. p5 p' ]" C/ F5 p+ s function notice_details_meta_box($post) {1 q! }( S/ D3 K5 i: D4 a7 `2 c7 x
wp_nonce_field(basename(__FILE__), 'notices_nonce');- j+ t0 f" J+ S8 ]) Z
$notice_title = get_post_meta($post->ID, 'notice_title', true);# r) n$ x2 S0 w M) U
$notice_content = get_post_meta($post->ID, 'notice_content', true);
2 i a' D( y( c" p7 D! f ?>5 v/ \+ {, E# e; S, w5 j% m% h
<p>
! c, E" j. @7 [# J; c6 @- ~ <label for="notice-title">Notice Title</label><br>
K" G# h$ ~1 ]0 [ <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">3 e0 K& E# B7 i3 L( s/ N
</p>
4 a; Y& C* J6 P* _; \ <p>
# ^" R' @- n* l6 _ <label for="notice-content">Notice Content</label><br>3 Z7 f8 {! ]5 P' @/ t
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>- Q' S" R$ Z* k0 p: |2 s# f3 }
</p>9 {' O) [8 n; [% a9 i, E1 ]5 a
<?php
/ T+ {" y( k s }
7 U- \; X: Q" v5 U, `+ `/ \/ C" r, u" E+ C, K* ^; A
add_action('save_post', 'save_site_wide_notice_meta_box');# r# S/ i7 y; y5 _' W
function save_site_wide_notice_meta_box($post_id) {
9 z5 F7 q9 ^, J( q3 R; a if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
' r+ h% n! s# \" G, ` return;
9 ^* C4 g" z% {& h if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE); W; h/ v5 G7 u, {. U! L
return;
8 d# ?1 l9 w9 U2 S( _
8 F2 }, A% U. ^$ x: b3 d* p if (isset($_POST['notice_title'])) {
* K% V5 b) D' l update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));( Y. a, P" n X5 I r) f
}
1 e X# W% O' F if (isset($_POST['notice_content'])) {& C# b6 y P! R
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
; y+ d4 {% ?5 k3 G; G/ c }0 k2 p& Y. h( D8 u) l' L6 i) D$ H' m
}! H- {/ m$ ^! ? K, y G1 T
```6 z$ y6 Q8 V9 m! \: c8 N
+ C b! m; E6 N
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。8 A: w. U; F" G2 I. D
) Q$ t; z& e1 E4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:# E- J8 S* X! C* K) H8 ~* T
7 G0 z+ U: p3 t% [7 K" Q h6 ? ```
& l% u; m- |7 w. W $args = array($ S& }+ q1 h( F
'post_type' => 'site-wide-notices',
% w* p& e3 X. Z& O5 {/ Z 'posts_per_page' => 3, g/ m7 x4 _& ]- g) w: Y; Z* @
'order' => 'DESC',5 z1 Z: _- c6 a7 p- t3 N
'orderby' => 'date'$ o4 z6 I$ `, }: x
);7 d( D: B. a6 d I. R/ ~1 p) t) L
$query = new WP_Query($args);
/ `+ a4 }; H4 M# E) Y3 ] if ($query->have_posts()) :, V3 _1 M! H/ [3 x: V: k
while ($query->have_posts()) : $query->the_post(); ?>
+ n- u5 n9 [" Z <div class="notice">
' o! r1 o' i( r2 Q1 j4 v3 V <h3><?php the_title(); ?></h3>9 p5 S" o( a: P+ x R; c+ L
<div class="notice-content"><?php the_content(); ?></div>
* o% s. N0 Z- h" j </div>1 E, N2 U1 T- |
<?php endwhile;
4 I0 a2 J! V% S; w; M k9 ]& |0 U) u wp_reset_postdata();
' \6 f& i- O7 @- a* a endif;
6 } ^7 v4 o2 G3 B ```
$ }4 O7 t! b* ~ [; M8 z
: o/ N0 [# d" Z( I( s 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|