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

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

[复制链接]

331

主题

511

回帖

3347

积分

管理员

积分
3347
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:, i1 i& e' }3 `2 ^
( ^( c- j6 `- i3 ]( M0 i, Y) @
1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。
# S, ^' [( P3 }/ ?2 v/ C. W
6 G8 N' }& u- d/ F2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
: _; `. [; o) E# o# S
, g2 R& c5 _7 M/ m$ q( s) V3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。; @$ i* n1 H9 L; Q

9 g) L% p4 s2 l, h9 [4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。
3 H0 T  q/ [: u  m0 e1 y+ u5 |
1 K; B# b- R9 \- k: P注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

331

主题

511

回帖

3347

积分

管理员

积分
3347
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
  a5 W1 j  V+ d
! o' b8 ?1 S( f2 q: X4 Q& p如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:/ x& M5 \' X+ o1 m( }; f
2 b7 L! N7 M  f! a' g
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:8 y( y4 i4 w( c! A9 @3 ^9 }
$ ~0 S: c' K( z5 a: V* O: c
   ```: @' v2 @2 e8 @; h5 E" S
   add_menu_page(
9 u/ j, t" [& t" Q# R  o7 Y       '全站公告',& U2 H$ y# f  }5 L# X1 d
       '全站公告',
  O# b4 W+ o; x       'manage_options', //需要的用户权限
3 g# a3 W5 i7 V9 Y- p: X       'site-wide-notice', //后台页面的 slug, d. X) ^9 g8 P. @0 m# S, \; }" `
       'render_site_wide_notice_page', //用于渲染后台页面内容的函数9 K0 _* ^. q+ x! T1 J) s7 j! V
       'dashicons-megaphone', //用于显示在菜单中的图标4 V' q+ c( d# ]. m! H! j' M& C& m
       30 //菜单项的位置; b  v2 U% i/ H
   );
: C5 h0 L$ y6 Y0 _- a' c   ```- ~0 ]% S- x, ]% g* B

( S5 Z1 y* I; V   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。
: J% ^- g& }* D. |* g- U& ]8 O0 n# F
2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
5 V' f, Y* H( @$ t
+ i  |5 h: S; h# i   ```, y/ K7 Q2 D5 M+ `
   function render_site_wide_notice_page() {
; W- a, N3 Q, w: F$ M      // 渲染后台页面内容' E! s$ D1 z7 l9 M( r
      echo '<div class="wrap">
% b9 V/ [* Z+ V8 n          <h2>全站公告</h2>
3 R+ N; o5 l( C          <form method="post" action="">3 f2 v6 Y8 X7 x
              <label for="title">公告标题:</label>2 a7 I: ~( C) a2 \1 ]4 [) a
              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>' w. c( M& M" v, J" N
              <label for="content">公告内容:</label>
$ _) ]; J& f6 z) C. L6 s. X6 R              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>5 K, @! ?! W3 u! [9 Y' P
              <input type="submit" name="submit" value="保存设置">
1 r# i; l) `2 X          </form>
" ^& v1 L8 H0 ^5 e" i4 j1 G      </div>';$ S+ `3 j4 k2 i" `
. I/ Z! ]' D3 u& v. o1 o2 K/ U
      // 处理表单数据/ b, p! k9 V' U9 P" H. w1 g
      if (isset($_POST['submit'])) {$ [2 U" N7 E) B* B( F7 M  }
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));4 \( X  {$ @* Y& K3 Q3 T
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));2 q1 U1 D+ u- B
      }# ?& h+ t0 G( X
   }
$ V9 g+ i3 g+ k# W# k   ```8 e, ^0 `7 v4 y
0 i1 ?& i3 e% @8 M9 b; X/ E( x2 o0 }
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
9 n% J5 W6 p! j$ @
7 u: j. ?8 V5 \$ t3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
+ w9 l3 k/ i/ f, e& A
' W, y7 C* q- n* \% x6 y: l   ```9 ]4 e% `* E- Q- @
   <?php( L( E: [5 z! h0 ~6 v
   $site_wide_notice_title = get_option('site_wide_notice_title');9 s1 U" W1 N1 X. Q# L. z
   $site_wide_notice_content = get_option('site_wide_notice_content');
0 y" ?$ z  x) @0 R* K  z   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {0 C/ u' r- H* h/ U9 R' G0 _: K
       echo '<div class="site-wide-notice">';* a1 o4 q) ]$ x6 g9 {; R
       if (!empty($site_wide_notice_title)) {; Z( i7 J$ O( T3 p4 `1 X
           echo '<h3>'.$site_wide_notice_title.'</h3>';. r: `+ Y. z! g& T
       }
8 }5 p( O2 {( V( T  A       if (!empty($site_wide_notice_content)) {
" A) U8 @' v! Q7 z           echo '<p>'.$site_wide_notice_content.'</p>';9 O0 @: ~7 b7 K8 t( I) P4 j. l
       }
- d: a! C( p- T  u       echo '</div>';
) C/ H9 `8 s4 T; l. e( |) l   }
4 u: N5 u+ |) t) [& t* |3 x   ?>( o- O  W/ g2 F8 B3 J7 v
   ```) d% b9 X1 @; K( ]/ M! f8 g

+ d' }9 Z. K! @# l1 T8 @' }   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。) T2 L% C0 v; ~- E2 e" D- `6 l

  v$ Z- X, P* i4 g请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

331

主题

511

回帖

3347

积分

管理员

积分
3347
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?
* j9 ]. P# P7 c( t+ ~9 ?. @( ?* Z+ f2 J+ e$ f
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:
1 r5 h" n; f) z0 w# \* z8 a/ ?0 s; \. m; C) i
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:
2 {, }6 J8 ~' l. |) y2 T6 V: a) f/ x4 Q  r; U
```: v* x7 ?  s. P1 [9 O* s
function create_custom_post_type() {
6 F/ |# u5 c$ k, i    $args = array(
1 T* M. u; c% Z1 c& y2 L5 B        'labels' => array(
: ~& _' T; x. ~- k; \3 O  v3 V            'name' => '公告',7 B0 w/ ]! X( l  k+ t% y2 A
            'singular_name' => '公告'
6 C% I1 c. n1 \) {        ),& B8 m( A+ N5 h2 [( y
        'public' => true,1 B2 Y4 D3 y) ~
        'has_archive' => true,1 N  \( c( i4 e7 g. C
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
- @  H7 b! ~+ F- }. H: L" M        'taxonomies' => array('category', 'post_tag')," Z( w& q2 g5 Q
        'menu_icon' => 'dashicons-megaphone',9 N# ^9 `- P, {6 [7 E+ N, t
        'menu_position' => 5,
8 F( l0 y/ _/ g        'rewrite' => array('slug' => 'site-wide-notices')6 w0 ]% g) P4 E  C
    );
6 f5 B; @) H' A+ P! p- u    register_post_type('site-wide-notices', $args);
: n, q7 ?5 C6 T}
( b4 t$ a6 O& r- h+ ?. z: Padd_action('init', 'create_custom_post_type');4 x! s% K2 N. _9 L  Y6 b& U
```% ]- f2 U9 F6 s# A% }7 w) W+ l  {% I8 U

+ J; Z* Y# B8 `9 \: a7 n在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
2 B5 r9 M: l# `9 `" {) G0 Q) Z; f& g1 {; o) R2 n
2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。% g; s( x% ~' T& k: A
* s- ?/ k5 Q3 ^: N* O  f: I
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:# ?/ ^* m- O2 o" B8 X

& C! i+ f- {  h$ I; s```( z0 N# d# o/ S! C
$args = array($ k- L9 S1 l- d$ [, `- q
    'posts_per_page' => 3, // 显示的公告数量3 A; w+ n# l: o. y* N
    'post_type' => 'site-wide-notices', // 公告文章类型
0 j' K7 [- P% f1 `# E  y  S    'post_status' => 'publish', // 公告状态
9 ]% i% i  o$ P9 X- B3 I- C0 l% s    'order' => 'DESC' // 排序方式* B( {$ |, o; r( |' J" X  P
);. m8 f2 r$ d( b. A
5 k9 H, g4 }- c
$notices = new WP_Query($args);
3 k# v: m7 ~! s1 b7 q. hif ($notices->have_posts()) :
  t, c- ^  M8 d% r    while ($notices->have_posts()) : $notices->the_post(); ?>/ {3 x& |# d4 Q& m
        <div class="notice">
0 N! y! Q) Q9 X7 J            <h3><?php the_title(); ?></h3># S+ @2 S; `4 D' }" N/ a
            <div class="notice-content"><?php the_content(); ?></div>* u. o0 }7 r7 m* x- j  j, }: f
        </div>
8 o! p* W6 h$ D% f) v; A' \    <?php endwhile;
1 {7 o: G' \& Felse :
/ }6 V# {% \& g( r( G8 q: ?( O/ \2 N    echo "暂无公告";
0 i6 X. ^+ m5 l8 I3 D" xendif;' }0 i( `; }* [" Y
wp_reset_query();
3 [* h- M7 m$ u" j```
7 M1 E$ I. _- x- g- ]8 f. f3 S) s0 X! H7 E
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
$ G* [/ ?8 V+ ~2 P7 k, B. @' t! k  n0 c
以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

331

主题

511

回帖

3347

积分

管理员

积分
3347
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
$ q$ t0 R; ^! }2 C
" `  g" |1 |' _5 g如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
% @/ h8 G4 p, ?  q' M! x0 l7 ?3 }6 d2 ~( j3 ]
以下是创建自定义插件的步骤:
0 R% S" q. c( l. ?  q4 `7 n
& K* i6 u0 d& j3 A! U1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
6 D. T3 g0 a( w' G# e3 i$ v; y( d; Q' o! d5 D0 z
   ```2 {. m6 Y- y7 {7 L4 A; g  }/ I( W
   <?php3 R0 v; ~+ q0 R% H) T6 U6 c4 [
   /*
( c: A& V9 [6 f+ y# h7 a   Plugin Name: Site Wide Notices Plugin3 Z8 h9 v; h5 Y2 m5 N
   Description: Adds a new custom post type for site-wide notices.
9 e, |2 U8 y6 E) P' |   Version: 1.0
5 i/ }% G( B6 _   Author: Your Name
4 f0 U7 s' a) x. \3 S2 H   Author URI: http://example.com1 z6 N# s' j- Z) s8 r! w) C& Y) H
   */
( I' y6 W/ y6 G5 u! n6 W2 y  _! \7 A. [0 u
   // Add plugin code here...
8 f9 u# \9 i# [0 Z) d   ```/ |: I. P  B' F( P& e. [
8 h+ g% U6 ~5 l0 c- e. `
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。  \( J8 c8 }" B4 v5 ^/ j
- z- u" V1 T' v. k1 l
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:( v" Z+ Z4 v9 W! v9 N& P; V
7 {6 U* }- R7 E: w4 h
   ```7 J/ F) ]( V2 `+ U) E( C) R
   add_action('init', 'create_custom_post_type');/ T7 H$ a6 v6 O8 e1 f; t
   function create_custom_post_type() {: N5 C; o% w# e; |
      $labels = array(& m6 k' d/ \6 s# p, m- t9 G
          'name' => 'Site Wide Notices',
* B2 p# ~$ i! |          'singular_name' => 'Site Wide Notice',
! f6 M' r& B4 E$ G2 I2 g          'add_new' => 'Add New',
8 j" B6 d* A( m1 |* p( u          'add_new_item' => 'Add New Site Wide Notice',2 s4 y! [/ B1 p3 M7 }. X' u. ?9 p( S
          'edit_item' => 'Edit Site Wide Notice',/ j# l% ~4 P# i6 Z8 ~% f" F! N
          'new_item' => 'New Site Wide Notice',
/ D2 ?5 u& o6 E1 g          'view_item' => 'View Site Wide Notice',
! k8 ~$ m" {7 a1 L2 Q          'search_items' => 'Search Site Wide Notices',
! k& y; s8 I, A6 @          'not_found' => 'No site-wide notices found',2 T' d: ?7 }9 Y! x/ x
          'not_found_in_trash' => 'No site-wide notices found in trash'
% ?  \8 x9 B6 t: t% o      );
6 c  q  {( X) k% s/ D: J3 x2 R- f5 Y# y+ O7 N
      $args = array(
% F: r0 \) k5 a6 v6 _2 M) m- j8 ~4 H          'labels' => $labels,
+ ^' I# A( M7 O. |/ S" k0 v3 r          'public' => true,$ u$ E4 D: ~! t+ s4 P; {
          'has_archive' => true,' F$ C/ I) F: \  W  `7 }/ V
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
2 |! g9 r  S/ h& r/ H1 z          'taxonomies' => array('category', 'post_tag'),% Q3 L$ f/ r% `& H
          'menu_icon' => 'dashicons-megaphone',
. o) {& r; x  ?( y/ W: {          'menu_position' => 5,% L* J6 ?* r6 F& b
          'rewrite' => array('slug' => 'site-wide-notices')
5 u+ q- ~& ^6 `7 P$ c      );" h, h1 g0 w( a9 z) \/ F

9 S6 Q) v" _1 x' ], C& W3 }# \      register_post_type('site-wide-notices', $args);% G0 l# s. K9 [9 X! q
   }
0 }% o. F# z0 {   ```
% r1 h: d' o. x6 U2 e, U  M0 }8 m# \
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 M" R) h) B' W' p+ s- x7 a

) v+ E' M8 w6 Z4 I% c8 w3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:3 Q- p9 S9 r, A0 w& @

  D) u! N' a3 r3 K7 }$ P" C   ```
7 j1 |: e0 b' g/ m! c: t9 B   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
% y, e+ D7 h" o; e+ _8 F   function add_site_wide_notices_boxes() {
1 \+ O: t! ^! q, C      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');9 g' I8 J1 w# X; G
   }: j6 h6 Q& }4 l. _% n
  i9 R8 y$ e/ r9 D$ z  X
   function notice_details_meta_box($post) {6 t5 b$ I* c6 L$ w8 C
      wp_nonce_field(basename(__FILE__), 'notices_nonce');
% X+ P3 J& K4 a/ F: w& u$ A! h  @8 W" \' c      $notice_title = get_post_meta($post->ID, 'notice_title', true);
: ^" G, u1 ?7 A; g      $notice_content = get_post_meta($post->ID, 'notice_content', true);$ M: u+ v( `+ ~' R* O* B3 p' k5 f
      ?>
2 Y7 [& q# J/ H' n2 m$ M      <p>% S+ ^* r- w2 y. @1 w! ^4 c$ Y* v
          <label for="notice-title">Notice Title</label><br>' d( y2 n8 a; u! X# P
          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
& |7 y! t4 g, }/ w2 s: ^5 ~( Z      </p>! K" l8 Y" h7 v3 }0 B/ _! ?
      <p>
: b# q$ h, d/ V+ H) N4 z+ \          <label for="notice-content">Notice Content</label><br>
  f+ Q: p) v* X- F* X          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>+ h. L" s& k. e
      </p>
! a# L0 T: Z7 R9 Z% ~3 J      <?php' {# k- W5 h3 V6 n; q0 Q1 e* A
   }
: D8 \7 o0 W$ `: h/ U" X
  X: y" I' q3 _( D+ Y- F   add_action('save_post', 'save_site_wide_notice_meta_box');
, C* E' e! I- h# D5 y" {   function save_site_wide_notice_meta_box($post_id) {( y/ D0 r8 f  V0 V$ u: X8 \. U
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
1 {- A9 M. ?2 [5 A( c+ ~/ `7 ^' [. Y- S         return;
* Z) Z) B9 ], d. v& D      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
( [) U" W, C3 ~( e7 _+ V         return;
5 I" k% ]- @, [# b' c, I3 d% |8 h+ {$ h5 w
      if (isset($_POST['notice_title'])) {
! Z: n* z/ e0 B0 o# e# R/ ^4 k. g          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
6 W5 ~1 ]/ Z: B6 a3 c* \6 ~6 }      }
# F9 W/ y) h8 s% t. A      if (isset($_POST['notice_content'])) {; O4 b& f8 j7 I7 k9 Y
          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
4 ]4 J7 U. Q) D      }
+ K' Q2 p6 T+ ^! o/ C   }
3 F4 P' v& b+ `6 O* ^8 I( p   ```
4 k/ C( Y0 w) T# w5 y0 s/ E( d( i$ ~% a; I! C/ r. k
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
3 ~" @; h$ a; l2 Y) X
4 U) Z# p. |8 L/ r# X9 c4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:1 }: c& Z9 M, \! L4 O1 O" V

$ L8 k3 t8 r( \5 ^# S   ```
+ b3 r! h! D6 w2 i0 K   $args = array($ o4 _" t6 T4 N+ ~" E. K
      'post_type' => 'site-wide-notices',
4 C  U9 [  j5 h8 G7 E      'posts_per_page' => 3," v- h0 b: {/ \. }# e' {
      'order' => 'DESC',
$ k; v" G8 Z2 V- z0 l5 B% L- x2 b      'orderby' => 'date'( M0 x7 |3 Z5 j$ E5 f& _# k7 J
   );
0 c* ]7 f. {2 c; Z. G8 U   $query = new WP_Query($args);/ f- |, d5 j: F( ^
   if ($query->have_posts()) :! p: y  {+ U$ m1 G  R. @7 e
      while ($query->have_posts()) : $query->the_post(); ?>9 T! K1 A1 X5 q
          <div class="notice">
- W' `: T7 [* l  h              <h3><?php the_title(); ?></h3>
4 `0 Z3 q. ?$ p8 U( j5 ]- |              <div class="notice-content"><?php the_content(); ?></div>
9 t/ W0 Y! w7 X          </div>
3 H6 Z; d: ]( \1 N4 u" X' ~# Q      <?php endwhile;4 [! A/ p/ P5 _" B+ _" d( ?
      wp_reset_postdata();
1 U3 k  Q' M; t3 h5 K   endif;9 \! Y0 b1 x3 @" d( x
   ```
4 I8 G9 `# _$ g; J$ I$ Y  b9 t2 b! h/ [- r: Y; q
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-17 11:13 , Processed in 0.074463 second(s), 3 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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