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

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

[复制链接]

338

主题

521

回帖

3480

积分

管理员

积分
3480
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:
6 h( L/ ?9 [/ D+ A
3 V1 t8 R) Z9 o# E2 c. o( J8 k1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。' E2 m0 _) T# Q2 t& t$ L0 J
# S+ }; ~3 B" Z! I7 X$ Z
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。- G# v( k+ X& b2 U3 F, }& f4 Z* Y
+ F) q3 B# [; k
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。/ r" z- B' w+ z  i3 S

2 B# I/ E9 ^; C5 q4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。; z1 Y9 H# j5 }

& F3 B; h8 K2 `/ s' D. g# |9 w. y注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

338

主题

521

回帖

3480

积分

管理员

积分
3480
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
0 x: D- T  Z( Q$ _/ a( ]" q7 E5 C/ o$ C& C7 |& N; k
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:
4 M) D6 A4 i: `# D9 n+ ?% s/ a) _+ [
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:8 \' ?$ O# [* s

; l0 @) d5 m7 l' X5 f   ```- o: D+ a; h# m4 }/ V" h) X
   add_menu_page(  Y9 V: s8 @4 i* V/ I2 r4 H
       '全站公告',- n1 m6 n0 l$ m* u! X8 [& v3 U
       '全站公告',
, j- U9 P. L8 [; U) l: o+ g! S       'manage_options', //需要的用户权限, u, e0 U1 ~/ ]2 i
       'site-wide-notice', //后台页面的 slug
4 v: ^8 \$ m3 B% }7 v; h       'render_site_wide_notice_page', //用于渲染后台页面内容的函数, i8 {; m- [; T
       'dashicons-megaphone', //用于显示在菜单中的图标: t% {' r3 x: V. b  o
       30 //菜单项的位置, C$ J& H" i  S
   );
$ h3 g; i5 C9 P6 @! N3 x7 E   ```
( E' B; I0 c# g5 Z7 d: P( t# J: M1 q
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。+ q' Z! Y; ]# }; l* k

+ |  `6 g# ]" x) D) x! c2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
7 |3 n6 u5 w' z# r
9 l( u+ F1 S% f% {   ```2 m" p3 \: S, B* M0 L, D) F
   function render_site_wide_notice_page() {
  A! X# A! p8 t# K0 b: y) R. \# d6 g      // 渲染后台页面内容) n5 d. n1 f8 j) }
      echo '<div class="wrap">0 x) K2 M1 \) C
          <h2>全站公告</h2>
' V0 |# E' G9 @) ]$ v+ z2 N          <form method="post" action="">8 w3 w5 z. R$ @' ~
              <label for="title">公告标题:</label>+ ?7 A% g2 h1 q1 K2 e* E. _
              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
% M7 l( @; P6 H% `0 l* W* l8 G" s              <label for="content">公告内容:</label>
4 ^8 x( D! v3 Y- q8 @" S1 V              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
- b& V/ b! e" ]* b6 a; B              <input type="submit" name="submit" value="保存设置">
8 ^2 p3 _; m: w2 L! u; \          </form>
  W  q5 {8 s; c4 t/ q" n1 u4 h      </div>';
' U) S, _9 L" k% N% F- K3 v$ F6 M" l1 k, N' j; X2 E& m
      // 处理表单数据
; G& y  @/ g7 v3 d) p7 e      if (isset($_POST['submit'])) {
  W2 b1 e2 Y: b- G          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
0 G) Q$ ~1 b' X( ~6 N3 X7 T8 s          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));
. N7 u8 J' F; E      }
9 C3 u1 q9 \3 z* J( M6 i   }
* n7 C& g8 j# E4 T! `   ```0 l4 U9 `: {& r$ v; F

( {2 v; q6 S+ y" b/ V   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。, l0 E' V4 Q/ ^. ^3 H. s

( @3 l8 H4 N  u2 Z# y+ [3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:& q2 h+ n' U- c0 p
3 D7 R/ j/ l; Z% [. L; W, {! m
   ```
8 Y( T, n" e) w% g: Z9 C, F$ c   <?php0 F1 l( B+ U7 a1 e
   $site_wide_notice_title = get_option('site_wide_notice_title');
/ F, l- S3 I" N   $site_wide_notice_content = get_option('site_wide_notice_content');3 A+ E8 [6 [" K' v% L
   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {0 @- E0 V; i9 ?0 {. l
       echo '<div class="site-wide-notice">';8 C/ y  {5 V( b4 E
       if (!empty($site_wide_notice_title)) {, O1 I! G/ D' \! X: @  J9 B0 L5 C: ]
           echo '<h3>'.$site_wide_notice_title.'</h3>';
8 y, C# U! E# s+ g       }
: X5 l' v% H+ P+ [7 W+ a       if (!empty($site_wide_notice_content)) {
, l5 B( ~3 w! T7 f% d& j           echo '<p>'.$site_wide_notice_content.'</p>';
& _& F8 U9 X* a7 s' U       }
" x& O& C3 l5 `; w5 t2 Q) Z       echo '</div>';$ l% X% Z) `( L  {# M5 Y
   }, y& U8 b  Y9 w) s1 ~- d4 [$ x
   ?>% ~8 N2 F; `) r( C, ~/ |
   ```( ^+ t5 |& j3 W3 t

& ]8 T( n+ x5 C/ K7 z  T. D3 X   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。9 @4 _9 @: w' J6 n- Q1 ]3 w+ r6 U
' h$ G  J% H+ r  {$ o# O
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

338

主题

521

回帖

3480

积分

管理员

积分
3480
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?2 ^) Z, K( Y0 M* |& Y+ v3 y
+ y3 q9 u' n6 T( s7 g' f+ b
您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:+ e" l) R1 r, a) e/ O' L
7 l$ v; C7 a# t. }. t
1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:/ [2 S) d/ j0 b! h* T5 p* c2 f/ G9 `

( _# l7 I0 k9 d; Q5 D/ M7 M  `( u' J```
" a1 n2 Y+ Z$ s' ?function create_custom_post_type() {+ Y" G- a& r  c7 R! R
    $args = array(
% ^; p# e  F. M7 @3 H        'labels' => array(
, T" C0 D6 Z- D! u5 O            'name' => '公告',
  Z7 Z; V4 J2 G5 G1 k* z            'singular_name' => '公告'& ~7 V7 ]4 I- w  ]! V
        ),
! N$ I9 T3 n, A2 P- n        'public' => true,3 d. F7 ?+ j0 N' M; L$ n
        'has_archive' => true,! e* y0 ^, P! N' E; d: W
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
! P9 `5 b/ J: ~7 J1 z        'taxonomies' => array('category', 'post_tag'),
1 L, m3 j4 A- ^; g        'menu_icon' => 'dashicons-megaphone',
- E, i, [! R& Q" ~% V/ P1 a! W1 {: z        'menu_position' => 5,. G. D: A' H4 I
        'rewrite' => array('slug' => 'site-wide-notices')- ]' s4 ^: m( s: H) D9 R
    );
- c1 d, C! F: ^    register_post_type('site-wide-notices', $args);0 Y  C6 C- E$ v4 {2 }7 A
}
- G) A* b" Z4 \* ~add_action('init', 'create_custom_post_type');7 e+ J/ c1 `2 M1 Z$ N0 c: I7 K3 @/ S$ F
```; B, m, p: v$ Q8 L  F
6 a; L" v( }3 `
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。
. S( n- `; f6 O& U" H) C3 h' f) h+ p7 T' n; C* _  ~8 s
2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。- A; B9 @  d; I6 u/ a
  y4 C- O0 `" ?  R( f
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:$ y8 j& N9 `+ b. N( Z

1 Q: X9 a8 n+ {' @* S- x```
  Z, q! S9 V, C7 t( N$args = array(2 U: Z( w% n6 {) N/ j0 r
    'posts_per_page' => 3, // 显示的公告数量  P3 D" }) G. J' i
    'post_type' => 'site-wide-notices', // 公告文章类型
3 a# t( h( V, \5 v7 z    'post_status' => 'publish', // 公告状态
8 |, p; W- J) ?! u    'order' => 'DESC' // 排序方式
' d7 [& m3 ~% I' h! ~5 y);% m4 ?0 S! y- b( C( G: W

' E+ v; r5 q5 I" s$notices = new WP_Query($args);1 A  v7 s+ c, e/ S# W& i& v; o
if ($notices->have_posts()) :
; c4 `$ g- k8 D    while ($notices->have_posts()) : $notices->the_post(); ?>  ^1 a. e6 t4 L( D6 T" O* T0 O. {0 }
        <div class="notice">
! o9 v: c) C7 b            <h3><?php the_title(); ?></h3>5 e7 b5 l9 P8 I$ y0 i
            <div class="notice-content"><?php the_content(); ?></div>
  [( l' u8 p/ Z  M7 }        </div>. b+ F' h* A  L3 B; U' ?3 j( D
    <?php endwhile;
7 w, [7 J( K+ a/ g1 Q3 W- f5 Belse :9 |  _3 Q6 D. C) h! c4 y4 B
    echo "暂无公告";
3 S) X8 J0 u9 R1 Y" a) e. Rendif;2 E7 x1 B/ x$ @/ W, s3 I+ x7 l+ I
wp_reset_query();
- M1 w+ p( T8 _5 _$ M+ A```
# e, R6 u" F0 J4 p: _) V
0 R6 {& y/ @% d- g0 @$ {这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。
& L. F# X. Y5 R" E/ \/ S8 S- }
, ?" [$ G5 Q" Z) Q& M' O) Q6 i4 C以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

338

主题

521

回帖

3480

积分

管理员

积分
3480
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?5 H, u# O, t/ B: `

0 I. c. k! F7 K6 K如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。; w$ A, @: Z' g# }8 N
' x+ X" |/ M( S) C+ V/ r
以下是创建自定义插件的步骤:
  g  X; _9 \% o% ?% ^* b* D9 t) ?- T( d* L) S6 g
1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:1 ^, A4 y7 Q- g  g* s  a% [0 W9 `
( f# @4 ~8 c) m
   ```
8 _5 x' K8 |3 U# {3 w( V   <?php- J; Q& R" C9 x2 l% j
   /*" H  C( ?: N0 [. _% j* i
   Plugin Name: Site Wide Notices Plugin
2 ^, Y5 H/ l+ p/ w1 i3 ~! D   Description: Adds a new custom post type for site-wide notices.
6 R* c3 |  j# Z$ v   Version: 1.0
% P/ _) J% Z; O1 V+ \7 V   Author: Your Name# i: l5 U: J# P5 e- {2 H
   Author URI: http://example.com8 x5 p! H+ u% S5 @
   */1 f. K; l* P5 A5 j
& |4 ]1 o3 O& f+ J. O
   // Add plugin code here...+ I9 l1 p0 }  d3 B
   ```+ H: `$ M$ e# x% k

& E' z7 j& r/ N+ ^8 _   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。& ~  ~% w% A4 L* h! z. l
3 M, \) a. p8 G  Y4 n7 |
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:7 a) e3 V* x8 U! T4 h8 N/ H

$ N" z' S% r/ k: L1 E  M/ L   ```
7 }/ M3 h: T- s; C   add_action('init', 'create_custom_post_type');$ h7 l8 g6 @* Y. b
   function create_custom_post_type() {
; B2 {6 y6 `2 m( l- _, P4 ~      $labels = array(7 }: Q( T) o1 ?
          'name' => 'Site Wide Notices',2 e* K7 R' W( v+ i0 R4 H5 }- r" b
          'singular_name' => 'Site Wide Notice',
5 \8 ?3 G% {7 v" U          'add_new' => 'Add New',
5 {, |9 G# `+ q, p; c          'add_new_item' => 'Add New Site Wide Notice',+ }- o) J$ G3 F, K( B' M
          'edit_item' => 'Edit Site Wide Notice',' s# E8 r5 |$ o
          'new_item' => 'New Site Wide Notice',
* p+ T0 u7 r7 O' C3 o. `( u* I          'view_item' => 'View Site Wide Notice',
" v& x% M7 N9 k6 O8 q          'search_items' => 'Search Site Wide Notices',
" _. _( T' ]( g% H9 d. C          'not_found' => 'No site-wide notices found',
$ u+ t, g% ~: _+ P  o* U% j- _/ p          'not_found_in_trash' => 'No site-wide notices found in trash'
& e3 H7 G$ C" \# B      );
) z: [' U. p7 m. ^7 Y) L* v/ ?2 ]1 f# j; H3 O
      $args = array(
' c, |: j! u" d          'labels' => $labels,
) h$ n$ X. T, ~6 \          'public' => true,
6 `# g& R6 }1 e& Y9 A2 E7 p6 o6 X          'has_archive' => true,' k. p% m, l) s, R1 H9 c! ?: L
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),( L* l' ~" v1 Z7 I6 W3 W' c# u
          'taxonomies' => array('category', 'post_tag'),: _* A. E* W# }2 a! ~: O! D
          'menu_icon' => 'dashicons-megaphone',
- \5 f& k' N- [& B7 w          'menu_position' => 5,
' Z. R5 ^9 v1 ^4 r+ V8 ^& B2 w* Z          'rewrite' => array('slug' => 'site-wide-notices')- U; U* |9 ~2 g: I  }# I
      );0 P1 G8 E3 d  y  u# q0 V6 W: ^+ y

/ v) i, Z1 B" |/ _/ p      register_post_type('site-wide-notices', $args);
" K' @' D, J4 O. K   }8 M; U4 g2 v7 _! ]  c1 K
   ```# h, }  i9 Q* {$ r# ?! E+ c" s9 V
5 E2 l! x+ w$ `  X7 T
   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。% ~" O. q: d2 A, L# S, y6 o
8 z5 r; l) I9 a
3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:' I6 ]9 D! w7 a

% G3 }, l/ M3 c4 L+ P1 W( a   ```* w4 L2 ~' w+ I* t; [( g
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');! t, l" t+ q" w  ^' j) }. g# u4 h
   function add_site_wide_notices_boxes() {
1 Y  D, G4 ?" f      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');
# K+ U+ \0 f9 V9 N# G4 `7 v5 h5 N   }+ }1 V, E* [- |* P
1 \: A0 X8 W' J/ x& t6 r; ^
   function notice_details_meta_box($post) {
) m8 b3 d( r, R4 J      wp_nonce_field(basename(__FILE__), 'notices_nonce');
' _" ]* M- c7 Q% f2 v* s6 W$ a& d# }      $notice_title = get_post_meta($post->ID, 'notice_title', true);+ R6 f' b) V' @  o( c2 _
      $notice_content = get_post_meta($post->ID, 'notice_content', true);
( T6 v. ]. y* F3 F0 a9 y* u' @8 x      ?># G: e; B8 a5 k. ^- _' b0 @
      <p>" i' t4 M' ]8 T( `% I, H
          <label for="notice-title">Notice Title</label><br>
# h9 z  R$ u4 B# N$ e6 \8 U          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
9 F$ [2 K3 W) b( E; n* e      </p>
5 M- M7 k+ e0 p2 J) ]      <p>
; q4 Z( K* J7 n+ Z* Z& D2 h. R          <label for="notice-content">Notice Content</label><br>
3 c( Y* W- C3 e          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
7 P$ h5 [  ?0 ?      </p>) u& J" j; \  h
      <?php- d5 Y! E7 a$ P3 d! ?( ~% B) w
   }
0 q( I6 }$ w7 z7 F( s7 E  X! c: E; _; G% m6 V' A1 s  ^4 v
   add_action('save_post', 'save_site_wide_notice_meta_box');
. R7 {8 ^" L; N- d" C   function save_site_wide_notice_meta_box($post_id) {
- K6 W( x4 p- ^7 q1 g- [      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
+ c0 [, A& w6 i. F* c, }3 R3 q         return;
/ O# ^9 u7 ~' n% k& T      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)% y4 E7 i3 ^& w2 F) q
         return;
6 {! }: C# g% y, v* o+ F7 u5 T
9 j  i; h/ ~5 B      if (isset($_POST['notice_title'])) {
$ T. `% x9 \9 @! S, s6 p/ m          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));8 Q1 i; Q" \4 K! W! j; P# e1 Q
      }$ S8 n' a" k( a  y
      if (isset($_POST['notice_content'])) {; ~& D! c1 w; {. z
          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));) o. h  h; G9 p  U7 V
      }8 H9 L# R+ P# P; C  L
   }
0 f+ N4 C% o; E! p   ```! B* O2 N4 i8 S5 \. p8 S) ~" M
/ _8 l3 d6 W+ y& Z/ z# P( ]
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。0 u( s7 e( P; e
7 }9 \/ h( ]) M: F
4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:: v- E1 y8 i# a# H$ D& \2 V0 X. u

( m5 C) H, @0 ~* G. p+ S   ```' }4 }3 d# n+ J# P7 t+ d
   $args = array(
) F; w* a6 c7 j3 ~& |" N3 J      'post_type' => 'site-wide-notices',
7 i0 b$ g8 G0 I% {& [, }! ~      'posts_per_page' => 3,( ~. z; u4 J, r
      'order' => 'DESC',8 i: j, c4 |. W/ U! c  i  u
      'orderby' => 'date'
6 F/ G$ a0 p5 V4 _1 y4 M) ?   );, C5 c- _3 e5 U5 C
   $query = new WP_Query($args);; b# y8 u+ F+ x  G$ L/ e7 d2 m4 _
   if ($query->have_posts()) :' B/ A  W/ `7 c: |! L
      while ($query->have_posts()) : $query->the_post(); ?>' B8 d/ z3 R' \4 J" t& E" g
          <div class="notice">( a! V# A) o; e0 s+ `
              <h3><?php the_title(); ?></h3>& o: ~+ b# |8 x2 E
              <div class="notice-content"><?php the_content(); ?></div>. K4 W* |. J9 s+ p8 S3 b
          </div>
. t8 G) R: e0 V4 y. p      <?php endwhile;6 H5 t. K, G6 V# ]$ L* O
      wp_reset_postdata();
8 R7 ^$ `7 f1 Y+ a5 e, @8 d# J% Q   endif;
# K& F7 e2 V) |: |$ h- R1 R   ```
8 J* @$ \8 y, i( d# j
/ i0 U& b; y  v4 E1 }, n* |0 v   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-10 04:12 , Processed in 0.016738 second(s), 3 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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