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

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

[复制链接]

581

主题

706

回帖

4842

积分

管理员

积分
4842
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:' w9 l% h) _4 W

, |( q8 t: ~6 d1 Q' v1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。. `- v, x* Z4 Z9 e  o
# w, d( n6 R/ g; r: o
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
+ v6 Y9 B9 |* ], R2 n
  g1 K' ^: [0 _  a2 k. O, T4 n3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。9 W, w. g+ f: l3 t

4 }0 l: J' y; p$ x4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。
" h( j. F4 c- i8 g# x8 o) a$ `3 L- y$ M
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
欢迎定制:13928122889
回复

使用道具 举报

581

主题

706

回帖

4842

积分

管理员

积分
4842
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
% y. d2 x, K# n( `5 l9 {" v8 W, @* @, [2 Q, T" j9 o3 ?3 G
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:- P& D; N- R- F

0 G1 T5 \5 I( Q6 _0 J- Q1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:3 c+ C6 ^* S8 z8 d: A' |7 v
1 k, v- v; |6 l, F5 R' s/ n; z/ `
   ```
( Y2 q5 _; Y# k% {4 u* j! X# G' u   add_menu_page($ c! N) T4 G2 _% b& h4 Y6 U- i
       '全站公告',: p# T/ h# I0 k( k
       '全站公告',
  d0 ]6 s3 ?7 A0 z; J' ]       'manage_options', //需要的用户权限/ p; i1 ?$ q' g9 k. D4 Y" i" n; {
       'site-wide-notice', //后台页面的 slug
8 E( P* u& L* m' m9 {0 F       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
# j' F4 u: c0 t& J0 z       'dashicons-megaphone', //用于显示在菜单中的图标1 Q0 R* v! v7 p, A& i
       30 //菜单项的位置8 }+ N; {/ O" q5 T
   );
, _$ b$ X) ~" H1 o5 }' X/ Y   ```
1 ^+ E3 V. ]4 `- v1 ?+ V; A, Q
/ y1 b$ X- Z) ~   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。
4 _0 I/ `) d6 g) s$ e% _: W
, h% \) {" t/ N! S6 ?) q: \. x2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:& R3 `6 D0 Y! D

1 d( L8 m0 c+ K) I  ?2 U* l) o/ Z% `   ```
" k3 ~! O2 m+ r   function render_site_wide_notice_page() {
% }2 u, \- g+ q      // 渲染后台页面内容7 Y3 f# ~$ M; b) {- }1 k
      echo '<div class="wrap">
) S6 l/ F" b4 o7 U! }! @          <h2>全站公告</h2># A  V4 a" x. H% u
          <form method="post" action="">
+ O% `3 L4 V, N! Z  k' r              <label for="title">公告标题:</label>
) L+ g7 s7 m8 W. a! K% u" s              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>3 M4 |4 a+ T3 p3 b% ^, B9 R
              <label for="content">公告内容:</label>6 a: X9 K7 t5 w6 V$ O# I6 b
              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>! K5 Y% I& b' }: `- _, i
              <input type="submit" name="submit" value="保存设置">+ g+ V% g2 n! C' w- s: ~/ t
          </form>
5 C' Q7 q/ w0 q$ n4 O) W$ A      </div>';& L/ b7 z) |% }6 i
2 f) Y, G) n0 ~' Z* y
      // 处理表单数据% m+ M; h- m  o& U8 G" ~% H
      if (isset($_POST['submit'])) {& |" o5 `! N1 `
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));0 d5 T. {) w3 S) w! D2 `6 |( s
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));
9 Q2 c3 y# x9 q. b8 j      }% j' r. {1 r4 b, x. N
   }
9 m; i* o! C3 [! w; J  H2 ~   ```
, r9 j- G- d# c7 ?$ x' k& ~; K% Y( \( n) G" `, ?: \
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。4 _+ x' U+ W% V/ ]# [# H8 u
0 U& k4 K' C/ Y* ]- }8 z& I8 n
3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
: D& |6 o9 I* x7 g* H
: F% a2 @! V# O6 Y& s3 J   ```) p) {! _# H) a* P2 r  b0 m
   <?php
6 P2 @9 f; Y. p% p4 ^3 K* ]" m   $site_wide_notice_title = get_option('site_wide_notice_title');* r( W1 ?6 z8 G' E3 x
   $site_wide_notice_content = get_option('site_wide_notice_content');' S1 Y4 ]% E8 H5 n9 d
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
+ V: T# c7 B  p$ w( ?8 p       echo '<div class="site-wide-notice">';
7 }8 s! A1 N  u5 R+ b2 R( Z       if (!empty($site_wide_notice_title)) {. @) c( ?+ w3 M- x9 X- L7 c4 R
           echo '<h3>'.$site_wide_notice_title.'</h3>';' g( D0 g4 e$ ^7 j
       }: _) o$ f6 m5 F7 `; t9 [6 Z% `9 ]) y
       if (!empty($site_wide_notice_content)) {
" T; n, `. r# Q6 t           echo '<p>'.$site_wide_notice_content.'</p>';! ]: F9 h% L! q( B( v& B+ z% F
       }
1 M0 u, k0 ]+ m/ Y: _$ g       echo '</div>';2 T: k, z6 M/ J( |% J
   }; G+ a9 `7 @  g4 f4 u! \: {
   ?>) h1 m* d2 k! j2 t' A( }
   ```
* D. c4 `2 B3 J5 y' e. T8 Q1 J7 C) R# \7 `9 m+ n
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。
0 @% z4 V0 l3 f1 D$ T% e0 s; x8 a: d7 y3 F( b3 x* q1 B' R6 _
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
欢迎定制:13928122889
回复

使用道具 举报

581

主题

706

回帖

4842

积分

管理员

积分
4842
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?
' H+ B% U; I4 f% N9 W. ~, @
1 A7 ?) F* \8 i您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:
$ q* ?5 X, W2 j8 w; w$ t+ C
5 C: Z& l, ]9 G/ |% [1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:, [4 C+ l+ j5 }) I% E. J

. i# g6 J/ d! n) I```
0 |2 b/ }  u0 H# r  @# [7 D- Yfunction create_custom_post_type() {
( y8 o! m* c  v, w( F! x" s    $args = array(
6 M& x, B$ B4 ]( ?5 L1 N        'labels' => array(. _6 L4 q; w+ b# y2 Y
            'name' => '公告',
& X7 w  x, u$ _# f% a            'singular_name' => '公告'6 y/ ~  j, k# i% s9 F
        ),
* X8 d8 a; _2 H. ?        'public' => true,; C4 v6 L1 c! b1 u- ?
        'has_archive' => true," t% p" s# h9 r7 m2 u
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
, w* V3 o' ]3 L) q0 Q5 y$ l        'taxonomies' => array('category', 'post_tag'),
0 H6 O5 x3 T7 d  f5 L        'menu_icon' => 'dashicons-megaphone',
2 M  X1 C1 X' x2 d% l        'menu_position' => 5,4 C( ~7 k, _" g9 l! r" i) s
        'rewrite' => array('slug' => 'site-wide-notices')% b5 W4 z5 g, x
    );3 K) A. o4 _" P; B0 N- G* F
    register_post_type('site-wide-notices', $args);3 }. ?+ `" `$ j: U4 d1 o
}4 }: o; V* v) H# a: w- |
add_action('init', 'create_custom_post_type');
( U9 s+ U/ y, u) g" t( Z) U5 i```  f0 q7 ^, y% q" G$ V

" J8 X9 V! e3 k; J在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。5 l2 V7 I% }7 ^% B) z# X

) B* [9 [1 ]1 k. S2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
0 |8 K1 |1 B" t3 w  P1 L$ f) b. I
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:, U, X& P9 C( A6 q* n, u

8 ~3 V( F  x8 E& a; z' K```
0 ^# s7 q! S( Q! [+ Q$args = array(* N3 c7 y4 g7 H
    'posts_per_page' => 3, // 显示的公告数量8 Y% c+ h1 ~6 n4 y! J6 h
    'post_type' => 'site-wide-notices', // 公告文章类型
& n" D) o- D2 B* e. ~    'post_status' => 'publish', // 公告状态4 l- u9 r7 o. }
    'order' => 'DESC' // 排序方式) A9 W3 l& _+ M! R3 N
);
4 w7 u" l9 U( I7 M# N4 a2 P6 G
; c2 O- t( p7 E$notices = new WP_Query($args);
2 a. ]  k7 x. X" {9 Q: R, `6 Xif ($notices->have_posts()) :
9 X4 s  U( X( B    while ($notices->have_posts()) : $notices->the_post(); ?>$ m4 X. J( T5 \4 U. w" n0 {6 s
        <div class="notice">
" ^( [3 _3 ^/ ?$ `/ ?) `            <h3><?php the_title(); ?></h3>
4 W2 s& P! ?) g" U0 w            <div class="notice-content"><?php the_content(); ?></div>0 r. g4 u' M  I# A& Y/ M( t
        </div>
0 i2 d8 V8 [" c1 \; I/ x    <?php endwhile;
. J. u; F. {2 p6 J) z& A/ L. _else :  c  i# s1 v- M: B. w/ V/ n- N
    echo "暂无公告";
, E2 |4 b$ E+ Z& i: v) Pendif;$ O  F( F1 d* [
wp_reset_query();* r) X+ {1 H; J' b
```
% j( C! W! ~7 Z6 m/ {
! t: Y% @( M! o  k, ?这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。6 T: d+ Y: y0 z
  h/ ^# x& {+ @- j% O0 ^
以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
欢迎定制:13928122889
回复

使用道具 举报

581

主题

706

回帖

4842

积分

管理员

积分
4842
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
2 [4 V( r5 ?1 L% y- [& w( [1 W3 W; m2 l$ @: D5 A
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
, }. }$ o/ D; A" z& P- Y7 f
- w# N) x# T& D1 P以下是创建自定义插件的步骤:4 k, h7 E5 r) v
# S) i  [' g' ^. K- I. H3 }
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:4 x1 f1 l% g" C, w. ^6 P

: }2 v' }+ l6 D1 G1 t& ~6 `& K: Q   ```4 P7 W( }- c  w6 @+ @4 C, Y
   <?php. z+ ?  _; E) l8 M1 O. o
   /*
' _1 G6 A9 H9 {" ^0 R& B. A   Plugin Name: Site Wide Notices Plugin
: z/ e- J" I0 @$ M) o6 y   Description: Adds a new custom post type for site-wide notices.
8 c5 _5 y2 Q* B  \, h9 J0 O! t   Version: 1.0
( b- ?: a" Q* ?   Author: Your Name
; b( O1 i( X6 z2 @: q5 w; O1 s   Author URI: http://example.com
7 F! B* w7 @9 Y/ P   */+ o+ J; a" O2 ]8 _
6 X( i8 A( g& G/ t
   // Add plugin code here...  y: a2 K% B2 Z1 O" _
   ```
$ M4 g6 Y$ ?# S% `4 W0 |& N/ z8 a6 f& l6 G# D
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
# D! `8 q1 W& @1 t3 b( a
! c! b. z- X$ D9 ^. E2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
! h9 q6 Y( E" A
) y3 m7 i; r" u   ```
6 o# O; c7 ~2 J9 G7 {: T. B$ r   add_action('init', 'create_custom_post_type');
, j+ Y5 `. C! z7 d5 A   function create_custom_post_type() {, ~, D0 V3 l) M+ z5 V4 E7 {
      $labels = array(
, s$ I, ?8 F$ O$ B: a) |          'name' => 'Site Wide Notices',
0 Y) m! \! x2 A+ ^5 F& k% T          'singular_name' => 'Site Wide Notice',: ~# t3 Y' T+ {2 E6 o, l0 o$ I! d
          'add_new' => 'Add New',
. ?$ B% V7 {6 b& X% _! H3 X0 v          'add_new_item' => 'Add New Site Wide Notice',
! f0 T5 Z' R1 e4 `          'edit_item' => 'Edit Site Wide Notice',4 p+ J; ?* I; m5 l4 N0 G/ D
          'new_item' => 'New Site Wide Notice',4 m) i  |! _. O- L# {
          'view_item' => 'View Site Wide Notice',
8 W  q6 S& ?. y" r3 {          'search_items' => 'Search Site Wide Notices',( V# |9 T6 ^" @5 s# c
          'not_found' => 'No site-wide notices found',
' m9 P1 u; X0 L/ S* }2 D* q# B  \: }          'not_found_in_trash' => 'No site-wide notices found in trash'3 [9 S' V; X+ @# C  `
      );1 `6 N# l. p7 ^0 m% y
' z9 T% \$ _0 D  M' M/ f
      $args = array(
  [# v" x7 Z/ P; p9 Z          'labels' => $labels,3 T$ D% i! y0 T
          'public' => true,, z! Y3 Y9 O9 R, e
          'has_archive' => true,2 S% p) q# L" ]" x
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
5 |; N' {1 L- m( D          'taxonomies' => array('category', 'post_tag'),
/ v9 _( t; Z# R& W5 {          'menu_icon' => 'dashicons-megaphone',
; [! ]  n2 V. s          'menu_position' => 5,$ ?; F4 i7 J, j) E* S: L  H
          'rewrite' => array('slug' => 'site-wide-notices')
0 w+ }. I0 T' f# p& O. v' E      );
- J% }# w' ?, k8 O! `" D
2 O( f) b0 M; K      register_post_type('site-wide-notices', $args);
8 v! x9 a& {* Z+ h, ]2 t   }
' u5 p4 |: o0 w  F" l8 V   ```. o% B4 W% |& ~- }
; c. C7 g! g9 ?  H+ q& Q
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。2 \+ C, e, Z8 Z+ L
+ p. _& f0 x& i& C4 {* e
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
- x) Y3 F; f1 A( D* a* z5 V2 j! ~8 p7 O! L; H0 H6 C# G5 B
   ```1 A* `% K' {$ N% p3 [) Z
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');( r' w2 Z2 m" y/ B/ C# `
   function add_site_wide_notices_boxes() {3 y+ J/ B9 Q! Q+ t, o) I+ j6 z' ]$ [
      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
. G: V4 V; q, g! T   }
; O0 m2 ?/ U1 p5 Q3 a3 x. i+ E; r. ]9 r2 d) P6 y- Z# b4 [
   function notice_details_meta_box($post) {
" u4 Q# E. _% B      wp_nonce_field(basename(__FILE__), 'notices_nonce');2 u" w1 X+ w7 {; ]
      $notice_title = get_post_meta($post->ID, 'notice_title', true);
- F1 K! P7 T9 E8 M' j      $notice_content = get_post_meta($post->ID, 'notice_content', true);
# J) I4 J1 n- k6 D( m1 O% {      ?>( ~5 P- D( W( y; M8 v9 y
      <p>
6 S9 H8 d4 a+ S          <label for="notice-title">Notice Title</label><br>
1 {4 r; {, m0 k" a6 f$ E  Q7 T          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">5 w8 G5 s) y$ n3 j% H, s+ H1 W
      </p>
! U4 H9 ~  d6 q/ {# r9 A      <p>
, N# J/ b" x  {* P4 _          <label for="notice-content">Notice Content</label><br>; A, y" F# }8 H5 b
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
1 l7 Q9 I3 j2 d# E1 x      </p>, F: D0 P" c! K9 S
      <?php
' n" T4 a% Y' }, J  q   }7 \$ _: ~# F; J: C' X/ G
* d+ R9 A$ G/ H" M5 U9 N' v
   add_action('save_post', 'save_site_wide_notice_meta_box');
2 t7 G, ^) ]1 I3 e$ q$ h+ p   function save_site_wide_notice_meta_box($post_id) {
1 v5 f- P5 I$ r7 `( D      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
6 A* K- h/ n8 D( q+ t5 {         return;0 e5 L, q% V$ k5 [. c5 f: f
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
" R/ ]8 ~6 P% \% i2 F( S6 E         return;5 m  ~8 F3 n0 _2 k, Y5 s3 t( R
  V9 _* \: l5 s4 I9 K% k
      if (isset($_POST['notice_title'])) {
9 E( ]5 _  _8 n4 y9 h  r- p5 I          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));$ L7 ~) H: i# E* j  E+ e
      }
  M$ Y9 b& H' C      if (isset($_POST['notice_content'])) {
" q% ^7 E$ u* o% T0 p/ N# q$ R          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
+ w) F; Q( l; k/ b% ]- z+ ~1 F2 c% L      }, E9 ]& b9 W: ]/ W
   }% ]/ X# z& O  }2 G+ G/ D' w! V
   ```9 `* {; ]/ J& G  `; r& ?
$ e& @! L8 q5 b/ N/ F% @- }  w
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。
" V& i& d9 D& _  e) J
6 E6 q1 l: z7 G( U  |4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
, Z+ n7 ?7 s2 f
0 A) ]- s8 w* s  S   ```+ T) F: s6 M6 g) p- G
   $args = array(1 ]1 x3 i- ]2 z' A3 y
      'post_type' => 'site-wide-notices',. ^: d0 E2 n: U9 g
      'posts_per_page' => 3,
7 q2 U8 C; G+ M      'order' => 'DESC',
6 K( e, r- z/ c( o- v5 [& l      'orderby' => 'date'3 p( R7 x7 a8 W& q) A# `
   );
3 G) g( P# g; _6 _  {   $query = new WP_Query($args);
7 }& s; x, o2 w   if ($query->have_posts()) :0 o$ L- g3 E: G
      while ($query->have_posts()) : $query->the_post(); ?>7 B2 U0 Z8 j2 t- k1 w
          <div class="notice">
! c# p# e; t/ K+ q' B' d, f              <h3><?php the_title(); ?></h3>, W/ Y  ~) Y1 Q0 V) `5 C2 J
              <div class="notice-content"><?php the_content(); ?></div>, U( m& c7 l/ m% N# i8 f6 I
          </div>
1 F$ K$ b, a7 B5 K% R2 W' }' c5 y) `, _      <?php endwhile;; @  J/ x; O2 x7 U; z! ]6 }% R
      wp_reset_postdata();
" U7 V5 F: Y: q% Y" I   endif;; }: p8 O/ G" c4 o* \: V
   ```2 d- G( c/ b8 C
7 W1 R# Z/ a: e0 u1 X" y+ E
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
欢迎定制:13928122889
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-2 06:53 , Processed in 0.012139 second(s), 3 queries , Redis On.

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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