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

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

[复制链接]

340

主题

522

回帖

3498

积分

管理员

积分
3498
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:& a. S: [* z6 t" x

( ^6 W, I; ?! U" O7 N, z/ J1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。
$ }" S6 |8 l9 }
+ @" d  Y8 i7 a2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。4 m& c! O- w1 w1 A: u

) J7 v  ~5 O$ n7 Y7 ^4 b# e* }3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
) u) U3 e& {9 C: t( k' Q6 v2 `
; Y7 g6 M2 D, T1 d2 Q  y- |4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。
) V9 h2 }1 S. Q& [- \
# ^! v$ z# c* t* I注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

340

主题

522

回帖

3498

积分

管理员

积分
3498
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
& z5 B" O7 H: s0 u) n
7 f+ o0 L/ }& p: o% q/ }如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
$ E# v* \& V& {
9 D2 \6 e# h2 s2 n* m- j1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:
5 j) c% ~; V2 j" q# M* K0 r' X+ s/ c2 u
   ```2 C, i" S' I8 a4 r5 O- |$ \; ?
   add_menu_page(
. x; k& Y" k$ W$ H- @! A; U       '全站公告',
1 n5 C/ g7 q' G' C       '全站公告',: n2 a; }$ b# U) `) l5 }
       'manage_options', //需要的用户权限
8 n% O& r2 ]. _       'site-wide-notice', //后台页面的 slug
8 I+ G. ?- Q  ^! \5 H       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
/ D. X! i8 u- C- ]$ w       'dashicons-megaphone', //用于显示在菜单中的图标* S3 Q$ b; \2 w$ v
       30 //菜单项的位置
4 l7 Q* _" g  _6 S   );
# D5 }  H9 x4 h/ w" a6 z+ N   ```" l! K: T$ x, R) w+ D, O

+ y8 G: \% K3 z   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。7 o! j6 ~7 b$ ^+ ~8 u

" w% B- w/ \2 q, b" g5 Q5 A3 M2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:& E# P# O: ]6 [$ t+ E% J

: R/ Y! C4 c1 w$ C( r' ~$ J. L' g3 E   ```
: W( T1 Y! n* Z  v& L- T* H   function render_site_wide_notice_page() {
2 R3 e5 C. j- I2 Z8 a, Q      // 渲染后台页面内容! m# A! X# J, d( [4 b
      echo '<div class="wrap">  d! t/ w2 S' Y! E3 E* \! M! p8 k
          <h2>全站公告</h2>
: a0 u1 v) B  n# ^( X* z          <form method="post" action="">
1 _2 v$ G* V$ v. [) q; F              <label for="title">公告标题:</label>
3 v: B6 D1 [" p1 B              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
& `1 `+ M. u. @5 w+ y              <label for="content">公告内容:</label>
; z3 Z! {2 X; M9 f+ H0 V              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
  d8 d/ h, P# z" z4 j              <input type="submit" name="submit" value="保存设置">
: u! `1 b$ o+ Z6 M. C# y$ o          </form>5 q+ K# r# {2 ?; e& G
      </div>';
( J! f" d: G! B0 B7 u6 [5 r) O" T+ Q* `0 I
      // 处理表单数据
0 W5 P* Z9 C' k- \, ^2 L5 O( r      if (isset($_POST['submit'])) {
/ w$ \1 B7 c4 z% D% T& L8 i          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
* ]+ I  m6 H/ p+ l0 m" M2 S          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));6 c/ E9 q  m4 r. Q
      }
8 `& K5 D. Y+ {9 F7 O5 Y7 u* J   }, ^8 y( @/ Y, f2 C
   ```& P, O& H7 y' C' M1 @. U

( B+ t9 K* n( J   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。
$ o8 ?6 E) r+ K5 |3 l
5 V4 Z) r9 D8 a! H9 Y: m! G3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:* G$ X5 V: |( d7 @/ V! g+ n

' G. l; h1 u" _3 u7 R   ```3 [8 M5 T  t  E: w9 {9 H1 ]1 [
   <?php
; ~! ]! {8 G5 Q   $site_wide_notice_title = get_option('site_wide_notice_title');, ]$ _8 ^/ H( b/ O4 x# R/ {, |
   $site_wide_notice_content = get_option('site_wide_notice_content');
; v. ]) g  C+ H6 E/ S" }# U. ?" G4 O   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {
8 d0 `4 r  ~# d4 e       echo '<div class="site-wide-notice">';# H0 D( W8 l! J& ~* G
       if (!empty($site_wide_notice_title)) {5 I- z. s9 ^& {$ y: I6 a6 K
           echo '<h3>'.$site_wide_notice_title.'</h3>';  c/ ]' ~: J1 T  Q' Q6 q
       }9 u! d  L1 B! |' l
       if (!empty($site_wide_notice_content)) {
. \2 l% j0 }* r) x           echo '<p>'.$site_wide_notice_content.'</p>';
3 }! b  D4 S! R' p5 ~( L9 P       }: e2 F1 b( P6 I- n! n
       echo '</div>';
( A4 \6 B) z/ t' N* h( ^4 N6 t   }( U) R  Q% [* I3 O, P
   ?>, f% L3 o( ?# ]
   ```
# ]! a& f3 w! W9 r. h) I7 X
5 ?7 A0 G5 E2 n. {  \- I   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。( T7 @9 E2 D  H" z4 G

& [, |$ \# o! W/ o# T6 T) V请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

340

主题

522

回帖

3498

积分

管理员

积分
3498
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?
+ o( z$ O# K' d7 j1 o  y* l1 P4 a& ~1 }0 n$ A% |, D, ]
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:5 B! f" a6 a+ _& I- B
$ w( t/ A3 m; f, D
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:
  y4 c) I- D; h+ ]# i# j9 ^  f; Q: i  `& q4 v
```0 E) d+ [) p4 y. s8 T+ ~
function create_custom_post_type() {
$ S) X# [7 I9 z    $args = array(
4 `/ C: O& Z. g) C* l$ n        'labels' => array(" P, N' Y5 v  y
            'name' => '公告',
8 ]4 i- M- E; Y' }. Q: F; w% R- J            'singular_name' => '公告', y$ \( J! L. }: Y8 Y
        ),
' y, X+ k1 j. M+ W8 \0 ]; d8 M        'public' => true,
3 U. {2 B( j) }4 a6 F9 z9 D" Q        'has_archive' => true,
8 f8 i+ `4 ?$ b  u4 ?* W        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),  P2 A1 l% Y7 m, `+ l- h+ k
        'taxonomies' => array('category', 'post_tag'),
, F2 E$ c7 \: F  ?: f8 T# w9 [$ P$ _        'menu_icon' => 'dashicons-megaphone',* l& I- J$ {# T1 r7 F; v7 x' C
        'menu_position' => 5,' f5 _+ X" e: G( w3 L2 h, _+ F
        'rewrite' => array('slug' => 'site-wide-notices')
5 G; M9 [' ]! q! d# `    );/ ^$ ]4 o7 T9 p6 ~" H& n
    register_post_type('site-wide-notices', $args);) m, B, l* |- [' \
}. I) \; @% E: q
add_action('init', 'create_custom_post_type');
, |+ @- h3 {$ h$ V- G' t! O```
7 m/ {2 {" n: q6 u! _& |
6 z! o( M: }8 Z7 n/ c7 n8 J% K在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。9 Z0 i* y$ ^$ Q

/ c5 u  N, x. I$ c2 ?. r2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。3 a# K" L# e) W# P
, {/ _1 ?( C+ k
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:$ @/ A4 i0 z. K7 O

' z$ Q+ E$ @$ M3 m0 U```
/ b+ }" [2 k4 V! D  j& e$args = array(
9 r. ^5 j: S* n: `" L: x  c    'posts_per_page' => 3, // 显示的公告数量
$ C- k& ~  a: Z2 u: y    'post_type' => 'site-wide-notices', // 公告文章类型
& p: P  _# K: t/ r8 y    'post_status' => 'publish', // 公告状态2 x. c) `% C! k
    'order' => 'DESC' // 排序方式7 U  L' l% a: \6 l: j
);4 v3 a7 b: B3 d/ j/ V
$ P; U: d  Q+ K3 k7 I; j$ [
$notices = new WP_Query($args);
8 g8 ~* a2 }2 `if ($notices->have_posts()) :- z: |4 y$ z% y2 t
    while ($notices->have_posts()) : $notices->the_post(); ?>$ ]( Z6 J) H2 z0 P" f( D4 g3 M
        <div class="notice">* P: K8 ?4 h5 b+ _! g2 X/ K
            <h3><?php the_title(); ?></h3>  y4 B& H$ v6 G3 b( A
            <div class="notice-content"><?php the_content(); ?></div>& w0 m( \* C: p0 k
        </div>, g* q4 {+ Z" N
    <?php endwhile;
* S6 V  O# e) k0 s0 D/ ^7 j) i5 felse :
9 \2 c0 i' a" P# p$ f    echo "暂无公告";
& b/ D- m" ?( G8 Zendif;; [. p) y$ a- v- n; H4 h8 ]
wp_reset_query();
* S4 h4 A. \; u( q```
0 U) _- W. b& {& i' c6 w1 ?4 v# V) G8 v
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。2 j8 |" w9 w, j7 C

6 B. J% v3 O& H9 A7 y/ a$ }以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

340

主题

522

回帖

3498

积分

管理员

积分
3498
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?  J3 t) F, }, n% E! F3 v
8 h# M( C4 O6 k( a
如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
, r( ^* o" I) G; X: J6 n
" }) r4 D- Z& D1 I) n6 g# Q以下是创建自定义插件的步骤:
) i( R2 ?2 G. G6 \$ j" n% @  [5 J3 C
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
( r4 c( Y# {4 B: Y9 N. v2 K" D4 c* A4 v" _- ^5 K/ d  i
   ```
6 e: t8 P/ K" E6 _   <?php
. s2 U9 k4 h* F* v& X   /*+ u* D, q$ W( L8 p: e: T) k
   Plugin Name: Site Wide Notices Plugin
! o( d- K4 m( ~" O, d   Description: Adds a new custom post type for site-wide notices.
' U" x; m) q) ]6 i) C+ X( P2 c   Version: 1.0$ q  {& Z; b' Y$ U: B; I& B2 ^
   Author: Your Name
9 j3 i3 S0 s( |& u6 C& K   Author URI: http://example.com
/ z& m$ ~7 E7 O- l8 W% x: x, m( I3 X2 L   */
) g  U0 i( h) l$ E$ L! K$ h5 q  b) Y$ b2 \5 v- p* a5 n) _
   // Add plugin code here...
: N$ t- l( J) i# l( N   ```" R- w$ a% ^( H" Y/ X0 {7 d7 \
- Q. t5 X$ w; c4 t; f0 M* n0 y
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。- u) v6 V& U& J) B8 }& G

3 [/ t# M7 [& z& D1 W' ^1 M. `+ A" a- [2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:
) G- o6 g" R. G4 A+ e9 Z; X4 Z0 C3 ?
   ```
1 F- v; m: V0 I; V" {! r   add_action('init', 'create_custom_post_type');
( T. j/ \/ Q, |( n- K  A   function create_custom_post_type() {
0 A+ G4 i: p' {( ]! L      $labels = array(
- O% c8 `3 S" E" X& R( C6 N4 K% j" F- h          'name' => 'Site Wide Notices',/ J7 V* T/ ]) O& H* q! x# U9 u
          'singular_name' => 'Site Wide Notice',
3 }7 s& R  `0 z3 C8 N          'add_new' => 'Add New',, q* T& \0 q) H! v6 S0 s5 ?
          'add_new_item' => 'Add New Site Wide Notice',3 u0 m% q# [. Z  l
          'edit_item' => 'Edit Site Wide Notice',
  g% v/ K3 f$ E% p          'new_item' => 'New Site Wide Notice',
, j# p! i& h& S" n# ?% u( z          'view_item' => 'View Site Wide Notice',
8 P* o0 s0 J' M# F  B5 I0 G4 h          'search_items' => 'Search Site Wide Notices',
; p. `! Z+ _8 s5 m          'not_found' => 'No site-wide notices found',2 N  M! k7 X# m0 R
          'not_found_in_trash' => 'No site-wide notices found in trash'( b1 m) ]2 x! F( A8 y& W. G
      );
% H" _# q  X( T% c$ p' f; b! z" q; g5 H; P: O  V
      $args = array(0 r+ a; |" q  U. ^# n0 C; ~& p
          'labels' => $labels,: l" w6 r7 X3 s( T
          'public' => true,* h$ H/ e  P3 R+ l: B  `' [) y: Q
          'has_archive' => true,* u9 C& M8 _6 C, L, Y* x8 j
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),& z) g3 R7 Y) q9 c! \5 Q
          'taxonomies' => array('category', 'post_tag'),* M+ ?5 n  e. I/ m+ I. `
          'menu_icon' => 'dashicons-megaphone',) P- |: ]7 I, ]9 U6 v) B$ h
          'menu_position' => 5,
% r2 a$ B/ E+ U! Q4 W3 v9 b7 I" U          'rewrite' => array('slug' => 'site-wide-notices')
+ D) O# e: C$ u! I% }; r      );  h& g% r5 z" A
7 f2 ^; C0 R+ Q% Y' M  m
      register_post_type('site-wide-notices', $args);
0 p. k% r! I! ]) q: x) t" ^   }; [" J$ l- s" T" U
   ```
3 H- |% w" q! l: d+ B
# S" ~* C- _) k3 D9 H. ~! {+ [   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
+ P+ h& D" z8 h# B+ J* u8 ]0 @4 a0 _/ ?& N& J
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:
* H  n/ j+ w% c
5 s8 [( _8 o% W' Z   ```- `8 e$ n3 m6 E- c/ r+ W
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
9 X8 l& L! O# N/ @* O4 ~   function add_site_wide_notices_boxes() {! A9 Y9 V4 c( a! A( ~) X7 I0 o) ^
      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');2 O) G% W$ U' p% o. L+ ^
   }
2 [3 i2 O$ m- i6 C) E  f6 s; k5 l5 U6 }# t' \3 `, c7 W, p7 f
   function notice_details_meta_box($post) {, e* G' c7 R0 i+ y$ g' b
      wp_nonce_field(basename(__FILE__), 'notices_nonce');8 p- s1 z7 b) w2 V  H$ x
      $notice_title = get_post_meta($post->ID, 'notice_title', true);
# k0 x, g% p3 r      $notice_content = get_post_meta($post->ID, 'notice_content', true);
# Y! h; {- i3 z6 R+ o' W      ?>) q2 W( M- G: E$ y  P' r
      <p>
6 L, T( ^' e* v! C          <label for="notice-title">Notice Title</label><br>
# I$ Z0 C2 Q* G0 n' P& Z+ t          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
8 J# A' f1 m& W. p      </p>9 m0 P& @) [* L
      <p>
% _" R% ]- i4 h7 P4 e          <label for="notice-content">Notice Content</label><br>; C7 t" P4 O. F+ q
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>! D9 a8 \1 z( i$ M; p' J
      </p>
' D1 T7 `8 P: z* U      <?php3 C& ^2 E" T4 Y" H& j! B% J- J+ w
   }
1 j8 l8 C0 G$ t* S: O, n
, y9 g9 l+ D7 k6 h* ?3 j   add_action('save_post', 'save_site_wide_notice_meta_box');3 }0 j1 Z3 ?8 F0 k: Q+ _3 `
   function save_site_wide_notice_meta_box($post_id) {
) w/ W5 \0 `1 [  N, a# X2 o" e      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))' K  o$ j  K! C' D5 e4 e
         return;5 I+ z/ Q5 {% i% ?, ^  Y
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)2 o- {4 T2 p5 ^$ A+ A
         return;
" l: _# G' Y* n+ g( Y6 Y8 H& Q# [- m# U6 |2 C
      if (isset($_POST['notice_title'])) {
, I+ l. @7 Z. f/ U# H          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
5 B3 v4 A7 R$ F9 P      }0 x% r5 x5 X4 J" g6 y( Z0 C
      if (isset($_POST['notice_content'])) {
( `9 W- k1 J6 [          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
7 I3 S- D4 R6 }      }# u/ `7 J: P0 k) n" w
   }7 m. S7 ?! o8 {' N# M% c
   ```
3 R! f9 S  H  p& K9 \  [- H! ]% V9 l# z4 |5 e% N
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 c9 _# l  ]6 S0 p+ Z
' @' G& t+ K2 _2 l' j& A' a
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:) S: r% V/ V, _' ^& C

- ]6 c2 S. K, w' o, }& S# H   ```; Z4 y0 a* s* e, y$ u" F
   $args = array(: ?' }$ c- Y# h2 S5 r
      'post_type' => 'site-wide-notices',8 `' c2 d0 Z- e& k
      'posts_per_page' => 3,/ R5 V6 a( b4 U; m
      'order' => 'DESC',- o; a' t2 V2 F& V3 o; V
      'orderby' => 'date': ~% b% G8 ?( R1 v  j0 T- u
   );6 Z6 X1 F2 \* ]% X2 O) P
   $query = new WP_Query($args);
, m) \* n9 F7 Y8 I+ v9 R   if ($query->have_posts()) :0 F  I  W& G' j# r
      while ($query->have_posts()) : $query->the_post(); ?>
6 z- N% ]: s: ~) ~! F          <div class="notice">3 j! {; K! |' C1 i  M7 a- P
              <h3><?php the_title(); ?></h3>3 T5 J& Z# r- ~& Z
              <div class="notice-content"><?php the_content(); ?></div>& ~, K  l- C  ]$ ~/ Y, N
          </div>
- j+ L1 r% ~7 r, W      <?php endwhile;2 e8 E4 S3 |3 i3 f+ S/ o
      wp_reset_postdata();1 U0 D- F5 V9 D* l" l! Q
   endif;
( p+ g  u3 J9 w. B5 C   ```* y+ ^0 U2 V3 H  K4 I- I3 n' ?8 T
; m1 c6 V3 b! Z; l* a2 L) \) |1 J
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-13 20:12 , Processed in 0.022244 second(s), 2 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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