|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?+ G) j' `. Y# [: L. e. G% Z
! _ d4 V5 R# U' o2 n+ }6 M如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。7 h; x( E# U, \& ]" K. |
2 h" V/ J: I8 i; a8 Q
以下是创建自定义插件的步骤:: l. v2 R/ |! g9 h; m) r5 c
# _6 w- e& c1 `5 U1 P1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:) G t% m* S; ]/ {
! b3 R% k3 w0 v$ U: @ ```3 n- V1 q/ u( E+ [3 C
<?php
0 |' P# |9 ]3 r7 p4 o0 Z" u /*
^; X8 w5 f }( r" O Plugin Name: Site Wide Notices Plugin
/ p G/ n& G/ I) N Description: Adds a new custom post type for site-wide notices.$ l4 h. B: D' B
Version: 1.0
1 H- Q, G g* S3 ^ Author: Your Name
" A% i4 ]" }# U& C |6 \ Author URI: http://example.com+ E1 ~0 @) Q8 s+ ?8 A( r& S
*/
: H$ O9 l+ O' C/ u9 r, i8 ?0 \) `, S& j* `; M+ s* u5 Z4 t" ]$ X
// Add plugin code here...
' b& \0 o) ^0 v2 ] ```
& I+ w2 m; a! T2 _ @0 h! ?" P
! l2 {& F6 w/ B/ W& A 在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
" B1 n$ u% ~! S* ~. l
- f$ G3 _) F8 I b& P2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:0 [4 x; P' k8 f8 |( L8 c7 m8 a
1 r+ V3 k F! p# s( r4 Y. ~- g) C ```
* y' e) }' m$ S B2 m$ X add_action('init', 'create_custom_post_type');
# Z( z( s- a# a) @ t2 A function create_custom_post_type() {
2 c1 N) O! h4 P% V1 ~. y $labels = array(- E/ [. k: z5 |3 { z
'name' => 'Site Wide Notices',; M: w, m" F; U- L/ c6 n
'singular_name' => 'Site Wide Notice',
; K9 v& F; v! A+ G& X9 j 'add_new' => 'Add New',
; N5 m0 M8 [! P3 M" N$ g 'add_new_item' => 'Add New Site Wide Notice',6 E6 w' T9 C3 u3 p9 k
'edit_item' => 'Edit Site Wide Notice',4 M8 H1 B- A4 L1 Z
'new_item' => 'New Site Wide Notice',. _7 ^' {2 V4 C G
'view_item' => 'View Site Wide Notice',
/ U, v9 A4 b8 y. P5 c+ T 'search_items' => 'Search Site Wide Notices',3 { B; s: {: f3 | f7 t
'not_found' => 'No site-wide notices found',7 g) y8 `/ {9 L( f
'not_found_in_trash' => 'No site-wide notices found in trash'
" E/ ?0 o5 y' B );2 {. h2 L2 j+ H4 m0 R
8 d' G* H1 f5 T* U! ]' [ $args = array(! K8 k' f% U: `5 K' r; a
'labels' => $labels,
6 ^& \ K2 B0 I1 p% A1 g; A/ D 'public' => true,
! x B! j! x! }9 } 'has_archive' => true,& F4 P; x( E! R
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
0 V# e4 u) K1 d5 j0 e0 E( j* k/ [3 l 'taxonomies' => array('category', 'post_tag'),
1 C3 ]/ A' h+ F* F; A 'menu_icon' => 'dashicons-megaphone',5 U4 H1 `3 ?5 S4 }3 `
'menu_position' => 5,
. v& @& p, F+ v$ B/ ? 'rewrite' => array('slug' => 'site-wide-notices')! _3 n9 _. ^# s$ p' h& [: A
);
: n) c2 O& n0 @" U$ Y, }3 O; p' ^6 g+ H9 ]8 }9 k9 t% L
register_post_type('site-wide-notices', $args);5 R& N+ X; o9 o5 u" o, m6 m4 a* b
}
& P% t' I* }: Q- ~ ```# Y* D) H4 T. l# T8 ~) o
! O# _+ ]7 A4 H( Z" k! S
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。: z9 t% t' |2 @+ K5 m
+ e, Q3 G% j, m2 f1 Y: p
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
+ H* ?( ^( A0 H5 {5 S$ q% {, }' z! Z2 a4 f& j: l0 y! S- }
```
, v; q( _ g6 r f( r add_action('add_meta_boxes', 'add_site_wide_notices_boxes');, F; {2 b1 K. X9 h2 q* w2 |: o6 L$ r
function add_site_wide_notices_boxes() {
) l( \- G; I! W( } w7 Q( ^2 W3 h add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
4 q a$ d6 n+ {! [/ C) m& l }
! W2 T$ Z& m: T: U0 I
% I6 f/ f/ D' f7 [ function notice_details_meta_box($post) {2 B, @5 q, D& P5 V! `$ _0 E/ ~' m0 C
wp_nonce_field(basename(__FILE__), 'notices_nonce');
! u. g/ {* ~2 Q8 ^( ?/ l $notice_title = get_post_meta($post->ID, 'notice_title', true);
! x1 O9 a; k1 p, t $notice_content = get_post_meta($post->ID, 'notice_content', true);
9 Q. V6 p% y2 u; U% I, A0 A ?>
6 I( j0 P y" a/ }. j3 M4 I <p>
9 \( a; f' \; b <label for="notice-title">Notice Title</label><br>' L6 S- J, u O' p
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
1 X0 n4 D' [' r8 w </p>
5 w" r5 M' n" Z9 l <p>
0 w: p$ P7 e1 E7 l1 x+ o <label for="notice-content">Notice Content</label><br>( l f$ ^* o' Q( q+ @, B. m
<?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>1 a, m8 a; ~' P# O' H1 }% W
</p>$ f( D; T, T S2 ^- I1 Y W( ~
<?php0 D8 ?3 Q B. C3 Y) g4 q
}4 f$ y$ i1 @# v. f( a7 e/ d$ v
1 t* A( Y0 z% r. {8 l ~% n7 t
add_action('save_post', 'save_site_wide_notice_meta_box');
$ ^$ }& V2 s9 X# ]+ U* a. I function save_site_wide_notice_meta_box($post_id) {7 \; d2 x4 y% ^ `9 @7 m) z- T/ n
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))1 k2 q( t3 ?' _, k6 i7 z
return;
! F6 |( R) n/ ~5 w1 y if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)5 }5 P: I; r+ n- m
return;
: V" B, k; N8 j7 ~9 \, V R" ^/ i$ k1 e- d* M1 [
if (isset($_POST['notice_title'])) {
# S# \# b7 H) @& ? update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));! d# t$ z& Z! R5 C5 j1 S* u
}1 o# @5 N2 k- p8 d+ v5 z- I
if (isset($_POST['notice_content'])) {1 q0 J# [2 i! T1 K
update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
. O0 I x: u2 U# z# D ?7 Y }
; m) B. p% U( `% d6 L9 H }# q9 S8 o3 B4 t, n6 r" e; k
```/ F" `. `* t: E. n7 r
+ {" t0 o# l0 N
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
% s A9 |8 z" U9 ?
- A# ~" U! y3 Y+ Q# j4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
, z* e" y2 @1 v/ t/ B" N5 v p( \. S# L- P3 j+ Q: \
```( m9 ~& f' I' B! V* q) R
$args = array(
( h5 O6 b- r5 c& |' l8 R0 ] 'post_type' => 'site-wide-notices', S0 ~% U' n7 R ]
'posts_per_page' => 3,
8 P j. x$ y; D8 t 'order' => 'DESC',
5 K. Y1 y0 l- R$ \* [6 b 'orderby' => 'date'8 n/ H0 k* x9 t9 y" o$ E. N! }" Z
);5 R; N. a/ R- m1 p
$query = new WP_Query($args);8 H8 A6 c$ {8 a
if ($query->have_posts()) :. o( y/ v# _. S/ r3 g& Z
while ($query->have_posts()) : $query->the_post(); ?>
) u' `% `& o+ E) }5 V% x* [ <div class="notice">
% k# ^+ S3 V) [7 \* ~" P. R! l <h3><?php the_title(); ?></h3>0 w% W5 ?1 h, Y( T' R
<div class="notice-content"><?php the_content(); ?></div>
2 M# Z5 A9 I# @+ E Z6 i1 }. _ </div>+ \/ q! T' F$ R5 E
<?php endwhile;
& J- N8 @: G% A4 m% I wp_reset_postdata(); E* Z! y2 F/ ]; G' A. @% S
endif;
- v5 B$ `7 Z* g8 ~! P1 \ ```5 |" S0 T/ f# a& b" }) I+ x6 T- P
- |( O1 i% z' S# _6 z; u* V 在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|