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

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

[复制链接]

333

主题

515

回帖

3385

积分

管理员

积分
3385
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:
; [) {9 M' y1 D3 o1 R
8 v: A- ?1 a& C1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。* ~) g8 z) `" B- i' m7 G( _
  C! h+ N+ T; E$ p0 U
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。! z1 S" g  n1 i/ ?% s4 }! r

0 L& ^! y  F! l, M0 w3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。% Z" J' r4 o4 J& f9 N7 ^7 j+ ?: J

, [7 I% ]' B; M7 P. y% L4 B2 S4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。( _6 H6 a: U( ~7 D+ a
& G5 a' t+ N% t! t
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

333

主题

515

回帖

3385

积分

管理员

积分
3385
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
; f2 ^5 h# ]% O, t5 D; \% d  N! y" r( G4 f3 W
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:; R# {  ?7 d6 o+ t. ^, g5 M

1 M/ ^; w1 n) e) g' ^$ o9 J; g0 m6 t1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:2 ?6 x+ v( Q$ n

3 L0 V' r% F! b% o& G5 r   ```, f7 D- C! i, s' g# d
   add_menu_page(& ]0 J% S# q9 @" Z5 B  j; |! ^0 p
       '全站公告',
; \$ @4 y! X$ n  g3 q0 h5 U       '全站公告',8 I1 S3 ^) _; W. C& K* G; \- D7 q$ f
       'manage_options', //需要的用户权限
5 K5 ~8 d" V! [/ G: v/ m       'site-wide-notice', //后台页面的 slug
- J( i5 ?7 {/ p3 C- ~( N( h8 G  c: v       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
% ]6 L9 X( z; }- u, Q' U       'dashicons-megaphone', //用于显示在菜单中的图标
! Z0 {5 M6 L5 }" U: a9 w       30 //菜单项的位置
+ ~3 U+ E' p. B' x2 I) E" B) a/ Z$ _& p   );# c1 A& n/ _& A$ C% j9 Q
   ```
: j; n2 \# K0 b) F3 j- g
9 |. B, q* K9 u' H   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。
; ?2 w3 r8 Q$ b) N& M2 [! X! p3 L6 U1 x3 s- J* ?& [# h
2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
: \, z3 I" j/ ]2 _9 l/ }: |# E0 a6 q4 Q8 y& x% m
   ```$ X5 }# J$ _2 a! w
   function render_site_wide_notice_page() {
  {/ m# j7 N" b; N4 K  R9 p      // 渲染后台页面内容
1 T  X1 c' {2 \7 Q+ E# ?0 b2 @      echo '<div class="wrap">5 b! {% O# I* O' W
          <h2>全站公告</h2>. }7 n' h( D' c9 Y, O; e! H
          <form method="post" action="">
  n7 D# J$ z7 A6 r4 y9 L/ z              <label for="title">公告标题:</label>
, ]& C0 W+ A; P( T1 C2 s              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>* v+ e" L$ [  d5 o3 ?* I$ q9 r
              <label for="content">公告内容:</label>9 \4 D# r& h% K2 V% C0 {$ Q1 D* ~
              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>2 `% G6 V8 n3 u' ^. X
              <input type="submit" name="submit" value="保存设置">/ I: S% }* W9 L! o0 @
          </form>1 @. o, z1 z3 `2 R5 g% I
      </div>';# w$ E- T; k+ p7 c2 m
9 v+ ~; q/ M2 d
      // 处理表单数据- W* G* `2 t% F' {9 N/ b
      if (isset($_POST['submit'])) {
/ \: A1 h7 B' E5 `  R          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
0 w) n$ A1 @$ g! ^$ d, C          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));
: s0 r/ t" H3 p' |      }" r5 ^0 s4 T) A9 V1 x
   }& j# E$ p2 U4 r
   ```
( c: m  I- M* V
: o0 H8 D- }# {; [$ Z" U4 u* p+ v: [   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
1 ]+ m' A. z3 ~! k5 |8 l
  ?6 B: c0 \/ `" u9 e1 f, B3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:0 L3 S6 s" E8 p( q% H

5 R& o! C! k* u2 S   ```
; h6 |/ F$ B: z   <?php  X* o) P, v3 n2 x, g
   $site_wide_notice_title = get_option('site_wide_notice_title');9 j" K5 F$ s! }  [
   $site_wide_notice_content = get_option('site_wide_notice_content');
. ?7 w! z" h5 c. Q2 F   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
) |6 H" C+ N5 t3 @       echo '<div class="site-wide-notice">';; i! O. M3 H9 E3 ^
       if (!empty($site_wide_notice_title)) {
& c- G/ \( P' M/ `2 |, r' k           echo '<h3>'.$site_wide_notice_title.'</h3>';3 {- V# Q9 B5 C; Y! O
       }3 z- K2 u3 @5 l+ d1 E# H& i' y
       if (!empty($site_wide_notice_content)) {" X0 w) t9 x9 @8 d. j; \* j
           echo '<p>'.$site_wide_notice_content.'</p>';* v* T* y9 a! R9 Y  P9 m
       }
" k% u$ K3 S- ?3 {2 z4 ~* J( f       echo '</div>';
& f- R, t9 B1 a* Z4 P6 r   }; ?" M- ^# G# Y
   ?>
3 x; o3 L: Q" J! }4 Q8 h& i   ```
) `- W5 U) x- i% w2 p: s$ v: i" k/ }9 v$ T$ d5 g
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
( F0 h0 l& m6 q" I3 W
- K# h0 L$ b; _8 y- @* E# u请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

333

主题

515

回帖

3385

积分

管理员

积分
3385
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?, J. ?# @# l/ o& i6 V
* ]9 ?  I4 C) z+ ?0 {
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:7 E" P5 ?% e/ i$ y/ D7 T$ h+ V9 L
2 X9 \% a5 x  [7 g4 H
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:' P: P5 [$ ~4 c

* v  d4 q2 E! h- A( A```
$ _5 T3 q3 G7 Jfunction create_custom_post_type() {
9 ~$ [  N" j8 c' T. _- ?" v    $args = array(  {6 a& p. T3 E4 a3 S$ l) b
        'labels' => array(
6 g8 t2 |5 X: g5 w# D7 `/ y0 V0 w& |# M            'name' => '公告',) B) H( H7 @* u6 L) b% j
            'singular_name' => '公告'
9 R$ V& n) b" S7 r        ),
1 d! m( }2 X' {. v        'public' => true,9 @, U3 V7 k" b& p6 ?, x: d2 _8 ]
        'has_archive' => true,' ?3 K- l5 Z$ i! f' W3 P0 L
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
3 M: g9 ~: e3 R2 A8 ?- M        'taxonomies' => array('category', 'post_tag'),+ I( I5 R- t$ r+ R
        'menu_icon' => 'dashicons-megaphone',
5 r0 r% D/ g  B        'menu_position' => 5,
' m# K( q% P$ C* U' l6 H9 W+ s        'rewrite' => array('slug' => 'site-wide-notices')
: a* Z& @2 {) b/ V    );
& U% Y/ J5 Q- s    register_post_type('site-wide-notices', $args);
4 R; |% d# z* v}
+ k# q+ K4 D" m9 jadd_action('init', 'create_custom_post_type');
& s( J9 t0 K( D4 g) R6 k```2 ?3 n- v3 M9 @7 O$ R* K
$ g- E% h1 @. m# Z
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
; a2 d: {7 M/ }* b$ T  a) p& i& `+ r& l
2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。9 K. a: \* O: g
$ N3 }' v6 L& Z$ u
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:: n5 e8 U  [. H; E6 N' X: P  _

# j3 D: m1 S7 \; N% |```& v" O% [2 A# o, p; s9 Y; c
$args = array(1 r+ _0 q9 M/ D& ?6 S8 d4 C3 R
    'posts_per_page' => 3, // 显示的公告数量
7 K6 M; P" q. i9 L1 K    'post_type' => 'site-wide-notices', // 公告文章类型2 [4 K7 D/ U4 J5 a2 y
    'post_status' => 'publish', // 公告状态
5 q& O  X7 d" T$ g    'order' => 'DESC' // 排序方式
+ x9 Q/ @. N4 H& B+ ~8 ~) b);& N" M0 G( R/ e! Z6 b

- L; f. M# u* e' @! @6 F$notices = new WP_Query($args);
- ^6 D' k% G5 g! q  sif ($notices->have_posts()) :
% v) ~" R7 p; }* m    while ($notices->have_posts()) : $notices->the_post(); ?>4 K/ v5 }! G7 }$ F$ S: ^
        <div class="notice">
' v5 r) }; f$ j            <h3><?php the_title(); ?></h3>/ a0 Q; \1 k+ g4 m0 X
            <div class="notice-content"><?php the_content(); ?></div>8 ?  k) S& w, o) L
        </div>5 j) E5 r8 O1 X. R8 ?! C1 x
    <?php endwhile;
. c" u8 n/ q0 [( m1 s3 X6 I" Jelse :
; j7 _" ]$ b7 [) f2 U$ y    echo "暂无公告";
! Y( w# U- Y9 V$ H8 o- dendif;3 z3 [) k" P5 u
wp_reset_query();
( U# C% F: U# _" k8 d  D```
3 F* W2 ~2 x. K$ }& G: ^/ ~& ]; v' Y) L" N& _8 \! ~
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。* V' x7 O  L, A" [- |& \

2 n& |1 o, B( y, Z8 [以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

333

主题

515

回帖

3385

积分

管理员

积分
3385
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
% C) c% N; F% b$ \$ \8 X$ z" f, Z. [& \8 P2 J% L' \
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
# ^) |# G) j1 t: U) f, y) b/ a
; O9 ^+ g9 o2 _. }, X8 [) i. y$ @以下是创建自定义插件的步骤:; Z& i$ F. b" z' h9 d

) m2 S& h! F  y4 n% w! j) t1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
7 H" P' }- r" K8 G1 F
# }& Z* W2 o6 s2 c   ```
: X+ Z7 q) t2 o4 j& }9 y8 ?   <?php
1 d9 e  ]# J: W8 j& T6 w+ h" c   /*
$ f6 ^- [2 U" @1 |* |5 n# O   Plugin Name: Site Wide Notices Plugin' Z3 M! |) d* g0 ^, B: w
   Description: Adds a new custom post type for site-wide notices.7 f& Y( }) J- O2 o  B
   Version: 1.04 x- H9 _# S, l+ W5 p4 u
   Author: Your Name9 G0 S# ^. z7 D* w  W* F
   Author URI: http://example.com
5 ^$ U( a, Q' i: G6 f/ w   */* x+ k( o8 z  l$ }7 \" V

8 Q* S& V8 q$ I. E   // Add plugin code here...
, `, m2 ]7 C2 w( d. [   ```
4 D! Z' @6 s2 C3 s7 g0 X6 N3 M
5 U& w- s: q8 e7 G   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
, o9 P% x/ Z/ c+ n; E% ]$ R  p6 U: F1 s1 @, x. ?% B6 U9 h; b! r
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:& R- }$ j/ [2 B0 {2 a% L$ P/ i

& x5 @- U9 N8 P: [   ```
8 J. t* N% c  a% h   add_action('init', 'create_custom_post_type');
6 }7 {; u7 @: }/ w5 D% `; o8 }   function create_custom_post_type() {, X  o% W. R: Q: q
      $labels = array($ F& Q3 a) s# o1 t
          'name' => 'Site Wide Notices',; \8 j& _  L3 W  ~+ ~6 i
          'singular_name' => 'Site Wide Notice',
9 K* K* E9 ^7 z' X6 L! h  M5 n+ }          'add_new' => 'Add New',: o- |+ k3 a+ i) P
          'add_new_item' => 'Add New Site Wide Notice',
; j+ ~3 w1 r3 M% p          'edit_item' => 'Edit Site Wide Notice',2 p+ k+ A( Z; G
          'new_item' => 'New Site Wide Notice',
3 x5 W) }2 j/ k- Y1 s          'view_item' => 'View Site Wide Notice',
7 W0 B3 @0 d: x2 }          'search_items' => 'Search Site Wide Notices',) P! {$ K7 a* t# u7 ^, A: @
          'not_found' => 'No site-wide notices found',
7 l' ~$ D3 k( e7 N( _+ X3 j          'not_found_in_trash' => 'No site-wide notices found in trash'
5 |2 `5 A4 b' e0 w$ N      );: K3 q8 ~. w* h9 Y
$ C8 D1 H: C* M
      $args = array(7 l5 _% z8 L9 V9 Y) m
          'labels' => $labels,( Q" y6 |* |& C/ I5 @  t
          'public' => true,
. J" I9 q- D4 t: E          'has_archive' => true,
' \; d) L) c6 {8 T- e, w0 I          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),) `/ y6 v- R) S) W
          'taxonomies' => array('category', 'post_tag'),
/ `) `& k1 O3 K8 ^4 @          'menu_icon' => 'dashicons-megaphone',
+ ^" z& h# N9 i- V          'menu_position' => 5,7 l& L; @6 S$ e2 f6 U$ }; g% S: n
          'rewrite' => array('slug' => 'site-wide-notices')7 I" }  D! M! u+ u; c  ?$ e
      );
& U# O! e) c& _- P, d" I& }# t( g/ W& f% j
      register_post_type('site-wide-notices', $args);
  }3 x1 s' q; z4 y$ c% C' [1 h  b   }4 Y; c* h7 J. o0 j* }. c
   ```
  R; L3 g# O7 F1 M' j% Q: g7 |
( M6 z' k5 e; e" g( N) `7 a   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。5 C) K+ p) ]  v
  P  l( N- h) y/ b
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
* a& q# ]3 I! C# h3 q. l
$ ]' x! Z9 [2 M- x7 ]& V  @+ p   ```; Y9 H* V! V- b) `  h
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
: ^# S' m7 O- R' B% @) N" P2 X" j   function add_site_wide_notices_boxes() {
0 e  A8 k7 X. d      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
- o7 n& O# `4 H- L/ j   }
! _5 N- n4 n' s9 Y8 ~1 t+ E) v- M" w0 d
   function notice_details_meta_box($post) {6 y, g4 W' i3 K4 @
      wp_nonce_field(basename(__FILE__), 'notices_nonce');2 B9 E- L# T5 w9 {- b! G
      $notice_title = get_post_meta($post->ID, 'notice_title', true);+ S1 ~1 k; B# Q$ O% y
      $notice_content = get_post_meta($post->ID, 'notice_content', true);
) b; s$ V' ^, S5 p; D, X: L      ?>
3 C1 C+ K* M* r      <p>6 l/ c6 ?% B$ \3 }8 F
          <label for="notice-title">Notice Title</label><br>
/ \, `' V$ @& @6 G. i& {: y          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
* s( ^6 W) a' v7 V- V/ \      </p>
2 ^; N$ s, d9 s3 `0 K      <p>
8 d( G! F1 z7 C' L) d          <label for="notice-content">Notice Content</label><br>1 T/ A6 }, `- B+ A$ Q; O
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
# t. W4 M* [+ h% F- ~      </p>
7 |) Y- Q5 X7 k6 V  K- T      <?php
8 u; ^; j8 ]& n! m$ N  t6 D   }1 [% `4 R# m- F7 m7 g- x

% f( ]+ z% i: b- n3 y   add_action('save_post', 'save_site_wide_notice_meta_box');
, Z3 R" x" F  L# W* h* h   function save_site_wide_notice_meta_box($post_id) {8 A5 H% c1 I- ?! l6 P
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))0 S+ k& r' Y5 A
         return;: e$ _% j& M% X4 A
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)# @6 {4 Q3 F1 ?3 {' S  P
         return;
. a" a! W. O* m4 ?! b$ _4 O9 U- S% X: l
      if (isset($_POST['notice_title'])) {. C1 d) f, ~+ }# w4 [& H
          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));3 h5 c1 ?0 d0 x9 q
      }
- q/ m5 [9 U& F1 l0 Z0 v      if (isset($_POST['notice_content'])) {1 Y1 d! e9 V1 R7 z: Q- o8 h  [9 L
          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
# }9 {- p- N4 I; _, O1 d3 d; a0 w+ X      }5 r  Y" i; c: w3 m. s
   }
9 g2 Q3 a$ C' ^6 o, G% a   ```
/ Q' A9 o: O- v5 T: W7 Z. h+ b: J4 w0 w6 e8 X6 F
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。) j" v4 w) M7 S; m( n2 I

' c1 C; s' D% E: ?; [4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
$ B8 Q" x/ @2 u* }$ ~. m' I3 ^  s. A2 H, N! d$ u
   ```
( l: W+ o4 }7 ]- G! K% z   $args = array(
5 C% J# f; R" }5 K& r      'post_type' => 'site-wide-notices',
6 k& n0 X" c/ p6 a      'posts_per_page' => 3,
8 Q  v% G" K( N* U  N' j0 w      'order' => 'DESC',1 l8 J1 U+ i" H& v
      'orderby' => 'date'8 V! I- q' ~3 K4 ?( L% ^% z6 J
   );4 M( P5 C" `6 W7 E
   $query = new WP_Query($args);
. D' i2 C/ ~- e# [: z6 {   if ($query->have_posts()) :
5 b$ a% u% l* ]% b, U: c      while ($query->have_posts()) : $query->the_post(); ?>3 @9 |+ C( i- D8 |1 |6 w; T( J
          <div class="notice">; E% J& X- h9 ]7 i: J
              <h3><?php the_title(); ?></h3>
' P& S5 Q  a9 t7 s1 L              <div class="notice-content"><?php the_content(); ?></div>* p( D0 X# c+ F4 n5 C3 Z  \
          </div>
5 n" f2 K/ H7 F8 j      <?php endwhile;
9 G4 A+ i2 K: R0 {, v! a% I      wp_reset_postdata();
7 i' [3 |( H5 ~6 K   endif;2 j% @$ P! ^8 R0 ?) u$ E( q* Y4 f+ d
   ```, i; f) z- ^) L3 F

/ B0 I1 n6 Q! L   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-28 17:15 , Processed in 0.097908 second(s), 4 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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