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

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

[复制链接]

338

主题

520

回帖

3456

积分

管理员

积分
3456
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:4 s/ t; Q' }; B2 `
5 w, I' {% r3 H# N8 n, O
1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。% z1 [  h/ s! d% H- n. h
6 |$ B$ i' S- p" ]9 C( n: C% f
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。: Q( d$ d! d! g: C3 ^% v# o
/ o" O& @3 X  b* @9 o7 K
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
* l0 Z  L, Z7 t8 H. n; O, a: P( Y; z, i. y9 s) G" L  ]
4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。
, s9 y* i. b- P+ y+ f% x8 J
. p. P8 ^7 C: K1 {' C, U注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

338

主题

520

回帖

3456

积分

管理员

积分
3456
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?, G( }0 E% A; ~4 v' C2 m

! B4 g( W& A$ }9 O如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
7 _0 B/ ~9 J$ Y* f) K/ Z& A4 i# X- P; a1 j5 J3 [5 C0 a+ X: {  N# w
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:, e$ g9 d+ y" r* c# h, c7 O
7 y4 J) W  i' |1 D  X
   ```# p# q% G* `% T0 {' T9 R5 _- w6 k
   add_menu_page(
  B8 f% B  n# K+ N! \       '全站公告',
+ Y" V+ g2 m# P1 c1 p( t       '全站公告',
$ h+ A. r: \  ?* U2 D" {       'manage_options', //需要的用户权限2 O5 C- \/ w4 r  g* ~  H# S6 @
       'site-wide-notice', //后台页面的 slug
" e$ Y" J4 K0 N       'render_site_wide_notice_page', //用于渲染后台页面内容的函数. U9 B9 e( B5 O
       'dashicons-megaphone', //用于显示在菜单中的图标4 c+ \* d9 G4 U; o  u& b& C
       30 //菜单项的位置
% a1 @% p' ~' H2 \5 x+ u8 n+ M   );
6 r4 |# w; n" K' P$ v2 L  ~: n" Y   ```
  b- F7 h# }4 M' u' k0 x. b# m
( H8 _, t* \6 N7 z* p: [   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。; ^8 F' R7 j/ U/ n# x8 @/ @- B

2 s; t% K" t( D2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
2 C9 T% a8 F# j7 V, A
+ ]* s3 B  r7 P; ?) @! _   ```0 ]6 ?* o' H9 j
   function render_site_wide_notice_page() {$ e: ?/ I  X& `! I
      // 渲染后台页面内容
( _1 I' S3 j& i- V4 C2 v4 h% [; Q      echo '<div class="wrap">1 N* M, G# _5 }2 H7 O1 s
          <h2>全站公告</h2>5 d* F5 o4 Y6 B2 I% t% g
          <form method="post" action="">  R( G) T# T, E- R1 C) g$ M
              <label for="title">公告标题:</label>1 x& w+ {1 _" ?5 N3 }
              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
; [9 x8 C# _0 l( ^5 B( q              <label for="content">公告内容:</label>
% K- L# p- p0 {1 Q. e; J              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>$ l8 B" J  t# p5 w
              <input type="submit" name="submit" value="保存设置">
6 o2 i' j' K( u+ j          </form>
9 f) x$ C, w2 w+ l, h9 S      </div>';
9 E5 r( j% M: L
& i8 e7 ^& i0 y/ {) A      // 处理表单数据
' G7 w# _8 u2 P* p& ?2 k* e$ q      if (isset($_POST['submit'])) {
+ m! `$ i( C! o8 C4 W! J" p7 z          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
# i' @2 O1 y) M" I. K+ x          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));- \1 z4 m9 ]5 M* H6 ?9 D6 y
      }3 X. e. S( J0 H: M4 w/ P
   }9 q1 v8 t" Q( C0 O& u9 E) U: X" e
   ```
  _$ n2 @- {" g' `2 V7 ]8 H3 V, z' {
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。  y6 t0 E" K9 z' ?, x7 K6 S) L9 v4 `
  y2 v1 B; U3 H* x, x
3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:# B# d5 H8 `' S- h. V& d
7 g% n% j4 ~: X: Z1 _
   ```0 B4 }6 _0 j3 k! @
   <?php8 n& s% W2 E7 T7 J; |" q! z
   $site_wide_notice_title = get_option('site_wide_notice_title');
  A) _5 u0 M* x   $site_wide_notice_content = get_option('site_wide_notice_content');* k% L) ?/ o  R0 Z
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {" g) ^4 {. {8 ~3 K
       echo '<div class="site-wide-notice">';
  v2 o+ c/ x  m       if (!empty($site_wide_notice_title)) {
+ ]! U2 W  B; W           echo '<h3>'.$site_wide_notice_title.'</h3>';
3 _  @; _6 C9 Z2 v6 a" y, t       }" h! b- t" F, u" j, J! y/ o  u
       if (!empty($site_wide_notice_content)) {
* \) P7 S6 N3 m7 x           echo '<p>'.$site_wide_notice_content.'</p>';+ i+ i  ]6 C* v
       }
. ^) C! m* w. l3 H5 c       echo '</div>';
% w3 H2 L% ~# b( m/ Q: U6 i' H3 {   }
4 ?+ \, L8 Z! y3 P8 l* B- q   ?>7 U8 w# F, s, H" E. S7 Q& F
   ```. m3 @5 R, N4 v& U0 E7 N3 F
* I. V; n$ t3 S9 a. ^. D
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
4 A: U# x) ?. O) b1 ~$ H/ |. C% S- E4 l3 {' P8 m7 }8 C6 c2 u
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

338

主题

520

回帖

3456

积分

管理员

积分
3456
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?- ?# ]( Z& p- Q$ K3 l6 ^$ F7 ~
9 I8 s8 z( L# X! e7 L
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:
+ n7 L2 @1 B2 q* V5 \# U# b, d) F3 c( a
- u( h4 C1 W) j2 v6 y1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:
1 N! y7 z+ @. j, r7 R
, n' o9 I" L8 J" M: B```9 d0 z: G: G  e; `6 C! L+ W2 a& t
function create_custom_post_type() {. N6 ~. W7 m8 ^: @4 D
    $args = array(/ X2 ?" c! d' B9 h3 ]9 _  `
        'labels' => array(+ T4 A/ |; r5 g
            'name' => '公告',' y3 W4 J. |! e" o5 X5 O
            'singular_name' => '公告'/ z& x; ~* r0 i) g% |
        ),& J2 N. ^& s9 i8 }' u4 u% c
        'public' => true,
9 ?' _* u6 V/ g* Q9 O6 v. _8 N$ @& ]        'has_archive' => true,4 r) Z1 i- }: T% p/ `- G/ P5 k
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
: A* g+ @  T" O4 K, l! _4 U% W        'taxonomies' => array('category', 'post_tag'),
( H' |, Y0 t& L  {' i1 h3 G        'menu_icon' => 'dashicons-megaphone',
  }" {6 Q4 q! o        'menu_position' => 5,9 c9 f' n4 w- k0 _' M
        'rewrite' => array('slug' => 'site-wide-notices')6 E: U3 n8 i, u/ U
    );! D. y2 a5 E- H2 ^' L$ a9 S
    register_post_type('site-wide-notices', $args);" a9 u# |" J2 Z3 `! [# d1 \7 R
}7 Z/ e( V2 U8 {  {
add_action('init', 'create_custom_post_type');
, |4 m' j& I( }. W```' ~7 l% a' s: T3 i8 s- B
4 \8 u1 u* y5 }  d, K7 F
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
+ P) f' G- z# {9 K8 }
) \& h5 P( }; i0 J" ]% v! y6 W2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
* c' u3 T9 v1 A" ]0 U; |/ [  ]
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
9 {4 D: j1 k( X9 s" }' H! V7 [: P$ S6 ~' A! I! Y
```* _3 i% \7 s$ a+ H/ p' E
$args = array(
8 x  G* t( y) m8 @  H    'posts_per_page' => 3, // 显示的公告数量
8 ~9 t* _) J7 C* ]' r! w    'post_type' => 'site-wide-notices', // 公告文章类型$ S1 ^. ]4 F. I, l7 d4 x
    'post_status' => 'publish', // 公告状态
6 g* H: n9 u  Q4 ~! x8 t6 U2 Q    'order' => 'DESC' // 排序方式
* f, r' l9 n5 ?);- Y# i+ L) C& E! t

# W8 t/ k7 h$ m% `- Z7 a$notices = new WP_Query($args);
' |- |# i' o; g6 eif ($notices->have_posts()) :$ L; z2 B# }4 v) O
    while ($notices->have_posts()) : $notices->the_post(); ?>1 ~2 c& K  x9 v! Q( t8 P2 g: F/ \
        <div class="notice">
- P( s( ?3 k2 ?% w            <h3><?php the_title(); ?></h3>
5 T9 x& X. P% {! s& b, ^& f+ f; F            <div class="notice-content"><?php the_content(); ?></div>9 Y1 ~& S0 m) _& z
        </div>
, e& Q; s  w2 ^+ l  C% ^    <?php endwhile;8 g7 p7 R( a" c7 D5 p" V' @
else :8 B9 \" t: ^" F& t. s; B3 M
    echo "暂无公告";
$ b. {* P& {9 Eendif;! ~5 ]' v* |* h$ c$ B; [
wp_reset_query();8 g7 D3 `2 j* `8 @5 \
```  H/ V8 d0 j) m8 d' s3 u

/ Z: ?) G" d( y  p这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。7 j7 ^* n& h6 I, |8 t2 j

- G/ T3 l, k3 g) n2 N% ~以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

338

主题

520

回帖

3456

积分

管理员

积分
3456
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?& x: o1 [  S6 \) ]' A5 z

! ?" h% S  q! i: U) T如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。, w7 j" n! Q: U' ~+ c

& a, q* V+ p7 z$ X: w$ `以下是创建自定义插件的步骤:
2 L, r4 X: b  o( {- _1 }5 a' d- l+ d% h0 |! A$ u- k
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
' E$ m' P0 o5 V9 t5 H' L6 Q% ?/ r  C8 W: h9 h3 t
   ```- k' X1 W# V; T1 P9 x- T7 K
   <?php; D- B, p, @# k/ m% K7 C+ R% C/ q
   /*
: \! H$ r" T2 n& _) P7 N   Plugin Name: Site Wide Notices Plugin
3 _, f' R8 D. m6 B: [9 a, x   Description: Adds a new custom post type for site-wide notices.
1 A7 l$ k+ a  K/ L2 H$ B4 d- K   Version: 1.0
$ e. k7 p1 x5 X  E  r, u: N   Author: Your Name
7 a9 O8 B% l  f+ L$ ^1 _4 j   Author URI: http://example.com# ]/ t' M% G4 D: \
   */# Q' T; W  O9 s4 ~+ g4 }

4 j; G$ U* q/ k   // Add plugin code here...0 i" N" d6 a2 e4 U  X
   ```' F6 l* U" @- [# l

7 K, ^7 i- _' U; ~1 [. z+ ^3 U   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。8 A, C# p; ?+ y" c
( \8 K- y9 d" q! k% `" h9 d' i
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:* Y0 J$ L3 d& V9 N/ A

/ X4 h0 _  ~. J7 G7 R   ```- A& ~- Z% Z3 V5 {
   add_action('init', 'create_custom_post_type');% ~, ~5 Z- N3 f* r  }& P
   function create_custom_post_type() {
% r# Z6 a- I/ l0 x      $labels = array(
' ]- {% G( s3 ?, e: @( p* c: E          'name' => 'Site Wide Notices',) O7 ?" z  L7 G7 A
          'singular_name' => 'Site Wide Notice',3 X! [: Y; u7 c/ i- O  J/ G- F9 S
          'add_new' => 'Add New',8 d, {3 O5 y0 z% z% H2 `
          'add_new_item' => 'Add New Site Wide Notice',2 Y+ H9 K: U5 o8 ^  T# s
          'edit_item' => 'Edit Site Wide Notice',
- p5 o7 I3 W- D3 J# k7 P3 z          'new_item' => 'New Site Wide Notice',
1 L: `# ]8 P' n6 s7 w8 e          'view_item' => 'View Site Wide Notice',
  _. t4 x% t' j5 J7 e! c          'search_items' => 'Search Site Wide Notices',7 T0 {6 x6 g; ~, a. c( w& i1 [7 y2 f
          'not_found' => 'No site-wide notices found',
, J1 j  x) g1 W; ]3 l% l) a" M          'not_found_in_trash' => 'No site-wide notices found in trash'9 g1 T* V& B# @2 t: E: q; u
      );
# z3 K5 |1 N* k4 @. M; Z: P& Z, i" o, O6 ?" b0 C4 |
      $args = array(
* j$ d/ N6 \- k5 H6 ~          'labels' => $labels,
5 m. r& {  G2 l1 O; \          'public' => true,
8 _: h) s7 g8 L          'has_archive' => true,0 ~7 Y) T% r9 h: D3 F
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
: U- L1 _3 N0 X# h          'taxonomies' => array('category', 'post_tag'),: ~6 X0 U( q2 f: f
          'menu_icon' => 'dashicons-megaphone',) @% Z* |( G' f3 r
          'menu_position' => 5,2 [( X* r$ m4 i$ \$ t
          'rewrite' => array('slug' => 'site-wide-notices')8 w& d3 R: R! M( X% H2 L9 k
      );2 P4 k! a. E7 a6 w$ @/ ^
2 ~8 G& F$ b! ~. ?2 M0 P+ ^4 n
      register_post_type('site-wide-notices', $args);5 V- F  [( x5 w. y2 H
   }  |) y* f8 r( ?2 v- e
   ```
) C3 `8 I3 ?" u5 s* K
+ {1 c7 a" |& l   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
/ Q; a: s$ S+ O1 H( N
' Q- I1 j" [$ T3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
: z! h" X5 x1 V' F8 l* o9 J( a0 e2 z( g& T
   ```
# i: q; G' i& M( q4 a8 }   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
; m0 S" _% N& k' C6 M/ c+ t   function add_site_wide_notices_boxes() {1 Y5 B7 X  M# L  s0 t
      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
& r3 h- F2 a  O) M   }
! L4 Y+ {; f  u; h% y$ C" D
3 l, m/ P3 M' j   function notice_details_meta_box($post) {
$ B/ h' P5 D# x9 g      wp_nonce_field(basename(__FILE__), 'notices_nonce');' Y1 e: k9 M6 L  F' n7 }
      $notice_title = get_post_meta($post->ID, 'notice_title', true);% w9 u5 S; Q3 p1 m* h! c  d5 s0 `
      $notice_content = get_post_meta($post->ID, 'notice_content', true);" c$ R! n; h. ~1 `- x& r
      ?>7 R( C0 [  _4 ~; {  c- \
      <p>
$ I& j( A) m7 J9 g          <label for="notice-title">Notice Title</label><br>& P9 r. l  R: R, g
          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">( i. q- p8 G) l6 V5 ~
      </p>
9 |6 U6 K8 u" A4 [9 F      <p>" t7 `& f& \) k# l
          <label for="notice-content">Notice Content</label><br>( C. {4 {; {& F0 V3 H4 e# D
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>7 }( J* ^% n4 n6 H
      </p>
  A, k9 \5 {1 t0 e- g      <?php1 |1 @/ [  j  {8 w
   }: s5 G/ r+ B) Q1 ]

% H! v% |2 C/ D   add_action('save_post', 'save_site_wide_notice_meta_box');
3 ]8 R) J% A# i% t; ?! m" b   function save_site_wide_notice_meta_box($post_id) {' \9 ^) `. [' B4 ^
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ C, q8 Y5 F- H/ _3 L         return;
: U, Y2 y# T' W0 m! q4 @      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% c! e/ D' D: s( k. x
         return;
, l" M# \0 g4 |# @. U2 t8 |0 G
% d  y/ S$ V4 R- L; H! I% }      if (isset($_POST['notice_title'])) {
2 c' E7 S* O. P6 \- m          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
" }- a' ]' ]/ n      }
* Y5 v$ ]& m! F6 V. M; @      if (isset($_POST['notice_content'])) {
" k* _' Z! Q& D1 U          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
; I% R+ u0 s, h      }  \2 s" |) O1 X6 f( O2 Z
   }2 m1 ~: \0 D% T; r
   ```% g) K1 U' M; u! u/ I9 P! _3 Q
2 j# L  v$ h8 a  ]
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。6 a# A" b2 ]2 G" l! T7 y" s0 w
, p, L+ D& r0 T" U/ [
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
% c  F9 }3 X/ W9 B# Y( u6 N" j' M7 W- z- Y4 X% F) }
   ```
0 A, C; q' Z. Z. J% E. z$ Y3 D   $args = array(
: H: A+ M% Q4 Q# T, z: e4 s1 w6 y      'post_type' => 'site-wide-notices',
3 p/ T2 P' u+ A* m. q2 q      'posts_per_page' => 3,
) Q. z2 Y% N' ?: o" }3 }4 S      'order' => 'DESC',# [( H$ f2 _( S  U" ?* [+ E
      'orderby' => 'date'7 r( w+ S$ R0 Z+ F
   );
9 P( I# c  H* S# g   $query = new WP_Query($args);
& z8 w% W6 w9 V& @* C   if ($query->have_posts()) :1 `. q0 N8 C8 k: N% S" [- o; C+ Z2 A; i
      while ($query->have_posts()) : $query->the_post(); ?>
' A; K% W% J+ a* Y          <div class="notice">- v) T  P) O( y! _9 {' w& e
              <h3><?php the_title(); ?></h3>" [! }% {/ L  ^+ v: E1 f8 n1 r
              <div class="notice-content"><?php the_content(); ?></div>
8 C# F, C/ h/ M  l          </div>  A! B  Q& l% e- p* _3 N
      <?php endwhile;% n5 \. s: O1 _+ E/ s8 e
      wp_reset_postdata();
4 F6 Q8 E( u+ |" D* }  I1 Z7 p   endif;
5 I' o4 {4 U+ i; {! E: p6 A   ```
: \2 {. q+ p7 _3 Z2 P4 c$ D8 y* r) R! I" i. ]
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-26 15:44 , Processed in 0.025345 second(s), 3 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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