|
|
楼主 |
发表于 2023-5-18 09:27:55
来自手机
|
显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?" ?/ H# ?2 S' Y0 g0 v ^
) O5 F4 K$ l2 u3 t, Z! e/ i
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。- b0 _+ ?/ Y. F( o6 m- s
5 j! A: |7 o+ N) M; z3 c! @以下是创建自定义插件的步骤:
3 T) C; ~$ n; e7 k3 K7 R; L5 _; _+ A8 @0 f3 g7 ]5 W; Z) {2 B( p
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:6 M/ e8 h, W+ ~ Y- [; p6 B# Z
3 s5 @6 D: }+ Z; ^ \9 p ```% W; w1 ?" d5 M3 ?* t2 L" [
<?php" D5 l9 I; U5 J3 E
/*& U, y% f& _- C3 N) M; x
Plugin Name: Site Wide Notices Plugin
" t$ i r. e" N6 o/ w- @* E Description: Adds a new custom post type for site-wide notices.8 _! A8 h9 z7 x
Version: 1.0
6 l7 W5 e! @- z6 _6 U Author: Your Name
? m( g* O0 r% i6 e+ I$ ]. o. y Author URI: http://example.com5 Z! D) K' s/ v% F
*/& `. v ?' {( Z* w3 P. A
/ L. ^. @" Y* E7 U, j
// Add plugin code here...
# d$ ?4 c9 t! s* i ```; W( N7 L4 g- M+ z/ o+ Z
) ?. [2 x; x& T8 h3 u- r/ I
在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。4 K6 o% A2 h5 @0 O( l7 g
; D" g6 K) t. M! X. g) ~3 L2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
5 X: M P5 M* _! x$ x+ S
" r- U1 P- }! s6 z% l ```' i Y3 ^+ J& m) {
add_action('init', 'create_custom_post_type');) o( w# d7 Q5 p+ E) P. D
function create_custom_post_type() {
# X, U0 p h/ u0 J $labels = array(
. [' H% }' U+ e! r m! a7 r 'name' => 'Site Wide Notices',/ O( [* |2 }4 G# {! k* o4 Y
'singular_name' => 'Site Wide Notice',( E; _' v3 J. V! z' I
'add_new' => 'Add New',
2 C, z0 L" D0 c6 P7 P) u3 [ 'add_new_item' => 'Add New Site Wide Notice',. h/ j& l- J" @3 j0 l6 z' t& D
'edit_item' => 'Edit Site Wide Notice',
! @) x+ M l; k 'new_item' => 'New Site Wide Notice',
" c/ l- |1 s3 Q9 J6 A3 f 'view_item' => 'View Site Wide Notice'," [: d& G- R7 Y! ]$ ]$ T
'search_items' => 'Search Site Wide Notices',
4 l+ U3 e# u K* s4 x& P 'not_found' => 'No site-wide notices found',5 Y, N0 C$ e2 ~ i# E
'not_found_in_trash' => 'No site-wide notices found in trash', Y" {$ u6 O- I+ G. k% N
);
$ M; l9 d5 O2 I' @. s. l& j4 u0 t3 |% I! H8 R! P* L5 L& g
$args = array(! m! i" k6 K3 L! A* a
'labels' => $labels,
0 b' O4 U0 ^& O% t 'public' => true,
& v; a( q& D; y- L 'has_archive' => true,# x! W1 X) Z% R: r/ _. K
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
7 ]6 Z% d, `; A2 }9 F0 ] 'taxonomies' => array('category', 'post_tag'),# M- W- b- A$ w9 I+ s5 `# \+ O3 p
'menu_icon' => 'dashicons-megaphone',% L* x: C: D8 G* C
'menu_position' => 5,
3 B) s0 H8 M5 w; k3 W; V/ ^7 P1 g0 F 'rewrite' => array('slug' => 'site-wide-notices')* |8 b5 `/ x8 F2 l5 s
);
9 k! I6 H; L; N1 j+ f- K2 x! t4 q+ C
register_post_type('site-wide-notices', $args);, \: g9 S! A/ \2 v( q/ S) }
}7 {$ @! i2 x" X) L$ J7 x' W
```
- u4 S g, ?4 i* q F. r& E4 l8 D# q4 @9 t5 S6 ~
在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
, F) y1 O* P) h$ l) ?9 u
7 l6 v' N! `& A- S9 O6 s- y; @3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:% g! D9 g( Y, z% `
; Y( }: F* Y) [# F) i ```
0 r v% p$ g( i; I6 f( v0 | add_action('add_meta_boxes', 'add_site_wide_notices_boxes');3 ]# R; P9 y; Z
function add_site_wide_notices_boxes() {
) V+ m1 p7 F: O/ N3 k add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');9 j3 C) I8 K7 W
}$ Q+ ?7 T3 M) G4 ]8 P
7 B+ f$ X0 V7 Y" f6 R3 B! D" `* R function notice_details_meta_box($post) {) r0 K6 V! U5 r t) H1 {( n
wp_nonce_field(basename(__FILE__), 'notices_nonce');
7 S) X- y8 ]2 y" N% e( h2 ~. m $notice_title = get_post_meta($post->ID, 'notice_title', true);1 D* f/ W% k$ S
$notice_content = get_post_meta($post->ID, 'notice_content', true);' q( q2 q0 x- E% e. K! h4 g) o* E
?>
1 w9 E( r9 s0 k+ J( t <p>
3 g% d& b, q8 S( k! x <label for="notice-title">Notice Title</label><br>
4 i6 I( A7 z" s) P4 N' g <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
6 _1 m, f' @2 ~1 o' Q6 } </p>* z$ U& j- R+ L3 ?" u% y
<p>
+ ]7 f! U1 E" s3 R1 _0 \3 \" { <label for="notice-content">Notice Content</label><br>
$ i2 s! j, J/ o# [7 G) a% e9 P; d <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>3 o; p5 P0 o( q( q5 x6 B& H
</p>
+ @; \7 C) y7 Q% f9 |" C <?php9 o( ^4 ]4 ]( M
}
9 H6 Y+ p& ]% s4 F
; G) u5 q- k% s6 h2 Y, a add_action('save_post', 'save_site_wide_notice_meta_box');
7 s, T i9 Q' C: Y function save_site_wide_notice_meta_box($post_id) {* r0 @/ V* _2 g X. R/ k8 l6 d, Y5 C
if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__))): ]. {3 C" ]9 r& L1 B! m5 _
return;
; E7 A( h2 j' n2 Z( l, I if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
/ }& v7 }8 T. A9 ^ return;7 E3 M7 U6 l+ T4 Q8 F3 g; I- `% P
, p. b& M& p8 v: |
if (isset($_POST['notice_title'])) {
; B$ E2 B! b: p- i update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));; l' e. Y& \4 B- `0 n
}- ~( `8 p1 K7 B# Q) J& ^
if (isset($_POST['notice_content'])) {
" T& Q0 R' ]6 ]! k" q update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));9 }5 r1 \) w1 q$ `4 H4 F4 t
}
. T9 I- d0 Z- h; B$ p: q- Z3 G }
1 H: G* B9 e- {- F ```
- j/ c4 v: Y' m4 J" a4 {+ t ^
~2 w. V% k( s: L; J d* ^ 在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
0 [* a$ a- ^: }+ m& ]
; E4 T5 x8 _; [ u, D# M4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
# F( ~$ Q+ \( e J+ v
$ W6 E% e9 \7 n b5 M# d ```
% }) X* H; v" U! R8 J5 | $args = array(( u4 y @* p, O" E8 p- _& _
'post_type' => 'site-wide-notices',; o1 B0 w% k$ D
'posts_per_page' => 3,9 H# S ?; ?8 U9 @. x) X8 k0 ?8 C- ?6 s
'order' => 'DESC',+ C y" S' N) E" w$ k
'orderby' => 'date'
7 D# ?7 F' V$ O! u; \" Y );; c. H- U* }* T; T3 k% e. F4 ~% {) W
$query = new WP_Query($args);
" S# O d! G1 j" A- e( v0 K6 ] if ($query->have_posts()) :
q1 F, R+ |+ m! m, t while ($query->have_posts()) : $query->the_post(); ?>
" `9 {2 A5 f5 ^& T6 e0 z+ X# L <div class="notice">
1 { x7 I5 I* h$ q7 h- p+ n7 r <h3><?php the_title(); ?></h3>
) |4 V" I8 Q( y+ N" a# D7 g <div class="notice-content"><?php the_content(); ?></div>5 V& n/ a, D: F# M
</div>. [/ i' D$ Z8 y1 m1 r" d
<?php endwhile;
% ?* `, s v9 f: K wp_reset_postdata();
. i5 G# S( m5 Y+ |/ B/ ~ endif;/ p9 m& e! p: l; o! l
```8 l+ Y/ n8 W/ \6 y( C0 ]
# `! r3 O" _ C5 Z3 Z5 x
在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。 |
|