找回密码
 立即注册
查看: 4225|回复: 3

如何快速在wordpress后台中增加一个发布全站公告的功能?

[复制链接]

347

主题

563

回帖

3671

积分

管理员

积分
3671
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:* _& q* r8 n, P' h

4 Y$ n! |; v' `- A8 k5 v& L1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。
! [9 y6 h( l) M3 `& p+ y& E8 `9 F3 d' A- R
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
$ e/ i8 x! `6 {7 |8 r2 l9 @! l8 Z  c+ v9 U* U
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
; m$ Z: f: D0 G& L) u6 H
  G9 g0 w* A( {* i5 j) o8 [4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。5 o0 a9 p% J" M
1 F' M2 Y2 Y. q4 K' B- W; g
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

347

主题

563

回帖

3671

积分

管理员

积分
3671
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?& R- s3 f* J! V& G8 d0 r4 x
# i# ]2 e; N! S$ {8 ^
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:) j) _9 U# l2 a
& W  |1 n1 z+ S* G3 K
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:! D2 u/ Q% k% X" z1 I( H" W& x

6 w' }! ~* |- K6 N& |   ```: @$ `( H6 Q. x% m
   add_menu_page(
( l6 c6 Q4 `) Z8 R' f8 `       '全站公告',
8 X$ ^9 k3 c. O4 e6 @       '全站公告',% r4 |" V4 S+ t; Q% N5 a' S$ G
       'manage_options', //需要的用户权限
1 D6 h' I3 A& \% z       'site-wide-notice', //后台页面的 slug& w- `% x. v( v
       'render_site_wide_notice_page', //用于渲染后台页面内容的函数6 O7 v# @6 |4 Z) a# ^
       'dashicons-megaphone', //用于显示在菜单中的图标
; j) q! @" U8 J( ?' D       30 //菜单项的位置' q! q- W( ?! u  P. o
   );8 B, y' x9 _2 @: W: j' c- t7 u
   ```9 n6 I, o, X" u# J# \/ r; _- C
7 s- B! E% P* @9 p+ B. O0 h+ P
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。# Z  c" f+ Y- b

& G3 {0 F# T* }# l9 Y: Y- t2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:2 r' d  ~8 R: v, O& G
$ Y6 _; V9 D: o& d# j' J  K) \
   ```) c; G- e8 f" \& K4 ]( P& u) x
   function render_site_wide_notice_page() {
8 y6 b( v/ |; y4 B4 W      // 渲染后台页面内容
+ m7 E; l2 Y  f$ D2 E2 h      echo '<div class="wrap">0 r) {7 y  Z7 Y( i5 W  `2 C9 c
          <h2>全站公告</h2>
4 o/ Q" D1 G4 v- |" D% A          <form method="post" action="">  ]) I, M- r( [
              <label for="title">公告标题:</label>- I/ V8 P. }& O6 C' K
              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>; j* Q( P5 w- B: S
              <label for="content">公告内容:</label>4 Y' @* }, Q# T. t: m, y
              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>9 `2 H$ g8 y) F# A- `
              <input type="submit" name="submit" value="保存设置">
* D" F# N( Y6 b! ?, _& O          </form>
, ]. c( i. y2 V9 f* U/ Y$ `& \      </div>';
" Z0 M2 Y! b# ?6 x. s1 M1 q3 s  i' L+ a5 U5 }/ a, Q& j9 Z: C
      // 处理表单数据
( y9 c8 l) K+ t/ O. k      if (isset($_POST['submit'])) {+ K; q: |9 F. T- P& r0 U4 r2 _4 m
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));3 c1 y2 @& M- i& y
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));
8 R/ H/ |/ L, X: M: k  o) l      }
" ~* _2 b+ O  y: S   }$ |1 W. v9 r: \% N  r
   ```
3 Z" t" `* e( `1 R7 e( W* V+ h- z8 S& ^
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
. q6 I) X# R5 b* J8 u- C
( u: J  d% g% t+ E' ~7 z3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:6 n; T* _4 o# j  s
" y" I8 M9 b% a* T
   ```1 I* B' u8 W- r2 s
   <?php/ X9 n# z$ e9 O' \" D! P5 @; {
   $site_wide_notice_title = get_option('site_wide_notice_title');
3 I  Q9 l1 ]! O" s, @. n( F   $site_wide_notice_content = get_option('site_wide_notice_content');( V! a* ?9 r# m- t, J! D( a0 C; G" _3 D
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {2 i; u. q# v$ s% X6 x) V( t
       echo '<div class="site-wide-notice">';4 c, I+ \% Q$ ]1 k$ m$ E& l. r
       if (!empty($site_wide_notice_title)) {5 ^6 f. \# R8 X# i1 ]1 m: B% |
           echo '<h3>'.$site_wide_notice_title.'</h3>';
. a- O5 |% Z$ e: T9 i# X       }
- q/ N* X1 ?) F6 |3 v9 O- N       if (!empty($site_wide_notice_content)) {
$ J* O' l7 w. l, d+ q           echo '<p>'.$site_wide_notice_content.'</p>';
5 `  k! ]& V4 T8 |1 e& f  }5 T       }/ L* Y; v* }" ^8 l# ?% z8 W
       echo '</div>';% M5 E9 v- G8 o/ J& }# k8 G
   }/ {2 F  b# h: o5 L6 U: n# p& @+ J
   ?>( _8 P- ]! S' G. P! E/ ]
   ```6 n' q! c7 W2 E$ f: i: F" P5 E4 [8 \

' x, B, X8 n* u! j, g   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。" J( b8 ]* {+ {/ a6 G2 t+ f
& G: u# i. ~6 l' R' f
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

347

主题

563

回帖

3671

积分

管理员

积分
3671
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?! U" Z0 P4 c) d; J' e3 W

6 e2 k' S1 ^9 ?2 g您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:& A2 D; e: v/ U9 F: t( t

2 a  r) p; D  t$ L, _4 P! u( P1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:: x  R' w: I% s, i6 y* D& a

- {7 W7 v. s+ U, G* C) }4 Y```
; |; `9 C- P  m. Ufunction create_custom_post_type() {
% @0 n# r7 C) z1 w$ S& g# e    $args = array(2 T- O; U: Z$ @( v8 U  p
        'labels' => array(8 X- s& v: _, T; H
            'name' => '公告',% u; L, q" Z: R& l5 l# c# I) S
            'singular_name' => '公告'
1 G2 x2 Y/ q$ x2 @- B/ [        ),
8 M" x/ K$ N) r7 s: e        'public' => true,) i: }3 l1 \. Z5 O4 G' r7 l/ l
        'has_archive' => true,! n' k( ], {. s) k# D, v% S
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),- h' U  B# U- J% F# M' d
        'taxonomies' => array('category', 'post_tag'),
5 R. J0 p( ~% h0 X1 F        'menu_icon' => 'dashicons-megaphone',
+ s; Q" d4 b7 w4 l! Z5 y- W        'menu_position' => 5,; E+ c* L; c" d) Z5 b
        'rewrite' => array('slug' => 'site-wide-notices')+ n6 A  }8 k8 x# J
    );: W/ m+ t# Y9 p( M0 B$ [) F
    register_post_type('site-wide-notices', $args);2 f: n# C: ~# H1 [/ Y
}
9 X, L: j( B( ?0 k/ O- V9 oadd_action('init', 'create_custom_post_type');
: G" v  I" U, h% m# O' }```
8 }# l; [9 Q0 E* Y, r
+ I+ e+ d) e0 I* t* p' J% U* p在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。3 ^4 t! D; d; Z: a1 e

. S: m! c% M: I# l8 e% ~3 S2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。) M* ~  O" C( s" h! p! N* \' R
+ L8 f9 k7 [1 D, k, s
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:: \! g' H! Z$ Y6 i
6 t9 l6 L0 S2 i$ }: A# A; c
```
0 t: C8 y" s2 e1 y$args = array(% z+ t% z2 M& B; Z6 L1 y. Q
    'posts_per_page' => 3, // 显示的公告数量# f: V, R7 v" |1 |! r  q
    'post_type' => 'site-wide-notices', // 公告文章类型
) }, o1 P, [# |8 w    'post_status' => 'publish', // 公告状态
1 P+ @2 I+ W8 H. L& ~2 V7 Z- [    'order' => 'DESC' // 排序方式! J  K+ }8 x  x0 U/ c) d6 P1 N
);
- _! \) [9 c8 K4 @: c: b& o$ y1 v3 B$ J3 H  l
$notices = new WP_Query($args);
1 k$ t" c5 R4 k9 B* |; e5 Hif ($notices->have_posts()) :
- I  s- O5 |6 S! j    while ($notices->have_posts()) : $notices->the_post(); ?>* s, O; o6 l* s, P5 Y
        <div class="notice">
0 d( L2 Y" v, [$ T1 x! I            <h3><?php the_title(); ?></h3>
8 G2 K& d7 V7 ?* E  h            <div class="notice-content"><?php the_content(); ?></div>. G0 r/ x3 W3 P  [7 T( R
        </div>
1 T1 _5 w9 I, j) G0 v0 l+ W    <?php endwhile;
9 ^: [3 ^  A2 P' zelse :
" r+ W! u; ~0 H4 A4 l+ L" H1 Y    echo "暂无公告";6 F  x/ f) I. N
endif;2 `& L( B1 H" W% @$ E) G7 n
wp_reset_query();
0 w2 K5 G7 [- g( W0 d```
$ S# f) U7 @0 g. i( P8 b) p2 o% u
, A8 n7 f1 C6 \" y6 b这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。4 W/ z# c& U- s. t+ u  |5 z
* }4 I3 M+ L7 }0 N* J- C
以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

347

主题

563

回帖

3671

积分

管理员

积分
3671
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
' D- C& X- L5 z* g* c
# f: b" F8 ~( k9 a2 s如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
* z) i: Z3 j( ~& h6 i/ e2 V% g" `) @. W
以下是创建自定义插件的步骤:
+ X5 Z1 u: W/ ?) |) ]/ x( P6 f* Z; z" ^
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
" @! T% V9 B8 ^/ S: W" B# ~0 y$ E+ O2 ]. V, v( d
   ```4 V/ {6 L( l5 a; p
   <?php
2 s) I! L( U$ G9 H' h' Z   /*- M6 [/ H2 q1 p
   Plugin Name: Site Wide Notices Plugin: F  h  [( Y. E7 V1 e
   Description: Adds a new custom post type for site-wide notices.9 X: w( k6 ~2 P7 c* g1 ?
   Version: 1.0
% u, ~4 h1 m$ B0 t# a" p7 V$ G   Author: Your Name
+ ~2 G$ U2 {; l( K; T1 b" j4 V6 d   Author URI: http://example.com
6 ^  P3 \) F/ g2 A   */
' B% E5 M: O8 O# ~) F6 ~2 V
/ x1 r( u- e9 Q  \& S( @   // Add plugin code here...
# W9 \2 M' u% U* K# G2 w4 [; g   ```
  c. N& d# y2 M0 D$ l9 k) [7 b
, C$ w8 |( v1 m, B. F$ {   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
/ Z- o5 O2 M& ?
: b& c# b7 U; N: `! b2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:! a& R9 x; @1 G9 h3 C# X( J4 I

3 B: K; k; m5 |# e$ \/ P) T   ```
( S/ m4 a  o/ B. f" G/ L   add_action('init', 'create_custom_post_type');9 x4 `$ |. q( B
   function create_custom_post_type() {
* p+ O" V# b" ?1 v% h! j1 R1 }+ D      $labels = array(
* B' t9 q; @# N8 Q          'name' => 'Site Wide Notices',+ D) k8 C3 |+ {0 P* h4 L; N- X7 n
          'singular_name' => 'Site Wide Notice',
/ T- l: R0 H: b  A1 j  r          'add_new' => 'Add New',  b, G6 I' F1 \
          'add_new_item' => 'Add New Site Wide Notice',
' G, f( _* [# M8 b9 l% I) F% r8 T. O( R          'edit_item' => 'Edit Site Wide Notice',* K. u, H6 Z& m1 \) \
          'new_item' => 'New Site Wide Notice'," o, P$ u$ \' b1 m7 i! Y- Z- O
          'view_item' => 'View Site Wide Notice',% }- v) a" a0 V
          'search_items' => 'Search Site Wide Notices',
$ G+ ^2 [" f( H+ F6 F; H  E          'not_found' => 'No site-wide notices found',
/ K, }2 w5 t, a' l( R" L7 e          'not_found_in_trash' => 'No site-wide notices found in trash'
8 m, C9 \+ N% `$ c5 z2 u  m      );! h5 B0 ]1 E1 I7 k

0 b) x$ E4 I8 u      $args = array(3 ^% Q& ]* j" O  Y. p% `% U
          'labels' => $labels,/ _# s$ [9 t: X! X9 g" A0 G
          'public' => true,
& t! l" S% N) P; h# u' F          'has_archive' => true,6 K( x. p4 f& H0 e$ z' m
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),; n/ v. }* `- s/ j  P
          'taxonomies' => array('category', 'post_tag'),+ A8 v9 `: P; h+ k- \
          'menu_icon' => 'dashicons-megaphone',
" r: L" ]9 q6 V4 W          'menu_position' => 5,. G* ~+ F. j$ D  C0 U* Z
          'rewrite' => array('slug' => 'site-wide-notices')
- [* |7 e  p( A" w" Q      );
' x* H( o: P/ V4 j+ K
3 |0 @# c% `( [- |  m      register_post_type('site-wide-notices', $args);. p9 @8 E1 p9 V3 u# L
   }
3 d* F9 S, t! w9 x2 {2 ?3 ~& \   ```8 F, ?' x% O- U2 x; D- u" R

3 |! R2 {3 M' W6 J" S   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
: e  r# i1 S2 O' l5 L* O9 h& _% D- N) d6 m9 O
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:$ F+ ]% s& Q5 }* ?' x  L- O) C

) F; S" p! D7 ^6 p) j0 [6 n   ```
/ H% B# _3 n( A- F  n6 p; t   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( Y* M  f) h2 k8 i% h
   function add_site_wide_notices_boxes() {
2 a. v$ ?$ d9 o0 `3 J5 K- S      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');3 y0 W; B2 X$ ?' o4 c, D
   }+ ~6 U  [* m* b/ t
; x- }* [9 M; x& f! g( f
   function notice_details_meta_box($post) {
) A" @2 M: _7 t! C      wp_nonce_field(basename(__FILE__), 'notices_nonce');
# q, @  W& b, _9 E$ p/ k% |. o1 F      $notice_title = get_post_meta($post->ID, 'notice_title', true);* u* E* ^+ b$ }6 z
      $notice_content = get_post_meta($post->ID, 'notice_content', true);
* ]% L. O; z# ]  w' u/ J      ?>
' R) y$ l, j* g8 V( k6 g      <p>* `3 ?! w! M9 L1 e$ \
          <label for="notice-title">Notice Title</label><br>
1 D% M0 e( H# K% T          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">6 {# J& J3 X5 N. P. h: h% K- m$ a
      </p>
" l/ D' I  j; f% v1 a- Q      <p># Q1 r) C; r# s* F- M1 @
          <label for="notice-content">Notice Content</label><br>
+ N' {: a+ r2 [          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>0 e/ J+ _  F* [
      </p>' R6 i) F3 V6 U4 _
      <?php; U/ X! b4 K( E9 f& G6 g& ]
   }- b1 \$ l+ k9 M3 K; k
# a6 N* |# F- G/ }) u$ x
   add_action('save_post', 'save_site_wide_notice_meta_box');
. O) p# n, v3 z' r, }   function save_site_wide_notice_meta_box($post_id) {
2 S% c* Y* `" i& g+ h$ ]      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))2 @, `/ v& X9 C6 v# [8 q9 x; p
         return;
9 j- S4 Q- n* X+ o- Q      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" O5 ^5 ?1 h# i, F( C         return;
% u( U4 x9 G6 g: I  o  ~- y$ }. O' {6 m" K0 A0 T! y+ q
      if (isset($_POST['notice_title'])) {
! j4 |' ?# C) v% Q. b- Z          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));' v( c) c8 h3 ~* \
      }
( F  ]  X' b2 k. u" {; P7 f      if (isset($_POST['notice_content'])) {
- {# q& I8 N, G4 U6 M          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
1 O0 {7 V$ C$ h. R5 ~      }4 C- {) N0 K& l3 M- S3 D
   }4 U: E3 a( {6 K3 }2 f
   ```
; b# T% [! x- h3 ^$ K, D$ _0 M, _5 e; Z& ~/ c2 W
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。5 U. W, V0 q2 z1 [! ^+ C
% B/ {# Q' O* O! Z6 D
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
5 H) s3 C6 _8 o- i( _& K0 X# i5 F  c; G% ?4 Y+ V7 R2 D
   ```* v8 L+ Y& d! i( x' i* s% |
   $args = array(  x% c7 r9 L8 D+ D
      'post_type' => 'site-wide-notices',7 M. e2 D# f; a* G3 Y1 K1 e
      'posts_per_page' => 3,
- J/ x& u# f/ L0 G      'order' => 'DESC',. m4 @7 c9 M& `3 M9 b
      'orderby' => 'date'
% o' a% {! K7 A2 x0 w/ t   );! F. X) d0 F3 A. h4 a6 e' F1 N
   $query = new WP_Query($args);0 i+ X9 D' X7 d: {) b, s
   if ($query->have_posts()) :
5 r/ c( Z" Y8 h! W" Z# _- v1 l      while ($query->have_posts()) : $query->the_post(); ?>8 `; M: w5 Z2 G) ~
          <div class="notice">
5 g! ]5 K" G4 m* L7 J' @" N8 T8 r              <h3><?php the_title(); ?></h3>
% G- M0 A/ w* F, m$ }3 `# M. s              <div class="notice-content"><?php the_content(); ?></div>
1 v) Z* N* d- l5 U" n          </div>& _$ Y* i0 K$ B- T
      <?php endwhile;
2 T* _" b% {8 h7 D      wp_reset_postdata();: J& H: v  K, v2 e4 q
   endif;) m$ S: U9 t; Q( E' B  H' ?
   ```0 z, N7 n) n6 v
3 g4 [) s6 i3 F+ z; ?/ \; o
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|通达创业无忧 ( 粤ICP备2023021749号|粤公网安备 44030402006137号 )

GMT+8, 2026-2-6 22:50 , Processed in 0.011453 second(s), 2 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表