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

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

[复制链接]

328

主题

499

回帖

3260

积分

管理员

积分
3260
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:
% q( q* f! T( s% [+ n- H! {( g+ m9 ]9 E$ i/ \
1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。. t1 l  K/ y1 b$ B+ h2 f- J
0 U9 H4 ]7 ~1 m) C( k, n+ \# ?+ C, L
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
" U& \  e& {7 `/ m  a/ P: {5 F" i% B) p+ V5 o4 I
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。5 I7 V+ @/ L  g/ e8 w- ?7 k; G& U

9 g$ C' e9 D1 b4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。6 j% o; a1 e, R/ N- Z

  ^6 C; ^$ V  n8 x8 }, M注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

328

主题

499

回帖

3260

积分

管理员

积分
3260
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
' y" ~1 w# }8 b7 |
! U6 J: {- a" G如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
' o, W2 G* }, G) _! h
9 s# @# s) [, C# ~. u7 B# A" n1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:
+ P3 c$ b% u9 b! M+ i6 {  V5 p. y7 O5 k' b/ |
   ```
+ c; @+ p, F6 }* b9 D4 [+ g3 K   add_menu_page(
- |1 O6 y& {3 a7 y; Z/ `       '全站公告',2 C( n7 b) {9 |3 Q+ ]6 e
       '全站公告',1 t( @* _; S( H$ g) _( A
       'manage_options', //需要的用户权限
7 V! U6 i* q/ X" i! q2 Z% E2 R       'site-wide-notice', //后台页面的 slug' F. m9 A" U* E5 [) V7 w2 Q) F
       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
! e' Z& k/ F$ v2 E  h       'dashicons-megaphone', //用于显示在菜单中的图标
4 O: Q4 i/ j9 R% M/ O& _: k       30 //菜单项的位置- x! e  u2 l' F7 q; p  B
   );
' T! ~% b2 r& J! l4 D# X/ @  D0 t   ```
7 K2 n; f' k* I& e3 `* v' }7 e: y# Q( J
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。
8 M4 Y6 P7 m9 Q
4 {) V$ Y0 H1 j) E8 y2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
0 _' h& }8 Y# O% P8 n5 _+ J# z
" y' |3 @2 x- k3 R   ```, L. m; J7 h/ u9 W
   function render_site_wide_notice_page() {! P9 z  R$ X4 T: U* y) X. r+ U
      // 渲染后台页面内容! B, @5 R8 J: U3 c2 b
      echo '<div class="wrap">
$ L: w  @; m7 v1 X! m          <h2>全站公告</h2>: C- z( R" Y1 Q; l
          <form method="post" action="">" u6 I7 b, ]2 ^3 `
              <label for="title">公告标题:</label>
& U  a' ?  r  P5 f! d              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
* {& Q9 n. _' M9 X+ O              <label for="content">公告内容:</label>
, k" J  V2 c5 n              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>. C$ ?1 X! G$ b/ s
              <input type="submit" name="submit" value="保存设置">" H6 P, T$ G% r! V$ ~* t4 z/ f9 u
          </form>- ?0 o2 L0 Q, [1 }9 `
      </div>';
; C+ k- M0 }' F; l3 |6 G3 Z. M* f6 o
      // 处理表单数据0 C; \& K7 N& [& \; Y( Z& K
      if (isset($_POST['submit'])) {" P3 f' q' A+ c
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));2 {8 Q$ s8 l* b) R& z& o' J
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));7 R1 ^  L6 _- p" z1 m
      }/ B2 r* c, {( v
   }; ^! \- ^& C1 z# k  r3 {2 P! k
   ```
5 j5 p% h  o- L7 ^4 C& h, Y$ U: ^. ?% ?! J- b$ L1 Y% }
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。1 f- G6 [5 ]+ D2 m) P
- y+ T) }" k8 ^5 {9 w) w
3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:$ p* w1 h* j$ [' D, X$ w+ w: n+ P
' p9 e6 J3 R' a
   ```5 C; Y/ e  v/ J, U- V
   <?php/ C3 O( Y( X  V- s' ~
   $site_wide_notice_title = get_option('site_wide_notice_title');/ U5 s5 r4 o2 [( l9 y) u
   $site_wide_notice_content = get_option('site_wide_notice_content');0 _4 q$ @" {6 M  y) D( z+ W* a0 `  R4 b
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
1 G0 n1 G! x1 @0 |1 s" u. b       echo '<div class="site-wide-notice">';4 K! D5 t8 S( A+ @# d
       if (!empty($site_wide_notice_title)) {
' d$ I7 K4 e4 e: h7 d$ w& ^           echo '<h3>'.$site_wide_notice_title.'</h3>';* K, ~1 b4 `; K& |
       }$ q% Y. _$ m0 [) I: C% w5 E
       if (!empty($site_wide_notice_content)) {
8 n3 B1 o; s  K/ a) y' p           echo '<p>'.$site_wide_notice_content.'</p>';" i) |# S, P/ N, z: B# x+ j& L
       }
. Z/ c& Z. T" K  Z       echo '</div>';
! Y, V) B8 `' X$ j; z   }% J$ l2 j, p( `
   ?>
8 v3 \1 j4 z) S. n3 C1 X+ w   ```# F5 y* O# }$ K: M; g  W6 K: T2 t' _# F* u
- c# L& ^' w  [
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
3 l1 s; }1 k0 z1 T
0 @: a) q3 a0 C. P, p% N( a请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

328

主题

499

回帖

3260

积分

管理员

积分
3260
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?
" i" c. `7 e; F, S0 |! F4 m4 g0 J# O1 W- I6 K5 Q
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:! N2 w; m$ U3 b2 Z, S3 L* V
# q" ~  s8 v4 @# X2 t" z" j
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:: [) G1 ~0 R& |

. m( c) v$ Z0 x& T% R```
* j4 U4 f/ R4 f) \" Gfunction create_custom_post_type() {
$ e/ W3 _# K5 m8 F    $args = array(
  q% T( b1 c: I; F9 A% V        'labels' => array(1 m+ ?; Z+ s3 I* l
            'name' => '公告',+ f: G$ z- k0 T8 U; f- [
            'singular_name' => '公告'
1 d/ S3 D5 \; [- |9 Y; t        ),+ D0 r$ D5 f1 u, A7 n. Q: z
        'public' => true,1 E' z% x' l1 e, I/ t4 r3 @" A
        'has_archive' => true,
& z  N  F. z! M0 |- ~7 Q% g* v        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),/ F5 F% x; {! ?
        'taxonomies' => array('category', 'post_tag'),8 f& J. p: T' S# }) O  o
        'menu_icon' => 'dashicons-megaphone',1 `1 M& D: |/ {: l7 a. `
        'menu_position' => 5,/ x- t3 n3 ~! P$ z
        'rewrite' => array('slug' => 'site-wide-notices')/ s* a+ i4 i( S5 ~2 t! j* k' z! w
    );7 K& t- T5 T5 ~# f3 \% p
    register_post_type('site-wide-notices', $args);
7 \( x" R9 ?! l+ K8 T}
* @& O9 w5 n- V' N7 E  T. ?$ Fadd_action('init', 'create_custom_post_type');4 g  c2 m1 d6 Z. [0 d% F
```
. h) E/ ~: I' `* e' B, {, u2 R  n" l7 I5 s+ K
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。( ]6 w/ j: y% ^% k5 p2 C# Y
; h; M3 w) f, a6 U: }7 R
2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
: b  M5 O3 W- P; W
' L4 t+ n0 I) P! A7 D  p% K3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
9 ?4 {3 b8 n$ x8 ^% v) z
8 m5 V& L& n3 D% F# G# P4 M" |```
. H. \- a+ t* n* ]. R$args = array(
& L& H; ]0 {% H+ D    'posts_per_page' => 3, // 显示的公告数量2 k" C+ s$ D6 [$ C! I% [! n
    'post_type' => 'site-wide-notices', // 公告文章类型9 j5 D9 v/ j, I5 f% \3 e
    'post_status' => 'publish', // 公告状态
3 L0 f1 g* i5 u' X    'order' => 'DESC' // 排序方式: T! F# M! M, ~
);
+ D# R4 X/ W9 k; n5 v0 q$ B
% o7 V8 R. ?* E3 O7 P$notices = new WP_Query($args);
  s: e& @9 k- _/ oif ($notices->have_posts()) :# e0 F" P: h/ G! e1 a) d
    while ($notices->have_posts()) : $notices->the_post(); ?>
  s% m  z+ T2 N- I        <div class="notice">
$ z) [) o3 \  R            <h3><?php the_title(); ?></h3>0 |2 ~0 N! y, @  {
            <div class="notice-content"><?php the_content(); ?></div>9 s# [, C- e% h. d) M7 ^- U
        </div>
- M7 L. I5 O9 ~- K8 q5 I  e    <?php endwhile;
* t  _9 E" }- c/ x, L! U6 eelse :
# A) C. y. z& ]9 V    echo "暂无公告";2 J9 y7 _1 m$ w; r8 o" n, e
endif;( A: f2 \+ x' v! e# h5 v
wp_reset_query();2 S9 a2 o- y8 W, [9 K" }) e
```
6 x( A9 Z# j' ^: z
: F1 {: i, w) [这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
# U+ \- p* c: k& B. l! X- M* v; n! h0 C) K8 ?
以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

328

主题

499

回帖

3260

积分

管理员

积分
3260
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
6 f% [$ g5 v: ~, V# A: V6 ^0 j- l5 v! D: @: J* `! i$ c) M
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。$ D; ~; Q$ A: e/ I2 x  M) z2 e

# C) o( m- q1 U" l以下是创建自定义插件的步骤:
9 C/ e6 T( P/ q  h$ {7 q, A9 w6 B! j8 ]) `  K# \+ l
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:$ Q2 q# z+ ^" r2 B, C9 p

% c, m% q% |! {! D  P   ```! ~% V0 e) b1 b/ k9 X2 r1 g( R
   <?php6 v2 K3 s5 x9 w/ M' W. f! Y
   /*
: Z3 Z0 p' ]) r/ g( X1 R   Plugin Name: Site Wide Notices Plugin- u3 B8 ~! C0 ]+ G. ]
   Description: Adds a new custom post type for site-wide notices.
( ^4 d5 _/ H7 l3 |% _   Version: 1.0
% h9 e4 r" z" [% s  @/ e   Author: Your Name
( @+ p; B6 n. F$ [8 d/ \   Author URI: http://example.com
, m5 B" |3 D9 k   */- Q! r0 ^6 C% \" u' V5 X: W

& C. r6 K9 J: P' _7 Y   // Add plugin code here...
6 P: q# Q- l4 X. \9 y   ```
* A) z: v( e$ @# o, C5 h6 l, Q- H3 c) V! b- s4 U" p4 ^$ n7 W/ e
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。" h0 C( j' z, l9 P' y
: Z7 s! }( }- q, L' I
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
1 x& i! Y" W1 B7 @' E* ?5 m. m6 {2 w. q  W* X; t3 T7 i
   ```
4 [. X/ n! d9 f   add_action('init', 'create_custom_post_type');9 Z4 f# B. Z  i. M2 V" s
   function create_custom_post_type() {
+ J" J9 c- t; h2 H2 K( ~4 @4 f% o      $labels = array(  {( ]# S% M' M0 a0 l, d0 @' {
          'name' => 'Site Wide Notices',$ \8 c& d+ @$ ^4 \/ Y; \
          'singular_name' => 'Site Wide Notice',8 w: P; m' ~3 v8 z3 p* k
          'add_new' => 'Add New',
( I# d% W' o1 u, Q          'add_new_item' => 'Add New Site Wide Notice',
1 e( S3 a: k& |# i" R          'edit_item' => 'Edit Site Wide Notice',
2 a+ a& J0 A, [  y          'new_item' => 'New Site Wide Notice',
: h1 K2 ~6 u/ p4 i. P9 V          'view_item' => 'View Site Wide Notice',
0 I! Z+ Y2 [1 @& m9 W8 M8 c# t          'search_items' => 'Search Site Wide Notices',( d& r6 P9 c. d: G' o. x3 s
          'not_found' => 'No site-wide notices found',: n% _% ~* T- E- D8 Y
          'not_found_in_trash' => 'No site-wide notices found in trash'  N! U* W3 B" T$ |5 b
      );
& F3 @, o3 H$ o' d* A
) L* \/ ^, I# `      $args = array(
- E# B# n5 `. ?5 {4 B          'labels' => $labels,
5 r  O) y1 o& [  |. r          'public' => true,
  c3 S5 I' q  H6 X1 u+ @          'has_archive' => true,5 e; {# q% P$ D  U( [" H
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( l: J4 Q8 G4 B
          'taxonomies' => array('category', 'post_tag'),
) e; Z  A) L1 U# L7 W) r          'menu_icon' => 'dashicons-megaphone',
3 p4 I+ ]1 j+ o, N% c  O: T1 F          'menu_position' => 5,- M4 O: ?( F! `$ o* A
          'rewrite' => array('slug' => 'site-wide-notices')
% K5 N* V$ y& a; M      );5 Q3 w/ F: S- w% p3 f" c
+ f$ w+ k( {+ `1 |, t$ Z4 m7 x$ y
      register_post_type('site-wide-notices', $args);, g; p2 z! V2 E
   }
5 Q1 S+ q% Z# }$ O   ```
9 D$ z4 }% L; ]; y. L7 k7 ]: b9 f5 `* j  o4 x* [
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。/ M& ~- `+ F  l& F) ^

! L" B! Q- S, _5 J# ~3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:+ _: r  H- E4 h! R+ f! O3 A
6 H5 y9 A- J. ], V
   ```% e% x9 k* K. l% e3 }# G7 I/ q7 \3 r
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
& P0 ^! h, L  @& C7 ]/ x. D& T/ ~! Q   function add_site_wide_notices_boxes() {5 q* Z: ~) r1 t$ A# ]; ^* D
      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');4 U" ?* S5 k/ ^) W
   }5 V' [( Q: D4 H) [2 B
! O/ J: C9 a3 ?7 M6 J8 H' `5 @
   function notice_details_meta_box($post) {
$ e# E. P- w# `$ J$ i; y1 _% t      wp_nonce_field(basename(__FILE__), 'notices_nonce');, M! W+ v: |; N3 @
      $notice_title = get_post_meta($post->ID, 'notice_title', true);
& ~& w" m. B5 d; q& G) T4 i      $notice_content = get_post_meta($post->ID, 'notice_content', true);
$ ~4 \( X) N! l& ?4 o8 ^      ?>4 G) P/ Y0 T$ s: d' _0 u
      <p>/ ~, G& h; E4 m/ w' m) t& f
          <label for="notice-title">Notice Title</label><br>
4 W$ V. j( u% v8 {, G  n8 q          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
  F& A9 U& K( R$ _( a# U  H      </p>
2 D4 c% s2 N0 m$ V5 P      <p>
8 l( R+ ~4 y0 C- x7 x; _          <label for="notice-content">Notice Content</label><br>
8 n3 O% X9 n7 b8 j          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>- M* Y$ V1 S0 v% O; `
      </p>/ F2 V9 A1 X3 I% P9 ^0 x
      <?php$ ?% Y  P% z' B* p
   }6 u) ~7 K- r1 o7 R/ {( G7 ]
, _4 D" W: ], O! j" N9 Q
   add_action('save_post', 'save_site_wide_notice_meta_box');
* B0 \7 r& s- K. |; U   function save_site_wide_notice_meta_box($post_id) {$ I7 V0 d' z6 y) b( c5 a
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
# n# C, u3 Q) E         return;: Y3 H* e( M+ X) z7 E) Z
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
% I7 p$ p$ F3 @5 n         return;, e; Y, Y4 _7 _2 o/ J
( k% @- N8 q# N% ^
      if (isset($_POST['notice_title'])) {
8 d, a+ d+ b; H2 M          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));  W0 {( a: @7 P3 S; }2 w
      }0 b( J, E" \0 I: x! ?
      if (isset($_POST['notice_content'])) {  t3 K0 K, h$ Y( {" T# W/ Z
          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
) N$ e* l2 \: `' M1 ]% h6 I6 ~9 M      }
. b) T2 K6 \' l8 P8 J$ Q8 ^' O   }0 K4 p4 y$ X# L9 Y
   ```$ m5 Q1 U1 h7 H% E
- _% U) R% n! R0 u3 F* B* e
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" |* Z" v2 _# C* N! J# `" @$ g- i3 S4 l2 m& {5 M# B
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
) o. ~2 i0 C3 |/ c2 x4 Y
% J& N8 Q, G+ _5 S   ```
7 }. e" g6 o+ S# }! h4 |   $args = array(
# S5 D2 k! H. P7 u( h. u      'post_type' => 'site-wide-notices',
4 s$ X6 t- d- E7 i. m1 r. ?8 b! O      'posts_per_page' => 3,
& c0 l- @) @, I  n) k: M      'order' => 'DESC',1 N& n5 W* V3 z
      'orderby' => 'date'% k; ^/ i3 K' g1 n/ B; s
   );: D/ P. @2 `8 U% p# r5 Y) ~7 a
   $query = new WP_Query($args);! D( o* m6 B. {! m9 Q4 x( g/ Z
   if ($query->have_posts()) :
/ h# m- E' k; b2 |      while ($query->have_posts()) : $query->the_post(); ?>
! m- E4 Y. I# t! w4 C4 r8 E          <div class="notice">- i7 @3 y& ]) J- N; x
              <h3><?php the_title(); ?></h3>
+ S( U& R  |6 q3 l$ s6 O1 Y              <div class="notice-content"><?php the_content(); ?></div>5 k; p+ g6 L# ^! P* W: A1 A
          </div>: ~! k8 l9 D! |0 f9 Y
      <?php endwhile;) m. m: [  v( E& t! K
      wp_reset_postdata();
/ p' R5 M- A( T7 i+ s) s   endif;* I! U1 }" h1 P! m
   ```* P4 O7 N' x( ~0 ?
. e5 x" g  N* a, Z+ t; ^8 i# \/ `
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-11 02:36 , Processed in 0.081789 second(s), 3 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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