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

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

[复制链接]

340

主题

526

回帖

3516

积分

管理员

积分
3516
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:' E! B6 l. z" e. U% M3 N9 e

3 O# r; m7 @' ?! ~1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。
6 s0 R6 G" z# f1 F$ c, q8 ]2 l0 }  u# |& j8 _  [* W
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
) W. K1 G6 F% F  j- P3 i7 K: U$ A7 Y& c- R6 C
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
2 j& o( m* k& h' H7 e
* ~- t+ F1 \$ B$ ~3 w4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。
+ V! S* b- N; h! P) V0 v; ?& E1 P7 ]) V. {" t
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

340

主题

526

回帖

3516

积分

管理员

积分
3516
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?+ @% ]* C6 g2 M! I( `  \+ q: y
! x/ }' H6 Z7 }- ~
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
: F* H. p3 q! G7 z- i# R7 D  c! \% ^: q* q9 k" O) O7 U
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:3 v% H2 D% b$ W, j$ F9 z

" c, j, y( T2 H8 W   ```
0 Z" Q+ Q4 |" O, q8 I7 L   add_menu_page(/ e$ f  s/ E: G4 n& E' ^
       '全站公告',
- _8 |4 z0 c' u6 J1 T" f  Y       '全站公告',
9 I7 O3 K9 b0 E       'manage_options', //需要的用户权限
4 G& e$ D- Z* S! |3 i" N       'site-wide-notice', //后台页面的 slug' Y- X2 e+ f: ^3 C) N* K6 s. i
       'render_site_wide_notice_page', //用于渲染后台页面内容的函数
/ s" K4 _* r7 |+ J       'dashicons-megaphone', //用于显示在菜单中的图标
; y' T* h) ^# E# T! h' h       30 //菜单项的位置
# N7 Z2 N; E  N   );/ |0 l  e% o) c  i, v; Z: W
   ```
/ G4 J/ v1 b6 y" a( S. A& A( k. X+ y# X  x+ p+ S- J3 N
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。0 `) N& y' q, z; v6 m" R; b# V* R

2 ]1 d/ N( I; E$ G2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
; Y: I% Q) ^( {: X3 G( r+ J8 O3 c, A2 {2 _7 c( S
   ```$ v& B$ O. K2 C2 I* p4 x1 ?7 i
   function render_site_wide_notice_page() {) P2 {% G/ }& D
      // 渲染后台页面内容7 l& e1 ^5 ]: L. M% |7 J8 a
      echo '<div class="wrap">
1 d% E0 c/ s9 e0 v% k          <h2>全站公告</h2>
' h7 N# M, {2 M8 r% v' ?          <form method="post" action="">
4 X8 h- v9 v+ {5 B9 x+ A              <label for="title">公告标题:</label>+ p! ]8 J& P  R! v
              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>$ n% |1 U9 _& \7 h5 y
              <label for="content">公告内容:</label>" l; i0 h6 s* U" c9 p5 g+ L8 Q7 R
              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>1 G2 l8 m% j. c" {1 f
              <input type="submit" name="submit" value="保存设置">
2 r: L& `6 c/ S' A          </form>. W: O( D! y- K! Y  Z9 _! m6 V8 t5 H
      </div>';; W6 b5 @# D3 R

& U# g, ^& {( L& W4 I      // 处理表单数据
9 f! C( K: W& z2 S# q      if (isset($_POST['submit'])) {7 |7 s( @1 f: _) w: S
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));7 A8 g, g& [, y  e" e9 J+ v
          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));
8 u$ W% ?: ^- a' c% w) A      }
* m- u* S+ s& x- `8 o' Y7 F   }
8 R+ `  w( q: s# i2 G0 G   ```
# R5 k! ^- A2 ?" l1 {# M" M; Y0 S# O7 a, U- t
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。) N# Y) z6 [5 U- R" F0 D
! k( M0 z; r5 h' f8 O2 U1 f
3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:
# A  l  i. H; B! ~6 o7 B# v
( e; E# C* u- @1 e; [% ~   ```
# r3 o( _! l' @   <?php1 C! b/ K1 p$ c6 p
   $site_wide_notice_title = get_option('site_wide_notice_title');
3 B/ g5 @( j; O   $site_wide_notice_content = get_option('site_wide_notice_content');
* V; T4 q# S, ^3 Q   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {. C& K6 Z! i& l; Q- k7 u
       echo '<div class="site-wide-notice">';
, N  R6 M0 r$ |+ ^       if (!empty($site_wide_notice_title)) {9 L( u5 R) R$ x! x9 K5 D6 u& E1 c% n
           echo '<h3>'.$site_wide_notice_title.'</h3>';4 G2 z& `4 E" N. V- x3 e
       }& [6 n+ D8 v+ {4 ]* v. T# b  D4 \
       if (!empty($site_wide_notice_content)) {
* [* O: }) |, T: E: M. F           echo '<p>'.$site_wide_notice_content.'</p>';
3 O  h- n+ q6 `- W       }# g, |8 x) ~, T  y3 Y
       echo '</div>';
: K& E( p+ D- Y; B8 J! M   }7 `) C' ^( z! R9 d6 L) q
   ?>
. J$ Q" p: p& U* G   ```/ m8 [4 R* S- ?* L8 m7 r, s8 `
0 q$ j3 S5 n# l3 \% n9 L
   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。$ ?8 O. z( X) I6 o6 i0 Y5 m9 Z& U
' @/ X4 l+ K. [' }. W" g1 U
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

340

主题

526

回帖

3516

积分

管理员

积分
3516
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?
/ j  W7 W! R, |$ H' Y! n1 Z# G# c: M$ X# w$ \: X
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:
8 ~# S0 l6 l' M5 h' ~* n2 @
, [0 ], G; V5 s% `6 |$ x0 R1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:2 \9 C1 u4 ^% U& E, j, D
, c  o( l7 G1 u& }9 s
```
( ]! M8 {: J- xfunction create_custom_post_type() {% n5 [% i& n4 A! |  a1 E# Z
    $args = array(9 ~) c3 E1 y. o# b9 @" e
        'labels' => array(
  c' u' y2 k9 B0 |% O1 t0 L7 ~            'name' => '公告',! Y+ X0 f5 w. H. D0 ~) H! l7 s) |
            'singular_name' => '公告'
4 n/ t- v( U! o0 n& M- ~        ),- M% j* |2 S8 T+ S: p5 @
        'public' => true,
8 b9 m1 h& \3 V- f* x* F9 b        'has_archive' => true,
" W, Y1 d5 G8 R6 f, b        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),* R+ p! H7 h% O
        'taxonomies' => array('category', 'post_tag'),
! Y+ J5 d# K) W  s        'menu_icon' => 'dashicons-megaphone',( e' C1 x, T7 a3 u- n/ T( x3 I* j
        'menu_position' => 5,
. f0 g; s; R: ?" l1 `& {- F        'rewrite' => array('slug' => 'site-wide-notices')
: [; I" E4 m$ `$ v* c; g) g    );% u: ]- L: y% z+ e& P- \* J
    register_post_type('site-wide-notices', $args);% w! m' Y( m2 U- P
}. u$ U7 |+ Y) T+ D2 e& g, k
add_action('init', 'create_custom_post_type');
: I% i! i5 z. p" g+ s% T```6 q& `* ~1 [' m8 E+ j) l
& F1 j& z+ S$ |
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
# J- Z- L6 G$ b6 r
6 @2 q6 T4 j2 ~" B2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
* j; t- Q7 c# E8 P! P1 i! L/ I  Z; N$ ~9 q
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:
8 u7 C6 W% U& [1 s+ Y" p& R& W( ]: [3 u5 [) ?  S/ o
```
0 g/ ]5 E# e# v2 R$args = array(
+ j! j- |6 n3 S4 H  i    'posts_per_page' => 3, // 显示的公告数量
4 L8 `- {9 j1 q2 ?5 j* h    'post_type' => 'site-wide-notices', // 公告文章类型2 r8 Q# `3 D9 G$ S) b7 @
    'post_status' => 'publish', // 公告状态
6 o0 z/ v0 ^1 S$ {& e# u( [    'order' => 'DESC' // 排序方式! l7 ]/ y. t3 G: }4 N9 `
);
8 r: M2 j9 k) N
5 i9 T4 B' A" i$notices = new WP_Query($args);) O6 X3 r$ g" L: E
if ($notices->have_posts()) :
) W2 t! P) J1 h# s. c4 A6 R    while ($notices->have_posts()) : $notices->the_post(); ?>
$ x+ \4 L# |& D* K1 K        <div class="notice">! Y, O* O8 A) }2 w# k! d! V. J! v( c
            <h3><?php the_title(); ?></h3>
: {0 ^: L$ g( C% D/ D+ k            <div class="notice-content"><?php the_content(); ?></div>
2 T% Z3 ?& I: D0 t- O        </div>6 B- e+ {& L7 x7 u- y
    <?php endwhile;5 D6 H& c! ~' M
else :1 |! O$ V* I6 s: S4 r* M6 ?; }
    echo "暂无公告";' E. Z9 P% q# v1 f3 r  k7 x! f' D
endif;
/ ?# \, f$ s# k3 awp_reset_query();% h" h$ a' l$ L! K0 ^* v% \; r
```' S4 j2 d9 _; d+ T7 _+ J4 D, O0 P2 p) J
  c9 f. d9 s) F/ ~' y# o  ]
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
) z5 y! \/ I# g! q. X3 \+ b/ q; O; v8 {+ B2 B5 P0 h
以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

340

主题

526

回帖

3516

积分

管理员

积分
3516
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?
" s# I7 E, f& x3 F' O: Q/ e
2 P( T! M5 F, Q; |: i- M如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。
  E7 m4 l3 M6 F* {/ H' E% R# G/ D( L  [. p7 V( v" r- R: s
以下是创建自定义插件的步骤:
2 I) w- h9 }0 X$ C3 {. H; @
( E8 r( j- \. ?6 g+ U1 k1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
; Z, a  s( o/ k+ t4 e& j, S1 `8 [9 {# \: [$ ~& D1 a
   ```
" J% y- @# p7 u& T, j. k   <?php
8 O) d- s+ x2 [& r4 O7 X   /** o/ J* I8 t4 z$ Q0 Z4 S8 d
   Plugin Name: Site Wide Notices Plugin3 X- g: O$ o( J
   Description: Adds a new custom post type for site-wide notices.
, c4 |2 R, R0 W$ e3 W+ c   Version: 1.0
4 v$ _( J  V/ Z: f6 W' @. Q+ J   Author: Your Name
) @5 @5 Z. l/ R4 }% G   Author URI: http://example.com
  M1 F( L  t' E7 X! \   */: H0 u' ~3 O. S1 B

7 M2 T+ |: f# `  A& x( ~# o# h   // Add plugin code here..., d" N8 T6 R% w3 w, O* [
   ```2 u3 l. v& K* ?6 i7 I6 z

" k% x9 _& m3 h9 s   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。" P2 ~  b5 g& M- o; P* m9 {/ T
: [; O8 c& a7 n/ K! L" R% }
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:( M4 W2 ?, d3 c/ s

6 j/ }3 `* a9 L5 o   ```1 H* u7 K# I3 L. o4 ]7 g
   add_action('init', 'create_custom_post_type');2 r( G6 J4 g' w( @- K
   function create_custom_post_type() {
' X, w( ]8 j  [      $labels = array(
% l% B& p5 E0 ~& O" ^          'name' => 'Site Wide Notices',: U0 }, J" L/ Y: {! A
          'singular_name' => 'Site Wide Notice',. E& S) ?5 ~" N3 \5 C/ V  L; G
          'add_new' => 'Add New',& R& W8 Y# X  U8 S- i, Z  C% F
          'add_new_item' => 'Add New Site Wide Notice',, V# G* @1 x. [9 \
          'edit_item' => 'Edit Site Wide Notice',
/ G. n( a9 Q2 ?6 Y1 i          'new_item' => 'New Site Wide Notice',4 N8 W5 q* }# t
          'view_item' => 'View Site Wide Notice',
5 F1 c3 m* A# Q: ]  ]          'search_items' => 'Search Site Wide Notices',2 b1 _+ s& ?% P) C6 k4 D, n" M0 q  Z
          'not_found' => 'No site-wide notices found',0 l) e/ F4 c# s/ ^
          'not_found_in_trash' => 'No site-wide notices found in trash'! }! }4 ^& F  X6 t
      );& T. j. d" i: W7 O/ u0 o
7 v1 g' J7 ]4 g  W+ B
      $args = array(: x) j* u/ N; ]# [! u
          'labels' => $labels,
% |% p3 c$ \3 e& K, s: a" p          'public' => true,
) M% [0 _5 |' F/ E8 i          'has_archive' => true,
! M! R/ p' m; b9 ?. R6 @; Q          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),7 o1 W, K+ v, w3 z: V
          'taxonomies' => array('category', 'post_tag'),
4 }) k' ~$ \' v# T          'menu_icon' => 'dashicons-megaphone',
/ M* M. X2 T9 b2 b. `          'menu_position' => 5,; u( H: L# X# S9 ]3 x! B; F
          'rewrite' => array('slug' => 'site-wide-notices')" ^' ^4 Y7 Z% F3 t
      );2 y" _$ Z; L+ S' \# o
" P  k* ?3 E" J( I, A" [: V
      register_post_type('site-wide-notices', $args);
! u" C9 r3 ]0 |4 N   }" q: L: v$ x) [" s# T
   ```
1 t# K" N0 p8 Z" y
9 _% d; x* \0 K+ W/ i( i   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。" N! D$ @9 A4 t! ~3 ]; R( a
. i- Y7 S$ ^6 d+ |& r
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:0 o, {" b2 I6 W' H, d7 G

6 t  E" P; ^8 ^$ Q9 M   ```0 [5 a* l4 V7 P7 \
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');
* j. i" B9 n  a, S# S( j3 A7 V   function add_site_wide_notices_boxes() {& n0 u8 O& |6 B$ q
      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
6 R( O9 e& j% b   }6 g) B5 o$ y7 Y9 @/ v3 N$ o: Z
, f1 X+ {3 c; D
   function notice_details_meta_box($post) {
3 J) |# n$ V! ~      wp_nonce_field(basename(__FILE__), 'notices_nonce');* L7 @1 N8 R! z; A, ?( q& J
      $notice_title = get_post_meta($post->ID, 'notice_title', true);# _# o4 o8 L) a$ L! T; ~" m7 h* h. z
      $notice_content = get_post_meta($post->ID, 'notice_content', true);- j7 c" I  q5 u6 y4 d& ~
      ?>6 q0 F5 N& X  n3 ]5 Y
      <p>3 m: ?. J% w  ~, w
          <label for="notice-title">Notice Title</label><br># q4 S" L' k8 k
          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">; ]/ }, @& }( S  |' G
      </p>
" A7 F# E# \  }1 y5 t2 K      <p>& f8 E. ]' r; q
          <label for="notice-content">Notice Content</label><br>; A7 t  C* O, W# n
          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
- s  `& N; q! v8 [& w7 F+ C- r6 `) N      </p>7 ?1 c8 A& e! b; k1 H8 M6 ~
      <?php
( Y. q4 M2 O0 Y' X, {. b   }
( z) M1 d) Q) q3 g4 Y$ G! f. V
( v  ?/ G+ d! I, ]8 [; i   add_action('save_post', 'save_site_wide_notice_meta_box');
3 [* v5 }/ H; h6 \   function save_site_wide_notice_meta_box($post_id) {& C& s. u5 x2 p( i8 H% {2 V) O
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))" B8 Y. a- w  d( x4 M9 N
         return;5 J8 R* W2 M. E; s
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
' c, @  I. I1 v% H8 D         return;2 _3 q! u# V/ w, F
. [8 l) }) e% P, J& s
      if (isset($_POST['notice_title'])) {
; _" B/ Z6 f1 j7 E# E6 G: [          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));
. q1 u( B6 c6 }      }
5 L/ L5 Y8 e( Q9 @      if (isset($_POST['notice_content'])) {5 y% L# A% O; I) h  q" d
          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));
% p( V6 f9 O1 ]! o, ~! o- P      }
9 i( L! k3 `; a6 i0 B$ r) Z) n$ E   }
) z. J2 Q& K, t   ```' Z( X) e, }1 ~# l# \9 U0 e: n9 j
+ z5 K6 a' h/ Z- w: v" ?$ H* v
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。1 s- h, Z2 y. a; h, y; e

4 u' q5 }1 |5 @, [; l1 }* J4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:7 Q( X3 ~2 G) p; n3 L
" {1 l5 L$ o# d/ ?% q
   ```( S% S1 q5 r3 W8 I) W
   $args = array(7 W/ R& O# {' r) u# p) j
      'post_type' => 'site-wide-notices',+ m3 F& T6 {0 z; f5 J1 \& U7 g
      'posts_per_page' => 3,1 k4 X& ^, `; i; B
      'order' => 'DESC',
: x8 S: R1 M  x$ y      'orderby' => 'date'
0 `; R+ u4 \9 b/ R   );
/ K- ]# _5 a* y. ~, r- O5 R' \# `   $query = new WP_Query($args);$ a( O* V$ V% U( t
   if ($query->have_posts()) :
- x* U3 U# z' h, o+ d- E( T/ g$ c8 A      while ($query->have_posts()) : $query->the_post(); ?>& i. W9 e% d# }9 `, L! }
          <div class="notice">2 Q* c) D- g& m/ Z
              <h3><?php the_title(); ?></h3>; G% m! y2 [# H
              <div class="notice-content"><?php the_content(); ?></div>
1 c  }/ a2 J4 \          </div>7 t' X: i+ x. p- G+ p
      <?php endwhile;) X0 Z* V9 v6 F
      wp_reset_postdata();
7 q+ b+ v) }# E   endif;
) D/ C# N  D5 q1 u) W) i, J   ```. g+ N/ {. o4 r
) L/ z" x+ n( A  t$ I* M6 J% }
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-16 00:11 , Processed in 0.024176 second(s), 2 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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