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

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

[复制链接]

347

主题

565

回帖

3689

积分

管理员

积分
3689
QQ
发表于 2023-5-18 08:34:20 来自手机 | 显示全部楼层 |阅读模式
您可以使用以下方法来快速在 WordPress 后台增加一个全站公告的功能:
. [) G7 n" q3 V$ `. B/ o0 x+ U' Z$ F: i' N- _5 k) b" `
1. 在 WordPress 插件库中寻找可用的公告插件,例如 WP Notification Bars、Announcements、SSP Elegant Notification Bar 等。
: p6 O3 y( |  r& b* W% Y/ T/ v4 ]0 v9 ?, g7 ~! l5 g! {  }& y
2. 安装并激活您选择的插件。您可以在 WordPress 后台的「插件」->「添加新插件」界面中搜索并安装插件,然后在「插件」->「已安装插件」界面中激活它。
* e3 l: [1 \) }/ d; i0 T1 S) ^4 |1 `% F: X. w# \' O- R; o
3. 在插件的设置页面中配置您的公告内容和样式。不同插件的设置选项和功能可能会有所不同,但通常都允许您定义公告的文本、链接、颜色、位置等。
* P% _. }* Q: \( k8 L% o% y/ o, x: T7 v/ i6 n2 A
4. 将公告添加到您的网站上。在插件设置页面中完成公告的设置后,您可以通过编辑 WordPress 主题的代码或使用插件提供的方法将公告添加到您的网站中。不同插件可能使用不同的添加方法,具体请查看详细说明或联系插件作者。" ~$ @3 M4 \% n7 Q& I
% D: v6 U1 O' ]/ ^8 [
注意,如果您使用的 WordPress 主题已经包含了公告功能,或使用了其他公告插件,可能会导致冲突或功能重复。如果您遇到了这样的问题,请查看主题文档或联系主题作者以获取解决方案。
回复

使用道具 举报

347

主题

565

回帖

3689

积分

管理员

积分
3689
QQ
 楼主| 发表于 2023-5-18 08:34:51 来自手机 | 显示全部楼层
如果是自己开发的话,具体要怎么做?
$ H2 z8 w0 g6 {" k" O5 R8 g5 W6 \4 h
如果您自己开发 WordPress 主题或插件,并且需要在后台中增加一个发布全站公告的功能,可以按照以下步骤操作:2 y" i' l1 F6 l7 E1 _( [
$ f0 }: W4 r, ~) n! ~! S
1. 创建一个自定义的 WordPress 后台页面。您可以使用 `add_menu_page()` 或 `add_submenu_page()` 函数来创建一个自定义后台页面,例如:) X) m9 g( y- F8 O& E8 p

* @* ~  Z% z" L2 b: D* H   ```4 n7 n! x% M# k
   add_menu_page(, K0 @  v; i% l: I
       '全站公告',
4 ^0 c. J0 r8 O3 X% u+ p1 i* V! c6 H       '全站公告',) k6 O( W& }; o, w+ C
       'manage_options', //需要的用户权限
( K4 M1 R) ~7 m! J. g2 M7 Z       'site-wide-notice', //后台页面的 slug
; I0 R0 G: O9 W. B) D7 y       'render_site_wide_notice_page', //用于渲染后台页面内容的函数5 U; t4 ~0 l( W- f6 J. v
       'dashicons-megaphone', //用于显示在菜单中的图标
; ^- D9 R9 e) _0 K5 h( a! `  `8 Q       30 //菜单项的位置; G; l( d* W( U: j
   );0 _3 m% I+ ~: ]4 a
   ```. x; M7 d) C8 `1 Z! i  k
9 n  {0 }% U7 E3 }" B
   在该示例中,我们使用 `add_menu_page()` 函数来创建了一个名为“全站公告”的菜单项,并指定需要的用户权限(`manage_options`),在后台页面的 slug 为 `site-wide-notice`,用于渲染后台页面内容的函数为 `render_site_wide_notice_page`,在菜单中显示的图标为 `dashicons-megaphone`,菜单项的位置为 30。1 ^, i/ |5 N& s) @; L% W3 P4 F

* l3 O4 Y* x1 [" k' {2. 编写用于显示和保存全站公告内容的代码。您可以在自定义后台页面中创建一个表单,让用户输入公告的标题、内容等信息,并将这些信息保存到 WordPress 数据库中的某个表中。例如:
/ s" y' ~/ V$ z0 N% G. v3 l+ t, V5 N# y1 h, d
   ```
5 W! l1 @# \* e! d   function render_site_wide_notice_page() {( g( g3 @$ g4 T6 o2 ~. ?( ]8 ]/ x
      // 渲染后台页面内容
! N+ V9 C) l( Y# W$ T      echo '<div class="wrap">
  R, U) t& W, O1 I1 e          <h2>全站公告</h2>
2 K3 F5 |" E9 f5 p          <form method="post" action=""># i0 u# W, I; E6 C
              <label for="title">公告标题:</label>
/ f+ u" I9 b# C              <input type="text" name="title" value="'.get_option('site_wide_notice_title').'"><br><br>
2 V0 t; y% x' P  @, q: g              <label for="content">公告内容:</label>
' m: A& U! A& L6 O. c- z              <textarea name="content">'.get_option('site_wide_notice_content').'</textarea><br><br>
, _$ k4 S* Y% w4 |              <input type="submit" name="submit" value="保存设置">
6 e: w+ ]* R% _6 R+ R9 B5 B          </form>4 ^: \, l' z2 H* Q1 N. b0 M3 d
      </div>';
+ J- Z  |8 V" M4 }5 c. h8 q% ?
- ]. w1 V9 F# h8 c      // 处理表单数据6 S+ r, L: p* R
      if (isset($_POST['submit'])) {, k, h2 s1 j2 \/ ^4 J7 |
          update_option('site_wide_notice_title', sanitize_text_field($_POST['title']));
) o% I6 |2 R5 c$ E( ]          update_option('site_wide_notice_content', sanitize_text_field($_POST['content']));! g) T) `: x4 \& ~
      }% W2 z& [- c! O' y1 V
   }$ S' t, p: {/ D" o
   ```
: E1 V  c* n( T* \: }/ r0 x) V: Q/ D
   在该示例中,我们创建了一个表单,让用户输入公告的标题和内容,并使用 `update_option()` 函数将这些数据保存到名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的 WordPress 配置项中。如果用户点击提交按钮,则将表单数据更新到配置项。% x! a: |3 N; t4 V" f2 Q5 T, ]

+ a) I; `3 R& `5 V2 D% i1 D. ~2 W3. 在 WordPress 主题中显示全站公告。您可以通过在 WordPress 主题的 `header.php` 文件中添加代码,将公告内容显示在网站的顶部或其他位置。例如:+ ]& X9 I3 ^( K! k$ T- _5 u  E
( Q; u. E3 _# q$ ?
   ```, z0 Z% x$ W6 C
   <?php
/ l" y- v6 q" @( P   $site_wide_notice_title = get_option('site_wide_notice_title');0 ?6 b' X2 F/ v9 y8 r4 ?8 L# b; Y
   $site_wide_notice_content = get_option('site_wide_notice_content');
% j0 a4 R; N! p% N& A   if (!empty($site_wide_notice_title) || !empty($site_wide_notice_content)) {4 U- f" W  H- j) x3 S) g- O* S
       echo '<div class="site-wide-notice">';1 N6 h' W. r+ Y0 V$ B& I
       if (!empty($site_wide_notice_title)) {; f0 F: l+ f1 f# W3 b; q4 e
           echo '<h3>'.$site_wide_notice_title.'</h3>';8 b; a# z% w( S& W( P
       }
" g: V& E, D% M, \0 `- p# d       if (!empty($site_wide_notice_content)) {( W) }$ O' V5 G, o
           echo '<p>'.$site_wide_notice_content.'</p>';
# W: R) _: {* b% B7 E# Y       }
* y/ S& f# ?+ D7 f. W       echo '</div>';( K$ t0 P; v% \  _- d
   }
, x3 y. h: K* w3 d, S) y   ?>
$ l2 y/ P# o! a( u   ```7 Y' J3 k6 X, O5 S

1 K4 a! p% `7 t  G   在该示例中,我们检查 WordPress 配置项中名为 `site_wide_notice_title` 和 `site_wide_notice_content` 的值是否为空,如果不为空,则在 `header.php` 文件中添加一个名为 `site-wide-notice` 的容器,将公告标题和内容显示在其中。9 \4 E7 Z2 z6 a
) Y) X8 I# Y9 ]$ e; B- A
请注意,本示例仅提供了一个框架,具体实现可能会根据您的需求和实际情况有所不同,您需要根据自己的需要进行修改和调整。同时,为了确保安全性,您还会需要对用户输入的数据进行适当的验证和过滤,例如使用 `sanitize_text_field()` 函数来过滤表单数据。
回复

使用道具 举报

347

主题

565

回帖

3689

积分

管理员

积分
3689
QQ
 楼主| 发表于 2023-5-18 09:26:50 来自手机 | 显示全部楼层
那如果需要发布多条公告,并进行管理,要如何实现?
9 }: [% }, U! U- ]) F3 A* C
3 C/ j" f* W9 \" D# r您可以创建一个自定义的 WordPress文章类型,例如“公告”,并对其进行自定义化,以实现发布多条公告并进行管理的功能。以下是具体步骤:+ p" ?: v$ k/ K' T( i2 b2 S

( `. d6 V) h. G' x& n0 J( b; i1. 创建一个自定义的文章类型:在你的主题 `functions.php` 文件中,添加以下代码:& u, h8 a3 O0 P" e- s. D0 f

) E5 e# p/ b  l* O. l& I% ?```( y3 o/ i# b# [% `" b
function create_custom_post_type() {- U) d) R6 C# G) p6 _
    $args = array(
; y' t- E; o& ^. H# s& h        'labels' => array(
3 x# u3 U- i; k6 n1 H9 s            'name' => '公告',  j) S0 z; W% R& H2 _
            'singular_name' => '公告'
( n( P8 N& Q' O        ),
" |! B9 j8 d# ~4 X- Y$ u        'public' => true,
0 T( @# L4 k, k& \9 q1 R9 R! B  K        'has_archive' => true,
+ I. F+ E+ T- {1 p' A) u        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions')," F- p! d: E- v; u
        'taxonomies' => array('category', 'post_tag'),
* I: O1 }# r5 w0 ]4 ~7 G        'menu_icon' => 'dashicons-megaphone',
( }& `7 g1 h1 g0 R        'menu_position' => 5,
0 W' k" p# s% H" ]' m/ V        'rewrite' => array('slug' => 'site-wide-notices'); K8 E8 E( T& D" Z/ U
    );  N- N/ m$ w, \3 b7 b' E$ d0 t5 P
    register_post_type('site-wide-notices', $args);- Q% M% i2 ~! {
}2 |9 O" j# `+ d8 ]5 F2 G/ J
add_action('init', 'create_custom_post_type');
& ]+ ]9 Z' n% l! z2 O, ~```/ q* b7 c, f( i. l4 j2 M( C
% B4 q! O: E+ s! M
在示例中,我们注册了一个名为 "site-wide-notices" 的新的文章类型,并定义了文章类型名称、缩略名、文章类型支持的功能、分类法、管理菜单图标等设置。$ |0 a# ]& h3 w3 w

& {3 k9 C1 Z. B7 |8 h7 A2. 在你的 WordPress 后台中创建新文章类型“公告”:现在,你就可以在 WordPress 后台中通过导航到“公告”菜单,来添加、编辑或删除公告了。您可以在编辑公告的页面中添加标题、内容、特色图像或其他自定义的元数据字段。
% I( [( N: [9 W/ E, Q* M  g/ \7 P7 m! V5 v3 ^9 n: C, G% K8 |
3. 在主题中显示公告:在您的 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:% I0 J6 i: K9 S3 r0 H% _

+ Y: u' S* o, J; |' I```; V; B! @2 i; _+ s: g
$args = array(3 _! k1 `- W: ^. c& Z
    'posts_per_page' => 3, // 显示的公告数量
4 N8 z2 |  |. g  |    'post_type' => 'site-wide-notices', // 公告文章类型
) \9 C* R1 ]1 s/ S    'post_status' => 'publish', // 公告状态
8 W' ~) R2 y7 W    'order' => 'DESC' // 排序方式
3 F; R* r' k1 h* f% ^3 r);
3 {8 }6 E/ K5 x8 x- s" Y
; D! U: \3 o9 L  t1 A( e$notices = new WP_Query($args);' w5 f. H4 V; O$ Q* n
if ($notices->have_posts()) :
4 L" x0 x  z8 [$ K# ^5 T! l    while ($notices->have_posts()) : $notices->the_post(); ?>
& n* ~& n4 @/ G& h) W        <div class="notice">
) w" g- s. \/ |            <h3><?php the_title(); ?></h3>+ y% F5 M# c: s5 `0 e( Z, c) p( [0 j  }
            <div class="notice-content"><?php the_content(); ?></div>+ e/ J1 u0 M! D8 U6 G! h4 C4 U
        </div>; {- C2 L3 t! F
    <?php endwhile;
& E2 ]0 y; E5 Q. H2 felse :- b! ~  G# A  }% m4 H' p
    echo "暂无公告";6 f( {9 n' Q& w+ Y! U! G, v
endif;
. h  _" K( I# rwp_reset_query();. |3 P, w6 F# `0 U5 r2 f
```8 g0 p; O+ \4 z2 {
+ _& P. S$ t$ u- [- B1 U
这段代码将使用 WordPress 的 `WP_Query` 类从公告文章类型中获取最新发布的 3 条公告,并在前端页面中循环遍历并显示公告标题和内容等信息。3 w: Q8 d% V! o! F; x: v

  q; _' @! x3 b7 `& g; N: U3 \5 P以上便是实现 Wordpress 后台发布多条全站公告的基本步骤。需要注意的是,自定义文章类型可对应多篇文章,具体根据您的需求进行调整。
回复

使用道具 举报

347

主题

565

回帖

3689

积分

管理员

积分
3689
QQ
 楼主| 发表于 2023-5-18 09:27:55 来自手机 | 显示全部楼层
如果不想修改functions.php,有其他更好的办法吗?" U* s5 w6 W- X

( V8 J( N, J. ?如果您不想修改主题 `functions.php` 文件,也可以创建一个自定义插件来添加新的文章类型。% ]" b' C5 \  l$ T& K) `, ]8 p! g: c
1 X4 {7 Z) W) u# ]
以下是创建自定义插件的步骤:) w. F5 u6 }3 T6 r% m

9 d$ k2 p7 \' \+ }; M1. 创建新插件:在 WordPress 插件目录中创建一个新的子目录,例如 `site-wide-notices-plugin`,然后在该目录中创建一个新的 PHP 文件,例如 `site-wide-notices-plugin.php`,并在文件头部添加必需的标记,例如:
/ l2 g% [7 ~1 g: a! [6 t9 T
. U5 V& p' q: T2 _   ```
/ @9 ?2 q- U' ?) z   <?php, y5 l' ]1 R5 r- q! U
   /*
/ m# n) N- G; T, [1 }   Plugin Name: Site Wide Notices Plugin
: v- B4 U" s0 N5 ]- F' w" q   Description: Adds a new custom post type for site-wide notices.
* c  }4 I# K  R6 E   Version: 1.0
! I# V6 f2 |/ W3 E/ Z' x2 v   Author: Your Name
: c6 y6 c7 V9 q& v. w' }% L; i   Author URI: http://example.com* w7 U1 A3 B$ a* {1 X# ]* P4 f
   */
" R) H8 g4 m9 b
1 _' l3 Q9 v8 }   // Add plugin code here...
- A& L0 Z( b! H  z1 s5 V6 w   ```
  b" k* |* u8 r% I& Z) O5 ^! m* F8 \2 c
   在示例中,我们创建了一个名为 `Site Wide Notices Plugin` 的新插件,该插件添加了一个新的自定义文章类型,并指定了版本、作者和作者 URI 等元信息。
. E# q. P0 ~3 d# ?6 f7 E0 g4 V- N7 ?/ q( c1 R8 W' M/ j
2. 在插件中注册新的文章类型:在 PHP 文件中添加以下代码,用于注册一个名为“site-wide-notices”的自定义文章类型:4 b* m# x5 [6 n( l4 z# t2 C

. S. Z' {1 _& Z% {   ```4 h3 j; j% _! j% O5 _) B
   add_action('init', 'create_custom_post_type');* D; x( C& _+ g; @! C6 B  f; s2 A
   function create_custom_post_type() {* q8 V3 j* F' s6 q' D
      $labels = array(' G0 P7 ?& q/ y9 n9 V
          'name' => 'Site Wide Notices',
6 h0 C5 e# E: X- O' ^' K5 o          'singular_name' => 'Site Wide Notice',
; Z, H5 x; s0 Y6 L" F8 ]          'add_new' => 'Add New',
% x1 _) Y" y* m6 s          'add_new_item' => 'Add New Site Wide Notice',( x. V5 P4 ?: [
          'edit_item' => 'Edit Site Wide Notice',/ N: ~; f7 g6 l  B; B0 X5 z- m2 A
          'new_item' => 'New Site Wide Notice',
9 N2 S) e' x! ?: l- i$ n' S          'view_item' => 'View Site Wide Notice',* N( X' A0 C0 e# \  T7 I  c$ @* M
          'search_items' => 'Search Site Wide Notices',& F- ^# N5 H4 F  p
          'not_found' => 'No site-wide notices found',. E  l7 d! F  t5 \
          'not_found_in_trash' => 'No site-wide notices found in trash'
2 f. L) \! z6 T; ^% d' m      );
# M7 N4 L+ E$ ^* ~9 B" B7 n4 C
8 ]' p+ L9 V0 F. l# b8 m  D      $args = array(
! e# U' Z3 \2 L          'labels' => $labels,
( N: i! J+ I+ C% d; h* P, E1 R          'public' => true,' X! `: d: o0 p# d6 A4 E
          'has_archive' => true,+ |! o* d4 |6 w1 }3 m2 q9 j
          'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
. j" P& ]2 P5 h+ C$ j          'taxonomies' => array('category', 'post_tag'),7 P- c9 V; l+ y* F( a
          'menu_icon' => 'dashicons-megaphone',% ]* x& `/ y2 u# w0 k) b' Y
          'menu_position' => 5,
/ k! F  O. q( X/ h          'rewrite' => array('slug' => 'site-wide-notices')
, ]7 t! ?1 l; N0 S# H( }: O4 _      );4 }: g) o9 T  V5 W

% A' K7 g7 q8 d4 c/ }  y0 d7 |% z      register_post_type('site-wide-notices', $args);
0 M( I1 E1 F$ g! B( C   }0 y, d; f# a. Z: M+ d4 ]6 S
   ```
4 b9 w) P. c1 w6 g; ], {. ?
3 m! w; Y0 n' J# v8 _   在示例中,我们使用 `register_post_type()` 函数注册一个新的文章类型,并定义了该文章类型的名称、标签和其他设置。
8 ^7 a, B1 g+ r: k. y: k* G
2 C' K; P! t8 |( O$ g3 H3. 在后台中添加公告编辑页面:您可以使用 `add_meta_box()` 函数在后台中添加公告编辑页面,并自定义字段和元数据,例如:9 `# n2 g( {+ L, U, D
0 K& r! F/ t6 F8 y2 L% J$ u
   ```, T& a2 z2 j0 F# }' I. w+ u
   add_action('add_meta_boxes', 'add_site_wide_notices_boxes');* x. t; ]6 H2 H. R9 @+ \7 F
   function add_site_wide_notices_boxes() {
- ]- j" w8 O( u% Q      add_meta_box('notice-details', 'Notice Details', 'notice_details_meta_box', 'site-wide-notices', 'normal', 'high');& `! R1 C- o0 f& W0 R9 |( ^2 k0 U7 |
   }
3 h4 m! d/ |. U, g7 m2 y8 W( ~2 N/ M  c* t
   function notice_details_meta_box($post) {
7 }& a, ]! |1 R7 M      wp_nonce_field(basename(__FILE__), 'notices_nonce');
  [- M6 {0 K0 q4 d# N: B- x      $notice_title = get_post_meta($post->ID, 'notice_title', true);
1 g% L! i7 M: j# ]( S$ B      $notice_content = get_post_meta($post->ID, 'notice_content', true);* M( {7 y, q  Q( o" L( h$ O
      ?>7 Q" I* A3 J  M2 e( K: j1 L
      <p>
+ Z' d) D! U, h0 k% f/ y          <label for="notice-title">Notice Title</label><br>* l2 |  w: S* X2 r1 q7 a4 A
          <input type="text" id="notice-title" name="notice_title" style="width: 100%;" value="<?php echo $notice_title; ?>">
% \, N. M- D6 p0 b4 x" y      </p>
, t1 Q' y% ~* P3 G      <p>8 P6 ^% s9 i5 A3 ?, M+ E8 O( {* y- Z& k
          <label for="notice-content">Notice Content</label><br>
- H+ X( x0 U) h$ O  A) v. @2 W$ a. U3 A1 M          <?php wp_editor($notice_content, 'notice-content', array('textarea_rows' => 10)); ?>
% M8 Y; Q+ N# o2 w      </p>. Z& A" \) U& C4 t8 \
      <?php
0 X7 h& G5 c( E8 Q& r6 D# R   }
7 d8 \& W, r( v% N! d6 Z" [- F: ~* N4 t, J
   add_action('save_post', 'save_site_wide_notice_meta_box');
: x* g& l- h0 D6 ?# \7 Q" R   function save_site_wide_notice_meta_box($post_id) {& Q. N, g) h- @  g+ T- {# N
      if (!isset($_POST['notices_nonce']) || !wp_verify_nonce($_POST['notices_nonce'], basename(__FILE__)))
4 c; L2 ~- ?: \9 S/ {2 `! r" r3 Y         return;; X9 U9 A& j! w$ l" w- d& z
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
0 W; K9 @5 ~1 t& k- d         return;
1 z1 J4 ?/ ^! X; s. w4 X9 P7 E3 }, O  A8 p, C# ]
      if (isset($_POST['notice_title'])) {
4 z/ R) x3 s1 B  b4 X! ~          update_post_meta($post_id, 'notice_title', sanitize_text_field($_POST['notice_title']));! d6 v. g3 d/ i
      }6 j8 c4 N3 Z+ p: f) y2 ?* ~  C7 n
      if (isset($_POST['notice_content'])) {
( |( n' S3 |* @+ }          update_post_meta($post_id, 'notice_content', wp_kses_post($_POST['notice_content']));7 K' m/ {/ u9 y2 J1 l! Y  V/ j
      }$ V2 n! n4 D* h4 t& r) b8 m
   }1 k+ }( }* G" d3 `5 i- z
   ```
, o0 c; V) N7 @2 A4 e- Z5 D1 c$ @4 Q/ t6 l
   在示例中,我们使用 `add_meta_box()` 函数添加了一个名为 `notice-details` 的元框,用于在编辑页面上显示自定义字段和元数据。在 `notice-details_meta_box()` 函数中,我们使用 `wp_editor()` 函数添加了一个可视化编辑器,用于编辑公告内容。在 `save_site_wide_notice_meta_box()` 函数中,我们使用 `update_post_meta()` 函数将公告标题和内容保存到 WordPress 数据库中。3 Y% _. t8 z7 l! R6 x  V

9 v9 I6 f1 t3 W% Z% o' |! q- G4. 在前端页面中显示公告:在 WordPress 主题文件中添加以下代码,以在您的网站上显示公告:8 C3 K# H9 N9 i

$ W* u- X) d1 w2 N' v! r! [+ u   ```
- W" {/ r4 K* W' O* O4 {   $args = array(
3 M  {2 y7 u+ |1 c/ I" A      'post_type' => 'site-wide-notices',; s4 D) X8 m5 s3 W7 k/ ^
      'posts_per_page' => 3,/ }  {+ K( |' \9 ?: }$ t* ]
      'order' => 'DESC',
! ]# j' y+ o  }8 T. v# D      'orderby' => 'date'2 w: e. K" v2 p/ e; r. K
   );1 T1 ]1 Q0 k$ Z3 u: [% Y
   $query = new WP_Query($args);
- ]+ |9 E; o- F5 ^   if ($query->have_posts()) :* e6 v) e9 }( r; @6 l; a$ s
      while ($query->have_posts()) : $query->the_post(); ?>* S  g) y  s8 Q  l
          <div class="notice">
  C5 L/ f( F+ A% v              <h3><?php the_title(); ?></h3>
% W# C$ ~* \  P/ w              <div class="notice-content"><?php the_content(); ?></div>
( R; Y7 ]) ~) c          </div>
2 r, g6 ]% e% w6 r/ E0 B      <?php endwhile;3 o( S' j. }  [' c
      wp_reset_postdata();
- J! a* m' j* }) ?8 q   endif;  O6 b% r2 Q) x9 H5 d! D" a4 h/ I
   ```6 m" P5 |0 O9 I8 O5 ~( u
9 {) @7 D5 b$ r
   在示例中,我们使用 `WP_Query` 类从 `site-wide-notices` 文章类型中获取最近发布的 3 条公告,并在前端页面中循环遍历并显示标题和内容等信息。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-22 15:51 , Processed in 0.013598 second(s), 2 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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