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

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

[复制链接]

340

主题

522

回帖

3502

积分

管理员

积分
3502
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:
9 E" X' p# K% m
% Y6 D# b/ H6 L2 k1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。. {! p) H; ]& n3 U6 q

0 T/ H4 d$ [3 d/ p+ n2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。+ \, X' H, v" k

5 y+ u6 W$ V( I$ s8 Y! Z% o3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
) Y1 O6 R2 u( Q8 H2 U* Q. l) O' G1 c+ n
4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。
2 m5 b" @/ T7 X1 [
, m/ j: T8 W# r注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

340

主题

522

回帖

3502

积分

管理员

积分
3502
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
' D! m; t' e! q; M
6 b: Y: W3 X5 S+ w$ ^: a如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:# I. U# ^# c- M6 s. i7 y3 ~
* \. i/ _8 C) `/ n8 I
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:
# B7 d" H, z& J$ e, S
! ~6 e* |" `4 j8 ^  F   ```- N& ~, i) Q( `6 d
   add_menu_page(3 W, }- D( m5 e2 _; I  H# m
       '全站公告',# v5 `. d- s6 R* w- L; f1 v
       '全站公告',
2 ]3 _/ e# d. @* ?( _, O       'manage_options', //需要的用户权限! N( r) B* P( }+ m; j
       'site-wide-notice', //后台页面的 slug
0 P, O) o# v3 J/ y: o8 A       'render_site_wide_notice_page', //用于渲染后台页面内容的函数$ T; p& S" u6 H8 P: E9 U
       'dashicons-megaphone', //用于显示在菜单中的图标* B& N% p$ C7 s  W5 Y+ [
       30 //菜单项的位置
, K, e( p; b' @# l% D% F8 ]8 `   );2 N/ I" v2 n) M. J- ^- L% }! b
   ```/ M' W% l  |8 ^- {* E

  N& L7 X: c8 R5 d, j# z1 q   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。
  A& w+ I+ e4 P5 n0 O# i' @( @. K( L! \
2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:' _: g6 }/ L; d/ h
, t/ ~$ A* i+ c$ L7 v( U
   ```8 s- |# T3 X$ V$ O% Q- Y
   function render_site_wide_notice_page() {3 i5 l! O. ^5 w* N% _! t2 p, W
      // 渲染后台页面内容! K: x( y; p) z  F. O
      echo '<div class="wrap">/ v5 F/ L6 H) j6 I9 z! W
          <h2>全站公告</h2>+ y' ]" y# M. n. C" h
          <form method="post" action="">
. Y: E2 J* @$ \' M5 w              <label for="title">公告标题:</label>  @; Y/ k( F5 z& P% T
              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
, T/ @, _+ }2 }* a+ A  @6 B              <label for="content">公告内容:</label>, Z. V" V9 ?8 {' [4 ?5 s6 Z
              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>8 V3 F; V0 I7 K: J& y
              <input type="submit" name="submit" value="保存设置">$ o. F9 [5 P! d' Q0 R) x  }
          </form>" c% i! ?( h$ h9 D$ N
      </div>';, j: G3 J% F; i0 Z* Z+ @5 @. H: H

% L8 A* H3 a, }5 z& f  ^      // 处理表单数据
* t. W+ R# {  u5 L. d( D+ e      if (isset($_POST['submit'])) {
* z  R7 ]- _8 M  P" N2 a+ L          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
3 l5 O. C2 P2 O* W0 K          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));5 v6 U% E* J. N. D9 I" p# a* ~
      }" H4 }0 a& `( _3 B
   }
, L' N/ D# T; o3 L9 \! j% ]   ```
0 @0 _% v# V: Z' a9 d) M; F$ R
4 a, F/ z3 r5 B. K   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
( g% o/ r! L5 Y/ s$ j% t3 p2 o: K6 e% v8 c6 T
3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
" I& [" f- F" M7 y. s  h
' i; u) r! i* |: _0 i' W   ```! N( r/ O7 X  V
   <?php, u+ `0 S9 U5 |. s
   $site_wide_notice_title = get_option('site_wide_notice_title');
+ j+ n5 b# A2 I! J+ M7 y   $site_wide_notice_content = get_option('site_wide_notice_content');( V4 K: G+ X" ]' S; I% w
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
3 L& Z+ p* O, K       echo '<div class="site-wide-notice">';
' y+ [+ b% c( L& Q, I       if (!empty($site_wide_notice_title)) {  z+ e% o5 u2 K( T) j! h
           echo '<h3>'.$site_wide_notice_title.'</h3>';
- Z* f9 }% |( q       }
7 r, w% ~4 }* t! f- C9 ]       if (!empty($site_wide_notice_content)) {' U8 d2 }) \( I3 o" j$ [/ l
           echo '<p>'.$site_wide_notice_content.'</p>';
. P; u" J3 j3 \( Y       }$ p9 f4 R- B1 a) _) X  _
       echo '</div>';9 g1 k4 [! ]6 E- G! M
   }
9 r; S; ?" J% W% A2 N   ?>. e8 Y5 h: v$ ]' e8 J* n
   ```6 b. {+ z! f3 a; H

7 P% X+ D+ b. e% L   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
$ H; J* J7 k$ C5 Q( ~3 m# V/ z1 s: q, w7 e! G  W
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

340

主题

522

回帖

3502

积分

管理员

积分
3502
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?6 W5 X$ @2 e3 U/ Q$ V

5 ?: [9 s" K# h. j% Z2 f您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:. M4 r1 O6 B- R  g% t

  x" q1 z/ \) p( ^1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:* S0 S8 q& t2 ]7 K5 X- ?- F

1 B0 p8 m: ]+ Q- O" d```
  m- S0 ]7 w+ C1 {8 t" jfunction create_custom_post_type() {
9 n& I( u9 y% N& |; Q+ j    $args = array(
3 R+ r2 `1 m6 ~4 R6 m        'labels' => array(# q( Q+ A/ j9 D& M. d
            'name' => '公告',
3 A) u. J, J3 c: J6 M9 C& t! _$ @            'singular_name' => '公告'
0 W+ E$ E! g) N5 N4 F        ),* v  Q5 D* [: }: `
        'public' => true,/ F1 X  @2 t% c- M/ c4 G0 `
        'has_archive' => true,7 r- P2 a" O% g* r& v% L! f, k' x
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),+ R6 j6 `, ~, R! @/ C2 F4 ^
        'taxonomies' => array('category', 'post_tag'),
! q% a) z9 _7 X1 a3 h) F: j% ]. E        'menu_icon' => 'dashicons-megaphone',# ~: m1 P2 A7 N+ Q& e0 d
        'menu_position' => 5,5 A1 c5 r! A9 [* n: H$ `& I
        'rewrite' => array('slug' => 'site-wide-notices')
' T: H3 M! @. R- Z  q    );( C. L+ @) n  V' \) C
    register_post_type('site-wide-notices', $args);: s9 r1 s( G# K2 p
}: ^8 y: `/ M* H! f- a5 B+ G4 {3 P
add_action('init', 'create_custom_post_type');
+ b* O1 @6 R+ K# N```# N& Q# b, u- f# I

: h$ |* u$ o' P在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。* m* {/ Y) n" K' ]: W8 K

! i8 u  a8 E  {. b- F5 D. M2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。1 u$ [. Y8 I5 q& f1 F9 S& y1 }6 G

1 X$ L: k* L( k- q/ r: d3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:& Z* U" Q+ y3 k5 h
7 S' [) l: M. X+ J0 [7 g- z) w
```
7 U" g7 _8 a0 m7 `$args = array(9 q3 A, k& O5 E, K/ ^2 p
    'posts_per_page' => 3, // 显示的公告数量
* F) f$ G1 P* R1 K! E1 w0 o    'post_type' => 'site-wide-notices', // 公告文章类型
) i% @! s# D9 Z    'post_status' => 'publish', // 公告状态% O- V9 i& C  _. l. s
    'order' => 'DESC' // 排序方式
& m7 b/ Y. u+ H0 j- U% U! z7 z* e);
. t) e( z0 H  e' m# r2 |1 ]7 ~3 B
. V! I/ q2 s( ~$notices = new WP_Query($args);
# G6 s1 {. f8 C  |if ($notices->have_posts()) :/ _5 Q0 c7 b/ e- w
    while ($notices->have_posts()) : $notices->the_post(); ?>+ [" `4 ~/ i' j! |$ I2 x
        <div class="notice">3 D) t8 X/ m5 r. V7 B  E% o
            <h3><?php the_title(); ?></h3>
- ~1 l  ^4 ~% s0 S1 k$ F            <div class="notice-content"><?php the_content(); ?></div>. B) U, E  O: {; l6 _. ^) C
        </div>
, H5 m7 W( ]3 E/ O  W' c- q% `2 d# z    <?php endwhile;, f1 z. F9 m; D* _8 K/ w! S: ]2 P+ P
else :
" h8 u  `+ K9 j" S  M" h6 ?# D! W    echo "暂无公告";$ [  e! x' A6 V0 n
endif;
* J# g/ o" _) @4 q5 J9 L; j4 mwp_reset_query();
* t, ~7 X: w1 D) I$ I" C```: F2 r% Y' z( Y- G% {/ L/ p* ]9 p

1 g7 G4 ~5 x: {这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。1 m( m6 M$ x! T7 T0 {) Z

% [, {! Q: @- t* m. f以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

340

主题

522

回帖

3502

积分

管理员

积分
3502
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?1 F1 j/ n4 K' p& [) K: I) L; X- g

! M# n% w% X+ z  W( A+ A# E4 @2 E如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。  Z: x1 Z! q5 F8 r' R$ z( c  u) f8 ~

$ W( S1 l+ p, p5 e以下是创建自定义插件的步骤:
4 k- V" K. w" c/ W, X5 |  M$ @- l* R4 t- Z' M! B7 p
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
9 W% \! C8 x2 u3 d- y0 T8 A& u1 B6 F: g# ]8 Z' I$ S5 S
   ```+ |1 J0 V) [: r" T6 ~* v
   <?php
( L  c/ v2 M7 y6 Z# t   /*2 _0 `4 o; K  D) w4 b
   Plugin Name: Site Wide Notices Plugin6 T1 d! M$ A9 f5 R% u
   Description: Adds a new custom post type for site-wide notices.
! @. P& a) W+ u* A" X   Version: 1.08 \& p0 S6 D9 G0 ^. E. Z$ u: T; s% q
   Author: Your Name
7 g) J0 E3 H& R/ o5 B8 X/ S* J   Author URI: http://example.com" T$ ]2 I% ^  T# H4 Z% e: Y+ W% o
   */
8 D$ _4 X! a( n1 b$ j$ p/ i4 c/ @/ t) B" s
   // Add plugin code here...' F' [! Z1 Z. I" T5 ~
   ```
5 T7 a- C) v; T/ b: Q, v& e
5 v5 Q$ C2 l0 l) Y2 z$ h   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。# G# o, }* u/ {* g0 b

' A: a0 d2 k& a2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:5 Z9 V, j( a8 [* }( g) h1 ]8 f

4 h6 v4 ?: K1 u) F   ```, j) y: y! s* H5 z2 _: e
   add_action('init', 'create_custom_post_type');
3 U7 s/ j1 Q( I  \: Z   function create_custom_post_type() {1 F6 h) ?* `" _/ q; k
      $labels = array(. A6 n+ ~; z, J' a* G5 q
          'name' => 'Site Wide Notices',% k4 q" ]8 R* C% L
          'singular_name' => 'Site Wide Notice',) N/ m: c0 G7 c7 S
          'add_new' => 'Add New',
, T# z- W" C  k! k' g: a          'add_new_item' => 'Add New Site Wide Notice',
& A! a3 V2 c- a7 \          'edit_item' => 'Edit Site Wide Notice',
4 s1 L) L. y) |2 A: F          'new_item' => 'New Site Wide Notice',
# b8 ~' J! ~; z. B6 I& L          'view_item' => 'View Site Wide Notice',% N0 Y" K! H9 F& ~# I
          'search_items' => 'Search Site Wide Notices',
/ S7 x- }- D1 L; N. ^. E          'not_found' => 'No site-wide notices found',
; C9 T0 o5 }, V3 }1 a7 ]2 K2 e' m4 Q          'not_found_in_trash' => 'No site-wide notices found in trash'' Y  Q* I- c% |9 f, {7 N$ D" k! _
      );0 s3 f2 n6 G$ o1 p( d
9 }% o1 e8 T7 H2 {: N# M
      $args = array(: O; Q7 _0 Z8 x# t
          'labels' => $labels,$ y6 j7 Z/ m  i) \  Y6 e
          'public' => true,- X  f9 C2 z! V5 `
          'has_archive' => true,
* B( p) D. A  T# n5 d8 t          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
8 G9 L  P3 i$ ]8 O0 d5 F          'taxonomies' => array('category', 'post_tag'),
7 }5 S7 y& ?0 K1 M# a! G7 `          'menu_icon' => 'dashicons-megaphone',7 J; J( H. G% r( ~
          'menu_position' => 5,# k7 ~. h3 ?' i
          'rewrite' => array('slug' => 'site-wide-notices'), k8 {! G. _4 ~, @4 _6 l
      );
4 \8 H5 `- b( c. s% K5 S4 E3 D, q0 I/ `1 ^/ K( n9 B: Z7 z
      register_post_type('site-wide-notices', $args);
1 w8 z+ u6 q# r+ R! N   }; o/ O3 I5 ]- j: b$ u
   ```8 Q1 U. p: ~" I$ T/ U: @9 s
3 C% M0 y) E! w9 G
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
* y1 t( x0 T* r% Q) K4 y8 h, l9 I0 C- R4 H: K
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:: J5 H5 [2 b& t7 W4 a
4 i% d/ J# w* O: ?$ I7 l
   ```
0 y' q- W' h, w; l   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
3 G9 R7 P& w3 b6 z8 J( s5 a6 Q   function add_site_wide_notices_boxes() {
& j; ]1 |' U# R# ^9 s      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');' }& `" D- A' k6 h6 p6 J
   }
: j; t/ j4 ~$ P# J* y4 d
9 x! l  e) g$ r; |% l: k   function notice_details_meta_box($post) {$ L8 s- A; O$ n7 R" C& @- l" Q6 w
      wp_nonce_field(basename(__FILE__), 'notices_nonce');
9 G& Q1 a, P+ W8 I' Z9 w9 {      $notice_title = get_post_meta($post->ID, 'notice_title', true);1 n/ l" ?  r# @: ]) P. y
      $notice_content = get_post_meta($post->ID, 'notice_content', true);
. Q$ h! }8 T  G7 s& S& L+ c      ?>3 D1 r: X& x$ R
      <p>/ g1 _* F9 F0 K, [6 y' ]/ r& A2 T
          <label for="notice-title">Notice Title</label><br>  K3 r0 Z9 Y& ?$ [. `# X
          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">2 z" k1 Z+ q! _5 ^3 k
      </p>6 F9 A. j* p- {1 n9 K9 o5 I/ P! ~
      <p>. b/ H2 O1 K& s" x* V  |* D
          <label for="notice-content">Notice Content</label><br>
7 N) p. Q4 X$ [: L/ `$ m          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>6 p$ }( E( Z& g
      </p>* R- \7 G6 n# U8 l$ Z
      <?php9 F  C! h* S) k/ P# b' U# e
   }* D* D: }3 i2 K7 r/ x+ H
- p6 n- c+ H8 C" T. a6 J& M2 s
   add_action('save_post', 'save_site_wide_notice_meta_box');
5 ?0 s9 P. y+ a7 a" K! I   function save_site_wide_notice_meta_box($post_id) {
  ]2 I% g  e+ g9 R; O! l2 ]8 E      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
6 e5 \; z0 S+ I: N. ^         return;
. ]3 K0 b( c' N* g0 q: P( D      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)2 K$ N: f- V, k: _1 H; \' W% [
         return;
' O# ~; Q% T2 ]! S' J7 m  @% Q: G0 `$ `
      if (isset($_POST['notice_title'])) {
2 q2 A0 N: V" j% E          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));+ N# z2 w0 B' U
      }2 x5 ~7 L/ j7 q1 [6 p' }: [
      if (isset($_POST['notice_content'])) {+ i- s) w8 G( m6 \5 p
          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));9 M. M8 d/ N: C& @$ c! @
      }. n# |2 A- H2 a2 ?% l5 s4 W/ n
   }7 @3 ]% E' P7 ~9 S
   ```
# s( t  h2 A. `# C0 U: R
; d# }( V8 ~% m6 {& v0 |   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
( o+ v% U8 P6 G2 p6 q% K1 P! F2 ~& x- C' D
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
, k0 f' n) K6 |6 k& g/ y% K# _
9 B* O& j- o% |8 `7 z/ v8 I8 L8 L   ```% W2 I* L) c. i9 F* N" s
   $args = array(
, Y$ J" e: N- z0 I% s1 A  E: T% x      'post_type' => 'site-wide-notices',: P% ]+ x2 I% D) g
      'posts_per_page' => 3,
3 F( n2 v- ]! n4 Y      'order' => 'DESC',
; e' Y' W; H0 c      'orderby' => 'date'6 _" m! ?+ F6 I* D+ V
   );
  Q% O" i, {5 Z2 y& j   $query = new WP_Query($args);) p1 U; O3 t* S" D5 k9 Z
   if ($query->have_posts()) :! q- c0 K* u( _: q
      while ($query->have_posts()) : $query->the_post(); ?>7 M6 J7 e2 @5 ?7 k& L: R. d9 y5 V
          <div class="notice">
) q, M+ S4 E& ^0 @              <h3><?php the_title(); ?></h3>
0 ?7 l0 O- y$ }9 K# X0 _/ [              <div class="notice-content"><?php the_content(); ?></div>4 L! A/ j% z8 f& `+ v; L3 b# J0 @
          </div>7 B4 U9 f7 f2 G2 |
      <?php endwhile;* D9 H/ n$ s4 R' v. R
      wp_reset_postdata();
) c2 H1 T0 H4 s8 I   endif;8 l5 B6 I* P' `$ G9 J$ `; d
   ```9 ]: Z, Q" s. Y3 g
" O$ u' ^; e  I/ R! U
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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