|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
5 `0 e1 i$ \( P, K: _& C9 A4 }
/ c/ n: i3 B1 c5 \( a如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
8 D3 D8 V1 R/ u- b& z8 f+ u2 Y3 S: P4 n# v
以下是创建自定义插件的步骤:1 g( ?4 c4 `5 @$ q" o" D x
6 c% j7 e* b) S' q6 c
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:* }: ~6 N! v# i8 y6 L& W) f
) x$ [' T( E, u% a- s ```5 T' k1 T# n2 [- Y1 [
<?php- G! L; H3 w5 O# b" {3 d7 J
/*3 ]3 p2 n! n+ u5 X4 B( ~
Plugin Name: Site Wide Notices Plugin
) h3 |9 b3 e. a Description: Adds a new custom post type for site-wide notices.. j" Y. h; H/ G1 u! j' Q J
Version: 1.08 ^# Y- V+ ~! W' C& c0 d! n/ s
Author: Your Name, z m& e0 P0 T( N/ c
Author URI: http://example.com8 [- i+ I/ g5 l5 s
*/2 p) z. s% h4 `# m3 S
7 j8 N# Y3 @" h) X0 V: u+ ?2 u
// Add plugin code here...
' ~" m5 a7 Q- I& B: R ```
! v7 c, ]; V `* t- b: i4 ?+ a( L5 `* l6 A! Y0 S' H. L
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
( Z( J* m; f8 u3 [+ h
3 p( g' f0 F6 ^2 U" f6 C2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:0 T9 e1 `7 s3 J \ N) V6 R
4 t* A# K- U! u8 N1 @ ```
0 C+ x% d" G% a# Z add_action('init', 'create_custom_post_type');
4 E" a0 n* G [8 D! C4 v function create_custom_post_type() {
6 a7 L& U7 @; ?8 s' A# j $labels = array( O1 a; K F4 s$ P
'name' => 'Site Wide Notices',
/ w- U8 @( p. {/ W l 'singular_name' => 'Site Wide Notice',
2 H, o" p7 f5 W) H4 r 'add_new' => 'Add New',. o5 m6 t# h* @
'add_new_item' => 'Add New Site Wide Notice',
1 M1 z# j+ U* R+ S 'edit_item' => 'Edit Site Wide Notice',
% R6 y9 @9 V" ^4 X e7 I 'new_item' => 'New Site Wide Notice',
& z: ^, [* C( i- ~ 'view_item' => 'View Site Wide Notice',4 F4 }5 n" M$ g8 w: e g( j# k
'search_items' => 'Search Site Wide Notices'," i) u4 j5 R, T
'not_found' => 'No site-wide notices found',# E( F8 }7 n3 R+ e1 C
'not_found_in_trash' => 'No site-wide notices found in trash'
3 ?, A( O) V/ U3 X7 S );/ b4 B# y. O- }) R& s. H$ m
& J& l$ g. k! t $args = array(
5 p' S# x8 T0 U+ A) a" q 'labels' => $labels,: I* c% {- S! @
'public' => true,
( L1 J$ j. m/ q/ p5 s1 a 'has_archive' => true,' Y2 U% b6 A6 i" V3 {' e
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
9 t2 H4 A- W0 v: B0 W 'taxonomies' => array('category', 'post_tag'),& m9 I q% F! {& g
'menu_icon' => 'dashicons-megaphone',# s9 q) C! d- u$ _/ Q. e
'menu_position' => 5,
( Z3 A( j R8 @( S- v! f* f 'rewrite' => array('slug' => 'site-wide-notices')
2 q4 ]; [3 {( a; F: h5 q% n );! g) N4 U8 i4 L0 c6 k. y. x& {
* ?+ `5 z9 E2 |3 n: ~
register_post_type('site-wide-notices', $args);2 A$ ^- Q% g6 I& f3 Q
}
' |: h% q: Q$ Z1 { I ```
2 T0 ]. g9 l$ B: l% V! |6 f& P+ n9 w6 Z& |$ m0 ?
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
- y! l- ~) I2 f" g* W1 m! D) o" D
& N$ U# R1 x: T( u; ]& N! Y9 c3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:3 ]! I# M. v8 P/ m4 I: Q
! d1 |; O% d: c. I( A0 A1 c ```
" ^: k2 t1 P% K' @, r$ n' { add_action('add_meta_boxes', 'add_site_wide_notices_boxes');2 Q! {$ A$ s J* o- h6 S
function add_site_wide_notices_boxes() { E9 E' z( |% B4 J
add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');% F2 D+ K9 w5 P! L5 l+ g3 w
}4 ]; q; D2 U. m$ P
3 C. | {& ~4 e( k6 O
function notice_details_meta_box($post) {
2 ~$ o: P& y l$ t& i5 {9 p wp_nonce_field(basename(__FILE__), 'notices_nonce');
" A" J( N, N# Y8 W $notice_title = get_post_meta($post->ID, 'notice_title', true);
) b( A5 K* h( Z' Y, q- c/ R $notice_content = get_post_meta($post->ID, 'notice_content', true);! O( k9 {( \0 b- ~; N1 B
?>
5 \2 v" }/ ?+ X' N <p>
# C, d- M6 q3 V% N- Y' g <label for="notice-title">Notice Title</label><br>! Z) q& a7 U$ |6 t u
<input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
B7 Y4 v1 H- {2 i </p> h$ d" R% n5 K. @3 I
<p>
- X+ f. [$ I; W5 f1 ~# B3 P <label for="notice-content">Notice Content</label><br>
1 G" e ^, v! b8 W <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
" w) p7 R9 f' f3 [4 C0 I </p>
+ F. {# f, G u; b% [' D9 } <?php% C3 a1 {& K0 p6 @8 T) ]9 f
}* V! y5 o; W( S+ m O1 a* t3 k
5 l9 m4 f5 ]7 m' @- \. t add_action('save_post', 'save_site_wide_notice_meta_box');! j* z, H5 _, D4 |* j5 w! \$ o
function save_site_wide_notice_meta_box($post_id) {
% A3 [0 ?2 A/ U" z5 ?: b: J% z if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
/ Y6 h3 k# i+ f) ^" \. j, y' K) ? return;8 U* j8 A) b" c( _1 {7 v' p
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" y% _7 ^* l/ r8 i" O8 ~ return;
+ k/ ^4 J1 r1 [4 U) @$ A- H2 I9 m' w' _9 e1 }' ^
if (isset($_POST['notice_title'])) {) z# W; a9 t# O# i
update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));. d! V/ P* |+ d
}& a- _, U: V) j7 [1 Q
if (isset($_POST['notice_content'])) {
& X# v9 x# Q7 W$ |& W) x* Z update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
/ x* M8 y' C% I- N4 C: _ }
5 T. S+ @$ y( R) L6 i }# D, ^( m' z' w0 Q F; {6 A
```
' S W& x4 n0 H% j& \! _" b9 E7 ]$ t4 O
在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
' h2 M& X5 h! {, L0 z3 ]8 w, N' x+ v0 [! ], Q
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:+ A. W: N# n" J# h/ `7 a
+ u' Z; f1 U0 s& r! P ```
8 P' e j! O: f1 f! Z& D9 b $args = array(. x" o# A3 ?+ ^! f* f
'post_type' => 'site-wide-notices'," V5 p* l3 b$ i2 ?' }7 \
'posts_per_page' => 3,
L+ N* T5 Q; x 'order' => 'DESC',
2 m, I- R% |4 G$ { 'orderby' => 'date'
* y0 g1 J3 N3 ?# x& | );
# ]) M+ ?4 c! b7 T2 `% L' h: H! u $query = new WP_Query($args);0 I9 w) m* }4 O2 h% ^
if ($query->have_posts()) :! s a+ J6 L) l8 \7 l
while ($query->have_posts()) : $query->the_post(); ?>
( S- \1 n |% q% Q. `# { <div class="notice">
f g+ P1 ~2 a9 A! z ~6 b2 ] <h3><?php the_title(); ?></h3>- {( [1 b6 N# Q
<div class="notice-content"><?php the_content(); ?></div>- {; X% C9 V5 d1 A3 Y
</div>2 `$ M R1 \ k0 d1 M( H8 m
<?php endwhile;) v+ {9 C+ P) ~. o; C1 }
wp_reset_postdata();' E7 Y/ h4 j& O6 @
endif;
. W* D( S( K8 M ```; L s9 Q8 d; ~, D4 o+ j [
9 T% f [) A& f7 ]
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|