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

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

[复制链接]

343

主题

554

回帖

3627

积分

管理员

积分
3627
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:  y4 J+ x- n- d# D

, L/ z& W; `8 \. j1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。
; M2 N2 f! b/ F; F" V5 l
- k/ }! t" A3 p0 N8 Q9 _2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
$ r: v, f2 t( Q+ _* |1 l, B" B
5 o/ [3 j  v5 X3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
% G+ A2 }6 p# l5 b& ~' z  j1 x
. g+ ?9 ^- f$ v4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。
4 K! X- G1 n* o' ~0 r4 q. m
8 R% `, c$ Q) [注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

343

主题

554

回帖

3627

积分

管理员

积分
3627
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
6 r9 s8 s( p$ R/ p& W( D- |! }: t! L% ^
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:% h  y* ^/ y9 c
1 A9 `. y/ E6 Q2 K. ]4 j
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:* o; X( e9 Z4 K

) l5 L/ H7 u# D: C# A" q   ```9 B3 g# a* R4 g) v
   add_menu_page(& i, |5 |2 i- D) h7 a8 k4 T
       '全站公告',
1 f( y4 Y0 @: T       '全站公告',  s' |5 A! k6 ^1 Z
       'manage_options', //需要的用户权限8 v2 G% `6 x9 V$ w( q' U+ z) r  L% B
       'site-wide-notice', //后台页面的 slug% Q0 z5 I# d8 q
       'render_site_wide_notice_page', //用于渲染后台页面内容的函数: Y2 W5 f5 V6 {! U
       'dashicons-megaphone', //用于显示在菜单中的图标
( I" V) u0 _; e: q       30 //菜单项的位置
8 H! T+ h% t* c0 F6 z% |   );
7 ?" z4 f/ B# D) @   ```: T5 T; m# l, O
, K2 {* [3 O$ W; S3 Q: N" x
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。) ?4 B1 `1 t- U8 [

' h" o" o" Y2 r- O2 B, B  g2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
2 v2 R1 Z3 {  O: t% u, g  `9 _
( ]# J/ `7 P" f( G5 U& d   ```
, T  T0 J; G- W* t   function render_site_wide_notice_page() {
( i. a% h, f; i. A6 A+ C9 o      // 渲染后台页面内容
) w* i7 O" v6 y5 z4 q7 M( b0 A      echo '<div class="wrap"># K* C: S: }8 S/ H: D  L. C
          <h2>全站公告</h2>
" S& ^- E+ T. m7 ?% v! c4 \          <form method="post" action="">$ T1 N% x3 R8 E; b2 B, _, b/ c# J
              <label for="title">公告标题:</label>
& t1 h+ y: K: U$ T7 f$ P: }              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
+ N* \% F4 {; b              <label for="content">公告内容:</label>
4 E6 x0 N! C( m3 n& r, z( p              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>/ `& r8 e7 e0 Y" ]2 U9 h
              <input type="submit" name="submit" value="保存设置"># I& E' a4 q/ b  G# p8 y4 x! L
          </form>
# m- c& y7 J3 r" e  ^5 L      </div>';
; B, B$ J0 w, j$ a1 u% h8 K! E* @6 P9 A# N. \* x$ g. Y
      // 处理表单数据
; ]% ^  z- B2 {- }: r2 z4 {      if (isset($_POST['submit'])) {
6 e8 z$ s2 O* ?% f9 I$ f4 E          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
  p& x- c" L$ u9 R4 b9 I  h          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));. C9 I7 P6 D6 d# }# x5 W9 `* a
      }
9 Y" `( x/ s) C   }( V  ]6 a8 @! G# e/ t
   ```
/ l  x3 J1 i+ j! s, m# K% ]% V& P
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
. q/ b" O' C: E0 b5 V1 u
( B# b+ E& h  U5 c3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:4 k7 N9 @* ]" U

' a$ _7 ], A2 G- V6 i0 ?# H   ```
" E) t: H* _8 g8 {0 z   <?php& P5 h, r8 m- e4 F/ e6 y0 E0 ^
   $site_wide_notice_title = get_option('site_wide_notice_title');
4 h  g: B  ^; r' {. z8 }2 J   $site_wide_notice_content = get_option('site_wide_notice_content');
2 B8 y/ F6 G5 M) P+ y7 F! {# J   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {% w: H$ z; j5 ?4 P; H' C% V! a0 o/ N  p
       echo '<div class="site-wide-notice">';
7 _- S2 a( p8 ?       if (!empty($site_wide_notice_title)) {
% a' c, E. S$ J) j# N& ?; s           echo '<h3>'.$site_wide_notice_title.'</h3>';7 t, h. N  I, R) q% O2 ]0 V
       }* M. {; d! C. p/ G! {4 Q+ {* z
       if (!empty($site_wide_notice_content)) {
3 f7 I! f, P( g5 s3 k           echo '<p>'.$site_wide_notice_content.'</p>';# z. t) u  H# X
       }
; m7 Y& v- d" r8 ?" A       echo '</div>';
3 s1 C6 y3 A, f9 R   }
- U0 C. q9 T; n' w5 U+ A5 l   ?>
0 M1 w6 B8 h" B7 u; {# ]; m+ t   ```
) q8 e5 t, h, g9 ]. R% i3 m' s# U% Z! _7 N2 o! T
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
# U7 a! R) J/ r4 f. ]! D- I3 x' l
& D, L3 M/ c& k# H1 f请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

343

主题

554

回帖

3627

积分

管理员

积分
3627
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?- \+ b$ Q1 f$ ~
1 M) ?% X& g' p8 b  j) U: Q
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:  `$ ~3 c! P& m, ^* g
3 u: G# }0 P0 E  I0 c' |
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:
0 p' q3 o( o2 v; L1 y# \7 D
: Y' R5 ]# E0 E2 P" d```
) w: t1 b( N: z$ x2 ~- g: D" Z) m8 ~function create_custom_post_type() {. n, D8 T0 x, e* ^. P
    $args = array(' F2 k* @; T5 N' J: O1 ?, |
        'labels' => array(2 z3 I9 D* g- G% R9 s
            'name' => '公告',9 P1 h, ]/ P- C1 E% Z, a' K7 P
            'singular_name' => '公告'
+ H8 ~3 W1 X( h9 i: e        ),
0 I( q& A4 Y6 }3 Y  {        'public' => true,8 D1 M6 ]* a# _, B
        'has_archive' => true," ~3 z, P$ C8 S* c: z% s
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions')," a# Y9 A" n- F1 {) r: Q6 B- t
        'taxonomies' => array('category', 'post_tag'),) O3 s/ Z& v2 M5 U
        'menu_icon' => 'dashicons-megaphone',8 D, `: f* N9 y6 Q9 |: \1 U4 A% _
        'menu_position' => 5,' @% i3 W  v$ D) v$ e
        'rewrite' => array('slug' => 'site-wide-notices')
* E2 Q7 P" b2 J5 b; S    );
0 T- ~2 D# Q: `+ H. x/ w( q    register_post_type('site-wide-notices', $args);1 Q( g# b; I! o7 p: Y1 l1 ^
}
& U8 {0 y+ k6 S, J  m" r% f% Oadd_action('init', 'create_custom_post_type');2 |6 g9 h8 H, S( ~$ M, i6 B
```
! C, t, x; n5 `5 p' n
% M% V" S3 o$ `0 R2 }0 U: r在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
( Q0 x$ j6 b7 e+ X: f+ O3 p1 m7 o- Z# x
2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
* u9 i4 ]/ Q, T6 E4 G! a
% t! B3 ~! G5 G6 I) p3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:2 C. j7 B* [9 I6 c7 u, V

5 f. e4 a, J$ h1 \" X. j```
& B5 e' H2 c2 z% u% s+ {5 ~7 _$args = array(
, o6 z5 f& b/ x2 f3 a  ^! i( v    'posts_per_page' => 3, // 显示的公告数量
# w8 Z3 x+ a4 o8 k& a$ G2 }2 G* Z    'post_type' => 'site-wide-notices', // 公告文章类型
& ~* ?" l4 B; [8 e( H, y9 \    'post_status' => 'publish', // 公告状态
2 ]8 C. |( N; ^& k6 S9 e2 U    'order' => 'DESC' // 排序方式
2 J! h# Y! M7 {' r2 |  C2 C( R);) p% |" e+ z0 J( [2 k( ^% h

5 n; |2 a7 u* \9 S$notices = new WP_Query($args);
( y9 F5 C5 R  F& F  ~& Vif ($notices->have_posts()) :# A$ }. s6 t1 F/ b
    while ($notices->have_posts()) : $notices->the_post(); ?>
! L1 e" B) b& s9 q! d8 K  v+ o        <div class="notice">3 y6 {3 \% v/ q1 j- F0 g1 y% p$ `+ r
            <h3><?php the_title(); ?></h3>
4 i( t! T. ?  c            <div class="notice-content"><?php the_content(); ?></div>& J4 n( R0 A2 W, n0 ]' H
        </div>
6 p9 [7 j) g9 y4 V- p    <?php endwhile;
. F+ r0 Y' i# `2 t+ p6 k9 H! Jelse :
2 w3 r* ~* W4 [: k) S) Z    echo "暂无公告";
. T* Q+ P% h$ ?& Z( f( x  pendif;
2 R& i' [) B# K8 k5 h+ Iwp_reset_query();
7 X4 `! i9 o( o8 n1 h6 |```
  G7 P( x/ ], N2 O7 B8 v: O- F' R
; Z9 d2 P4 }# R这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。9 I, x% H' I- G. k" {+ _8 N

+ W& i1 E1 G! }9 Z( e8 o. u以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

343

主题

554

回帖

3627

积分

管理员

积分
3627
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
3 z& L+ n  l: J$ t' a: J$ ]$ g# O2 p/ {0 u8 T& ], [' p
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
  i  [, S& \$ O* Z" f8 }# e* @! u& h5 n3 J, n) t
以下是创建自定义插件的步骤:# z% J" n2 |0 Y. _' N" x# s! M4 W
) u" e' e; h: i* |3 g! f+ q' k
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:4 H% j9 i* D7 I- c
; \) ~; u  P. R" V
   ```; [, O. q) d4 g7 ]9 Y
   <?php
+ c$ @1 D% [, o: N   /*
- y5 O0 W3 v% D$ Q   Plugin Name: Site Wide Notices Plugin
) Z+ l& N+ @: x5 D7 O8 i; _   Description: Adds a new custom post type for site-wide notices.
6 e' E6 K  C. a# V6 p( l   Version: 1.07 u6 k/ v) O8 l1 X6 D. @
   Author: Your Name
! K' x9 f1 S1 J- B6 w5 c* [   Author URI: http://example.com- t+ X5 @! H' K2 ?* |; @( g
   */
, X) W8 s' x% [- u* g' Q) ~; m  A" X
   // Add plugin code here...2 B$ `7 Y! [" x  N
   ```. |- G# Y) ~! E( k7 {' V/ Z" }% L$ R3 W

6 p( }  u8 D5 n: M0 G) {; e- T   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
$ ^) Q% U/ U- l. Q0 q  Q
- ~; r9 F: R2 ?- A- G3 B" Y2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:! k/ a# O2 _+ |/ k0 P* V" e
2 ~. Y: U9 v* l6 s1 \6 F2 j6 T% y
   ```; X; ?* i3 m# E+ P8 }( }& z
   add_action('init', 'create_custom_post_type');
5 k- g: r& o, C# S8 c   function create_custom_post_type() {
7 q, ]) j* V: o* Q# @      $labels = array(( D+ j+ V- i; c' g/ V+ N9 W- W
          'name' => 'Site Wide Notices',
! {3 J( [: I# ?# F; H          'singular_name' => 'Site Wide Notice',
6 X% m% |8 c, M  T          'add_new' => 'Add New',
, S: u0 [! q5 k2 P  V9 ~* u          'add_new_item' => 'Add New Site Wide Notice',
8 q0 v: M0 p8 r' U! _: F          'edit_item' => 'Edit Site Wide Notice',
! W! I  G' l. y/ p9 ~          'new_item' => 'New Site Wide Notice',
' O4 C- J- d" L2 S) q5 R          'view_item' => 'View Site Wide Notice',, o  h4 G; N3 G+ k* g: E# M: V0 q
          'search_items' => 'Search Site Wide Notices',+ L3 e6 n- [9 B- ]. p
          'not_found' => 'No site-wide notices found',* w2 W& q( W. h+ n' z' ]
          'not_found_in_trash' => 'No site-wide notices found in trash'& s; ^* S+ h) h: l4 W/ S7 X1 n
      );
* t  `* [1 }6 u* ?9 d. U2 o+ Z! D% |" H, A7 Z
      $args = array(6 f3 R0 L2 S9 H/ }
          'labels' => $labels,
1 Z$ C: z( [& v. n3 f- U. ^          'public' => true,
" t/ ]9 V  p4 a- m( f          'has_archive' => true,- [2 ~- @" B+ o
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),' D9 K% \' p, ~- t
          'taxonomies' => array('category', 'post_tag'),& I* A# b, o1 q; T  i! z4 Z( p
          'menu_icon' => 'dashicons-megaphone',' o* L) p; `' t0 v; \4 `" ~6 L  `
          'menu_position' => 5,- I0 d$ i- {6 V3 J0 n2 P$ @. D8 M
          'rewrite' => array('slug' => 'site-wide-notices')
' `% v, |# F: [/ s1 C6 j      );: V3 K/ }* B  h% O. l: s* L' E$ e

. V3 [8 e% ^/ C* e- P" S      register_post_type('site-wide-notices', $args);* c9 x: J0 F' p4 {  x% g
   }' j+ ]4 X: l/ a0 t
   ```
4 I' [; }, z9 [! g) Q3 k6 ]7 B
/ g, P* c; d: f- R+ U8 Z1 b   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
: q, g- e: A9 ^8 y6 C! N) q! l+ [6 V! Z
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:' f# p6 ]9 f/ R+ L% T3 j* S
5 `+ D" ]2 y& I8 m8 I3 h5 a
   ```
) V3 Y% B& F) k) n4 W6 C   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');4 D& g* g6 q# p% P3 d8 B2 K+ U
   function add_site_wide_notices_boxes() {* E5 m1 {6 g! |. J6 w& E
      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
' r5 H3 T- p" m4 X& O5 K" p   }
; r" Y" r: Y3 s! m" J1 N9 @. Q0 W9 [8 |4 h( T
   function notice_details_meta_box($post) {
2 P. ]3 a  m, B3 J) `% a6 _" Z      wp_nonce_field(basename(__FILE__), 'notices_nonce');
0 V8 P3 _2 ]- |) u! F: ?      $notice_title = get_post_meta($post->ID, 'notice_title', true);
; C0 k+ g- f7 M4 r: n1 r      $notice_content = get_post_meta($post->ID, 'notice_content', true);
7 Z8 }% c3 b* a- Y+ I# `6 F      ?>
; T: c! D+ [3 U      <p>
1 I9 K+ ]# @' H# L          <label for="notice-title">Notice Title</label><br>
2 U+ F( i" T* ]4 p, c          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">) r0 P$ A$ G' R
      </p>
& \, e( T6 e  \0 U7 ]% }# v* f      <p>
0 k5 ]+ u: T7 _# r+ @# t+ \% y          <label for="notice-content">Notice Content</label><br>
& R1 t; u" r5 ?: E; |+ |9 u          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
+ u+ j% \+ H# E      </p>4 e: ~% g, G& R- A* ]5 c
      <?php
$ |4 u4 Q" O3 M9 j! t   }- S6 J- Y6 c+ v! s8 v+ I
& u4 i# R# R% |( T
   add_action('save_post', 'save_site_wide_notice_meta_box');5 e+ D) `: ?% g  d# M! L6 L
   function save_site_wide_notice_meta_box($post_id) {7 b5 |. y, J& @
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 l, Z1 N" u+ n* r, d* y         return;
, g' `7 `2 r6 `& H      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- W; F% V: `6 [5 W7 T         return;: q3 d0 e, D# U3 x1 r
3 n6 W5 h- x" h, f9 ], x7 s7 ]
      if (isset($_POST['notice_title'])) {
; |% l0 d6 z7 f+ T1 G" D          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
5 v5 n: |! |0 Y+ k. t8 p9 \+ K      }& A5 S. E5 d8 I' U" G
      if (isset($_POST['notice_content'])) {1 y; g0 ]+ O% f+ b* ^
          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));: k& ?3 {2 G; ~
      }
& z( M2 f+ F/ y  g3 R* \5 b+ F   }
- k7 h/ a: t" R7 K$ E" H# d   ```
' ^& N+ C# z& C2 U0 D+ q3 x! H& s* {4 P6 ^9 X% B$ }6 E
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。! e3 m2 |) T7 s, G% Z- o6 }
" E0 C7 B  Y7 L6 W: E
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
( h7 Y# t! ^# y3 Y0 E% S6 u- f+ A0 i7 a
   ```$ \+ w8 ~% }. P9 q
   $args = array(( M$ U8 [8 P( Z: p
      'post_type' => 'site-wide-notices',# _0 f2 ^8 \; W, H
      'posts_per_page' => 3,
2 s9 c& E. @9 D. d* o      'order' => 'DESC',* o$ o. n9 K) E( |- L, |% o
      'orderby' => 'date'
. P% d: G4 H, V( J! e- l   );
" i' b: g4 t' Q" j( P- {2 C   $query = new WP_Query($args);0 v$ V  F6 h  ^  x; e* e
   if ($query->have_posts()) :2 S2 T( |" R- v+ ^
      while ($query->have_posts()) : $query->the_post(); ?>6 d; |2 B  a  r# V1 j
          <div class="notice">
( V& |) `: N  F. Y2 }  `6 U+ g              <h3><?php the_title(); ?></h3>. T9 s2 l6 `: G* E. Q
              <div class="notice-content"><?php the_content(); ?></div>" p8 F9 _$ h" s8 A  @; v) d
          </div>
7 G4 \! o2 K( a2 u      <?php endwhile;
: D+ D) C. a& N; ^8 g; j. [      wp_reset_postdata();
: i, r6 K1 S+ g' z# B, w! i# a1 x   endif;6 ~# d3 t! \# S' Y2 }5 T
   ```' f. l; i, H+ ]7 K) e% G

( i% E8 y. k- X: p# O* F1 b7 g   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-1 14:52 , Processed in 0.014223 second(s), 2 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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