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

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

[复制链接]

332

主题

513

回帖

3372

积分

管理员

积分
3372
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:
& i4 ~) v( J9 f6 B9 E# v" B3 r' v0 K
1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。
. s$ f$ J) T% G) \5 A: X# @% R* ~# }2 I5 ^) I8 }" m% \
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。- K# }& x0 b  m$ l% u- O9 Z+ p& O

: o3 Q6 k9 N1 S6 {3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
- \, ]! @3 u; q6 D: f
8 D7 Q6 k. p, W0 Z9 l4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。3 b" T, e" E& g% a! ], y0 s8 Z' N
0 G! D5 p  m6 y, b) d8 L
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

332

主题

513

回帖

3372

积分

管理员

积分
3372
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
# u7 ^1 Y5 p, Q: S6 z% X2 o8 ]. K/ q- f) r7 v/ b- f7 p5 I4 A
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
2 x2 R3 p, e; F# A; m( j
- X9 j& d6 d: u; ?( `( o4 M8 H1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:
/ Z* D# q% y7 B, B% S' b1 e, @/ {& L# z- Y" N& ]" e1 s
   ```
( ^% J8 d, o1 t( z8 J/ l9 X4 m   add_menu_page(
6 ]; @8 H8 Y0 H' W5 l5 x9 S       '全站公告',) `2 j: a& v: S$ I  m1 Y
       '全站公告',4 `! I: Z5 a, h' e+ c% N
       'manage_options', //需要的用户权限
  A7 K0 @. F( u7 C4 K  q       'site-wide-notice', //后台页面的 slug
' H" Z7 H  ]5 N       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
3 p. h$ q% R6 g% z  b' ]) I       'dashicons-megaphone', //用于显示在菜单中的图标. z4 u9 R1 Q* A( E# q2 C. [+ ^9 x
       30 //菜单项的位置
9 R) C6 X+ l( a3 Q! o   );
& p) H% q9 l# v% ~8 S) }' J9 Z" v$ F: l   ```
$ B# S3 {; r5 W, H
; I$ e' n8 S. J   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。$ n' x: w8 v# ^5 \3 ~- \& A

7 \$ T! Y* c, d% o& P! \3 I2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
- D3 C/ T9 O$ N( ^! T% j& N% a
! j9 x( S( C) S$ h) Z+ u1 k   ```3 ], m* @' N  Q- d5 s: k, s
   function render_site_wide_notice_page() {
0 @2 P5 t6 w  z0 b' T      // 渲染后台页面内容
7 C; F6 g7 y/ K/ E/ T" t      echo '<div class="wrap">
( ^! w7 l. h& t/ R7 Q# Y; G          <h2>全站公告</h2>6 i2 @& L+ L/ h/ O" F
          <form method="post" action="">8 c, U) x& n3 J1 }6 y) @
              <label for="title">公告标题:</label>& ?2 H: u& T7 F, ]  Y
              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
6 S$ U. I" C! e- u& S+ W% z0 {- {              <label for="content">公告内容:</label>
* I; x2 H; D# A: a- r5 v* n              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
9 x  ^. |4 O# p7 I( {. k) X              <input type="submit" name="submit" value="保存设置">2 m8 i# T8 l8 a( i9 z7 O
          </form>
; I1 k/ `1 `( ~$ p1 K2 B8 c  X      </div>';
* G5 N& c4 G* ~6 y/ W5 \6 g. t' v: K% y3 t+ D+ x
      // 处理表单数据4 i" O. [* j" V9 W) i0 R9 r
      if (isset($_POST['submit'])) {' W  _8 a! c7 G: M& _- _
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
+ v) H0 v' \7 K0 K9 u          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));
4 }1 x; N7 P4 _8 ~, S7 K1 D      }
8 K; d( t4 h  h, G   }# c7 Y( y$ p: H
   ```* \. @$ p/ V+ D5 G8 H
* u# Z2 a7 |0 I) u. l6 _
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
4 B2 [: S# b: G' H: Q2 z. e/ ?! ?1 b
3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:! L# Y5 }6 k+ L; x

) M) t2 X6 p* V   ```" I' f7 u2 Y8 E1 s8 p: n
   <?php; n# @5 O% c1 \5 k, z# ~& Q5 j
   $site_wide_notice_title = get_option('site_wide_notice_title');- v" x! [' ]+ J5 A( A6 s
   $site_wide_notice_content = get_option('site_wide_notice_content');2 x5 z9 Q( \, Y- j9 n, l9 f2 S
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {" w. ]8 h2 y! J; M; ?( w! x
       echo '<div class="site-wide-notice">';+ V* X' `0 N0 l( Q% z8 R
       if (!empty($site_wide_notice_title)) {
5 Y9 N# ^# l' i; _3 L           echo '<h3>'.$site_wide_notice_title.'</h3>';
; ~9 I& Q; k5 J7 v& x+ G       }: h$ w( l0 o3 z. O2 L8 Y
       if (!empty($site_wide_notice_content)) {2 G; o1 z: P7 C7 W0 H2 z
           echo '<p>'.$site_wide_notice_content.'</p>';
6 U! d: s# |# l& h$ v1 X$ I       }
& P1 \# z# G( e. @8 b, p: d9 o. `       echo '</div>';- M' M7 D9 {+ {" B( d
   }% t9 y8 q% O/ Z- ]/ o! [4 T
   ?>0 @- W/ {3 t! P
   ```' Z" o3 N$ f/ W/ l+ N5 D
1 `1 g( Z6 U, ?9 D
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。- @0 ^1 S1 R- j3 l- `. @
. D0 ^' d* o2 t: ?
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

332

主题

513

回帖

3372

积分

管理员

积分
3372
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?
; K1 Z, K& f9 `9 x. w5 c
- E, d& a$ w% U- T您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:, N( s4 C' t4 a) b* W
; d; N: D1 i, C" B2 }4 r1 D
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:* j$ E, v4 E" i5 j9 y: q2 h

0 H+ n5 Y0 g/ O7 v# D3 n```
) ]& @* e5 q' \% Yfunction create_custom_post_type() {
( d) o4 X2 i! k9 E; Q; `# ~    $args = array(
  G  L7 X' ^: H* l8 e        'labels' => array(
7 B2 F# n. y; R            'name' => '公告',9 l# ^! l  z' C6 W0 S% p
            'singular_name' => '公告'
% x+ q' ~3 u& O! p6 w        ),/ ^1 H- E9 C3 p# I% x
        'public' => true,
" Y& `' o& J+ K1 U2 g+ W" d        'has_archive' => true,! W1 y; p9 r9 u& I
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),, ?5 N$ q* n3 t) u, N9 L
        'taxonomies' => array('category', 'post_tag'),
2 a* Y) P2 V  {! o6 U( t3 H4 h        'menu_icon' => 'dashicons-megaphone',: W' w8 d1 G+ k( f4 W! @* A& w
        'menu_position' => 5,! l$ F, m4 l# k: x7 d
        'rewrite' => array('slug' => 'site-wide-notices')
! O% _! w( `( _( e% d+ o    );
3 B3 V4 P8 D1 b    register_post_type('site-wide-notices', $args);
* `+ ^. q& B' P1 o/ y}4 r" U3 O: }6 y, v
add_action('init', 'create_custom_post_type');: N+ g: z& B! \. d5 c
```
# Q$ ~- T4 A: h: t# w* g: r7 F5 v8 P
( J6 F4 j- P/ y+ }& N在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
/ p1 i1 ~' n' N6 W
+ x6 ~% {0 W# r( Z9 D2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
8 u& K: ?5 h% X, D4 E, P- @& B4 `0 s% J$ [1 @  [6 o) g
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
0 P/ L9 \0 |$ i6 ~& n( y0 Y7 H  D2 k8 h* C& Q3 O+ ?
```7 O3 {) h  g7 g: B, Z. I; h
$args = array(
# Y. [  n0 J: K9 I( X/ T    'posts_per_page' => 3, // 显示的公告数量
9 `4 K3 V. C% Z: a5 b8 f    'post_type' => 'site-wide-notices', // 公告文章类型
( G/ N2 m/ C! ]9 ]3 V& G% i    'post_status' => 'publish', // 公告状态# h: ?: m: g) n1 t5 {  l
    'order' => 'DESC' // 排序方式
: p# r& ~, }5 `% P6 ]9 O1 o* g0 [);
( P3 b. p5 k( Y+ j# p1 ~/ Y! i: \
$notices = new WP_Query($args);
- E# M$ C0 R$ Q+ F, l: j4 Jif ($notices->have_posts()) :
9 ?! n6 _, v' u    while ($notices->have_posts()) : $notices->the_post(); ?>! Q. a$ W7 b9 A) H( a, L+ l6 E
        <div class="notice">3 t% Y3 h3 I( E! m; ~
            <h3><?php the_title(); ?></h3>8 T) ]+ D, z) j" d  F
            <div class="notice-content"><?php the_content(); ?></div>& j, ^  l. P) I6 I: T
        </div>
# L- _3 @7 N, q    <?php endwhile;7 p" q, W) E& _. n) {6 D
else :
# `, ]" u6 d5 N; d    echo "暂无公告";( V# n5 l1 A/ s5 F% l) r$ V/ V: w  V
endif;
  k1 c( O" ]5 o7 K# S$ bwp_reset_query();5 _2 c: i8 e. p3 a/ H
```6 s4 M$ m( Z" e2 ~( b. {
/ V& I9 e8 N3 ]- \
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
. t6 T0 @7 x6 [; }3 `3 p
( O8 y& b. Q9 o" y6 [! r2 _以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

332

主题

513

回帖

3372

积分

管理员

积分
3372
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
( G4 M$ X& L% G) @* H! J" o: ?% b
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。. Q, V3 d) n6 |# S

* Z2 `/ s% X+ l# x, u以下是创建自定义插件的步骤:. T6 f4 g8 C) G& G
% ^3 F. W8 T& v2 \* W
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:0 r, o; E- x( V/ U

4 N0 n' }) {/ Y% a  @   ```
0 b* K$ H% K, i6 F: q! w   <?php( n; u4 F- m; h0 X! _1 u  P
   /*+ Z! k6 s% y# G/ K+ z
   Plugin Name: Site Wide Notices Plugin
3 R8 y: N% O, b+ i0 z% c3 G- U2 \! u6 R6 q   Description: Adds a new custom post type for site-wide notices.& ~6 p" C. m2 G: m5 I$ q" f
   Version: 1.0$ W, S. g0 r. j& A' q
   Author: Your Name
/ u/ n3 Y$ D) [- _/ m# E8 c# j/ O   Author URI: http://example.com' l. _  [" j$ Q% Y" }: y& a
   */$ s6 e0 \" l' E4 o
; \/ O3 Z$ p8 D# n2 M, j1 m
   // Add plugin code here...2 a/ h6 c! H6 H  ]! p
   ```
3 a9 u. h* z  g9 D' y/ z$ L/ L3 ^. J6 h: z9 z
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。* B: z- e7 T& U0 F" b: B$ ~
/ D8 c& t  }  T+ _! C+ n9 G
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:8 O# t) H1 ]) J; d9 T& w1 Q. J

5 P# m- T  D, G7 n$ v- s2 d) {   ```
/ T% }: k  ]  T* D3 t6 v   add_action('init', 'create_custom_post_type');  o$ @2 C& u8 w& B9 J% j0 ^, F
   function create_custom_post_type() {  d, r" O- W5 E  y) j! Z
      $labels = array(
( @" A6 \; A$ x. [          'name' => 'Site Wide Notices',
3 M6 Q/ R$ Y" C( d5 @! g% K          'singular_name' => 'Site Wide Notice',
4 I4 Y2 z. n+ L( g9 W          'add_new' => 'Add New',4 J  c8 F$ {$ Q& q# r
          'add_new_item' => 'Add New Site Wide Notice',& `" {8 ?- {% ~5 ]0 u  m
          'edit_item' => 'Edit Site Wide Notice',! L9 i. p- n1 z# y% Y+ z' q* B
          'new_item' => 'New Site Wide Notice',
% C) l: N" F4 M9 R9 Q2 a; B  w: _4 X          'view_item' => 'View Site Wide Notice',
7 G, z& t6 k$ r0 ]7 X          'search_items' => 'Search Site Wide Notices',
' ?4 y; E% R( l+ _( O# i          'not_found' => 'No site-wide notices found',
  F2 R2 ?& t% ]: s          'not_found_in_trash' => 'No site-wide notices found in trash'/ c' @$ e# R( }( u& c
      );' C3 s* D0 |5 j7 ~  L* W
$ H2 i: m7 Q6 V; b
      $args = array(( @/ N6 t/ n" s' s6 U
          'labels' => $labels,
7 ?8 T4 v5 r  ^/ G* l          'public' => true,1 ?% E' E! v# M7 l4 ~% d
          'has_archive' => true,. G# e8 S' |9 q/ {2 g9 b
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
# `2 K# T: M& p) l          'taxonomies' => array('category', 'post_tag'),3 _$ g; ]6 f! W9 k, A
          'menu_icon' => 'dashicons-megaphone',
/ \& N' X2 P7 Z5 t6 y8 ^, L; b          'menu_position' => 5,' x7 \" }5 Y; C
          'rewrite' => array('slug' => 'site-wide-notices'); m0 }9 M( u3 a9 Q& B5 q) \( H; @
      );
( q' s5 J# R. c" g1 P' }; L
* m- K6 a- F4 Q: V      register_post_type('site-wide-notices', $args);
" b5 U3 u' j, C1 q7 e" d# {   }
( m9 }' j* n. t2 ]( r. j   ```
  }( F/ w8 i: F1 {- A+ _2 x
2 Z/ H! M- ?6 c/ o4 o1 A   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
1 M+ P3 n" B& j- n  \8 J- f
7 d# j8 B! c; A4 S9 O5 N% ~+ {3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:' ^5 i' E! m4 ]) e2 V% \$ o1 T( O
6 ^5 w: |5 ?6 @; o2 Y& H4 z
   ```
& U" [# W! c8 t* }; ~1 c   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: p/ h- t8 p  F   function add_site_wide_notices_boxes() {
8 `. x3 a: Y! \      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');, A1 x4 _; U6 D) x6 |" X
   }- ?) O; [( o5 ?6 W
6 g7 y8 O" x5 i- r" [0 L- r1 _
   function notice_details_meta_box($post) {
, c! ~% e2 J" R: b0 S1 H+ _$ @# f; C      wp_nonce_field(basename(__FILE__), 'notices_nonce');1 y2 r+ A- F9 z1 q
      $notice_title = get_post_meta($post->ID, 'notice_title', true);
& I5 I# R4 ^+ O# U1 b      $notice_content = get_post_meta($post->ID, 'notice_content', true);2 Q8 G7 p' v2 w. a( ^3 t
      ?>) q9 `1 D! Z# h* M- B9 Q9 ^
      <p>4 F0 y, F3 N# A4 u& p) D$ G5 u; M
          <label for="notice-title">Notice Title</label><br>& ?" I  C3 Z$ o. m
          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
$ h+ l& e8 G1 K. t8 I      </p>
! n& \. X3 C$ K! o! h; D$ ^      <p>9 a; _; P4 `) p+ T5 e% }
          <label for="notice-content">Notice Content</label><br>- E1 F" E4 N9 {$ H/ t! U4 m
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>8 E: m5 K* a' C9 K" @
      </p>
' o2 }# S0 x: p, E9 d0 S, }4 ~+ S      <?php
8 h9 O" h) X" F3 V. E   }( y8 g5 K. N' h- M: ]7 M

' H, x- D) d# d( c; t   add_action('save_post', 'save_site_wide_notice_meta_box');8 T6 Y$ n) P& p& C& F. m! L9 f0 D1 Q
   function save_site_wide_notice_meta_box($post_id) {0 B* t) X- L% b9 O$ S
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 h2 a  J% U% M6 ]  ]! u         return;& I. o- V# J* k" G7 a0 U$ ^6 F3 C. U8 Y
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)1 q/ ~  z$ i( Y- C2 A% g- X
         return;
. r/ a$ i6 F* J, V+ V( a' M
: d+ _0 r0 ]" G+ Y) o  G9 M; w* }      if (isset($_POST['notice_title'])) {! t9 a/ ~6 t2 W( N$ ?$ U
          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));4 w( q$ |9 G; _+ i4 Y$ e& D
      }9 A7 R; M: B7 _  K8 \
      if (isset($_POST['notice_content'])) {
8 j3 T2 u- B2 O# h9 w6 ?, P          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
, w0 ~2 V! l* _! F: `1 ]) W2 m  n      }. b2 V8 [6 z: x- P; X6 I1 q- t
   }, w- L+ R- {" x+ r8 J  ]/ {
   ```6 Z' Y4 W) e$ d# Z* y  \

1 Y& j! H* [7 E  Z' z! u; H   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。1 f8 m0 m& ^/ U& J
4 D, z' E& @- }" Q$ S
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
; c, Y: F0 z+ M. H- e; F6 V2 j" A- S0 p1 V* s
   ```
5 x+ k* M: ?$ l! C/ C   $args = array(, Q- r  ]# y# l& {1 d
      'post_type' => 'site-wide-notices',' v% t+ v. Q- Q1 \% D
      'posts_per_page' => 3,0 l& q* S, {7 b, E
      'order' => 'DESC',
2 x# G0 J2 s& P* v2 |# I+ v      'orderby' => 'date'4 {  J! B) K1 m, n2 c2 U: `
   );& E$ T6 Z2 z' s- c- f' h7 O+ Y
   $query = new WP_Query($args);
; U$ Y2 d/ s7 X: Z   if ($query->have_posts()) :. E; T" Y" G) h& A: }
      while ($query->have_posts()) : $query->the_post(); ?>
/ z1 W, G7 ]8 a- Z  s          <div class="notice">
- k! B( B8 }6 F+ n              <h3><?php the_title(); ?></h3>
: T" G3 H) R! j- Y, W              <div class="notice-content"><?php the_content(); ?></div>
( \# n. c* b! s# @( ?% g# U% H' t: ?          </div>: u5 a  R# O; h$ T
      <?php endwhile;
5 M( D; C, P' [      wp_reset_postdata();
2 e7 ^; ^. \5 y  F7 o" g# e   endif;
8 Z2 i, w" a' H7 i4 G. ]  ]! C   ```
2 K4 R2 |' L( Q- ]; I+ h6 W' a
- `, h( t0 f+ h8 n& A' P% f( x, W   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

QQ|Archiver|手机版|小黑屋|通达产控投资 ( 粤ICP备2023021749号-1|粤公网安备 44030402006137号 )

GMT+8, 2025-11-24 05:23 , Processed in 0.071974 second(s), 4 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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