開始修改主要 bbPress 模板

開始修改主要 bbPress 模板

此頁面的目標受眾是基本熟悉編輯 WordPress 主題模板的人。

如果您只是選擇 bbPress 並且想要對正在呈現的模板進行一些基本更改,例如刪除 WordPress 評論代碼或任何僅應在博客文章中顯示的帖子元,那麼請按照本指南進行操作。

在 bbPress 的普通安裝中,該插件會從您當前活動的 WordPress 主題中查找以下模板,並將論壇 「注入」 到它找到的第一個模板中。

它將找到具有下面列出的名稱的任何頁面模板。

  • plugin-bbpress.php
  • bbpress.php
  • forums.php
  • forum.php
  • generic.php
  • page.php
  • single.php
  • singular.php
  • index.php

對於 WordPress 主題,最常見的是將論壇代碼注入 page.php 文件。要找到確切的文件,請安裝插件 What The File 並轉到您網站的 bbPress 論壇部分並檢查您的 WordPress 工具欄,看看要修改的確切文件是什麼。

然後您可以複製您找到的文件並將其重命名為上面列出的任何文件名,並確保它位於您的主題根目錄中。

wp-content/themes/%mytheme%/bbpress.php 其中 %mytheme% 是您當前活動主題的名稱。

建議使用子主題,以免在下次更新主題時丟失任何更改。

畢竟,您可以自定義新的 bbpress.php 文件以刪除任何不需要的代碼/設計您的 bbPress 論壇,bbPress 插件應該選擇該模板並使用該自定義模板呈現其所有頁面。

示例模板

不要只是將此代碼複製並粘貼到空白模板中。所有的主題都不一樣。將其用作指南。

請閱讀代碼中的注釋。

<?php
 
/*
*
* The template for displaying all bbPress pages
*
* This is the template that displays all bbPress pages by default.
* Please note that this is the template of all bbPress pages
* and that other 'pages' on your WordPress site will use a
* different template.
*
* @package WordPress
* @subpackage Theme
*/
 
 
/*
Self explanatory its a functions that gets your header template.
*/
 
get_header(); ?>
 
 
<?php
/*
Surrounding Classes for the site
 
These are different every theme and help with structure and layout
 
These could be SPANs or DIVs and with entirely different classes.
*/
?>
 
<div id="primary" class="site-content">
 
<div id="content" role="main">
 
 
<?php
/*
Start the Loop
*/
?>
 
<?php while ( have_posts() ) : the_post(); ?>
 
 
<?php
/*
This is the start of the page and also the insertion of the post classes.
 
Post classes are very handy to style your forums.
*/
?>
 
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
 
 
<?php
/*
This is the title wrapped in a header tag
 
and a class to better style the title for your theme
*/
?>
 
<header class="entry-header">
 
<h1 class="entry-title"><?php the_title(); ?></h1>
 
</header>
 
 
<?php
/*
This is the content wrapped in a div
 
and class to better style the content
*/
?>
 
<div class="entry-content">
<?php the_content(); ?>
</div>
 
<!-- .entry-content -->
 
 
<?php
/*
End of Page
*/
?>
 
</article>
 
<!-- #post -->
<?php endwhile; // end of the loop. ?>
 
</div>
 
<!-- #content -->
 
</div>
 
<!-- #primary -->
 
 
<?php
/*
This is code to display the sidebar and the footer.
 
Remove the sidebar code to get full-width forums.
 
This would also need CSS to make it actually full width.
*/
?>
 
<?php get_sidebar(); ?>
<?php get_footer(); ?>

為 bbPress 論壇呈現自定義模板代碼的另一種方法是在 PHP if 語句中使用條件標記,例如 is_bbpress() 。

這是一個例子。

<?php if ( is_bbpress() ) : ?>
 
<div class="abc">
 
This content should show if it is bbPress
 
    </div>
 
<?php else : ?>
 
<div class="xyz">
 
This should show if it is not bbPress
 
    </div>
 
<?php endif; ?>

上次修改 2021.12.25

報告拼寫錯誤

以下文本將發送給我們的編輯: