WORDPRESS如何更换模板 wordpress自定义文章模板_html

wordpress 自定义

Do you want to create a custom single post template in WordPress?

您是否要在WordPress中创建自定义的单个帖子模板?

Custom single post templates allow you to use different layouts for your individual blog posts. Many WordPress themes come with a few different page templates, and you can also create your own if needed.

自定义单个帖子模板使您可以为单个博客帖子使用不同的布局。 许多WordPress主题都带有一些不同的页面模板,您也可以根据需要创建自己的模板。

In this article, we will show you how to easily create custom single post templates in WordPress. We’ll share multiple methods, so you can choose one that works best for your needs.

在本文中,我们将向您展示如何轻松地在WordPress中创建自定义的单个帖子模板。 我们将分享多种方法,因此您可以选择一种最适合您的需求的方法。

WORDPRESS如何更换模板 wordpress自定义文章模板_python_02

Note: This tutorial requires you to edit WordPress theme files. If you haven’t done this before, then check out our tutorial on how to copy and paste code in WordPress.

注意:本教程要求您编辑WordPress主题文件。 如果您以前没有做过,请查看有关如何在WordPress中复制和粘贴代码的教程。

(When Do You Need a Custom Single Post Template?)

Sometimes you may want a different look and feel for certain posts on your website. For instance, you may want to use a different layout for featured articles or stories in a particular category.

有时,您可能希望网站上的某些帖子具有不同的外观。 例如,您可能希望对特定类别中的特色文章或故事使用不同的布局。

This is where you’ll need a custom single post template in WordPress.

这是您在WordPress中需要自定义单个帖子模板的地方。

By default, WordPress uses the single post template based on WordPress template hierarchy. All themes come with a single.php template which is used as the default for all your single posts.

默认情况下,WordPress使用基于WordPress模板层次结构的单个帖子模板。 所有主题都带有一个single.php模板,该模板用作所有单个帖子的默认设置。

Some themes may also include additional templates or layout choices that you can use.

某些主题可能还包括您可以使用的其他模板或布局选择。

It is very much like creating a custom page template. Most WordPress themes also come with page templates that you can use while editing a page in WordPress.

这非常类似于创建自定义页面模板 。 大多数WordPress主题还带有页面模板,您可以在WordPress中编辑页面时使用它们。

WORDPRESS如何更换模板 wordpress自定义文章模板_WORDPRESS如何更换模板_03

Having said, let’s take a look at how to easily create custom single post templates in WordPress. We’ll show you multiple methods, so you can use the one that works best for you.

话虽如此,让我们看一下如何轻松地在WordPress中创建自定义的单个帖子模板。 我们将向您展示多种方法,因此您可以使用最适合您的一种方法。

  • Creating a single post template in WordPress using the block editor使用块编辑器在WordPress中创建单个帖子模板
  • Creating a custom single post layout in WordPress using Beaver Builder使用Beaver Builder在WordPress中创建自定义单个帖子布局
  • Using theme settings to create single post layouts in WordPress使用主题设置在WordPress中创建单个帖子布局
  • Manually creating custom single post template in WordPress在WordPress中手动创建自定义单个帖子模板
  • Create custom single post templates by category in WordPress在WordPress中按类别创建自定义单个帖子模板
  • Creating custom single post template for specific author为特定作者创建自定义单个帖子模板
(Creating a Single Post Template in WordPress using The Block Editor)

This method does not really create a post template and is limited in flexibility. However, it is the easiest way to save your own single post layouts and then reuse them.

此方法实际上不会创建发布模板,并且灵活性受到限制。 但是,这是保存您自己的单个帖子布局然后再使用它们的最简单方法。

The default WordPress block editor comes with a built-in feature that allows you to save and reuse blocks. One such reusable block is called the Group block.

默认的WordPress块编辑器带有内置功能,可让您保存和重复使用块。 一种这样的可重用块称为组块。

The group block basically allows you to put several blocks and entire post layouts into one group. You can then save this group block and reuse it in your other posts.

基本上,组块使您可以将几个块和整个帖子布局放在一组中。 然后,您可以保存此组块并在其他帖子中重复使用。

Let’s take a look at how to use the group block to save your custom post templates.

让我们看一下如何使用组块保存自定义帖子模板。

First, you need to create a new post in WordPress. After that, simply add a group block to the content area.

首先,您需要在WordPress中创建一个新帖子。 之后,只需将一个组块添加到内容区域。

WORDPRESS如何更换模板 wordpress自定义文章模板_html_04

Now you can start adding blocks into the group block to create a single post layout. You can add any blocks you want including columns, media and text, cover images, and more.

现在,您可以开始将块添加到组块中以创建单个帖子布局。 您可以添加任何所需的块,包括列,媒体和文本,封面图像等等。

WORDPRESS如何更换模板 wordpress自定义文章模板_java_05

Once you are satisfied with the layout you have created, you need to take your mouse up and select the group block. Click on the three-dot menu icon and then select the ‘Add to Reusable Blocks’ option.

对创建的布局满意后,需要抬起鼠标并选择组块。 单击三点菜单图标,然后选择“添加到可重用块”选项。

WORDPRESS如何更换模板 wordpress自定义文章模板_java_06

Next, you need to provide a name for the reusable block and then click on the Save button. WordPress will now save your reusable block including all the blocks inside the group block.

接下来,您需要提供可重用块的名称,然后单击“保存”按钮。 WordPress现在将保存您的可重用块,包括组块内的所有块。

You can then edit any existing post on your website or create a new one. On the post edit screen, simply click on the add new block button and look for your saved block under reusable blocks.

然后,您可以编辑网站上的任何现有帖子或创建一个新帖子。 在帖子编辑屏幕上,只需单击添加新块按钮,然后在可重复使用的块下查找已保存的块。

WORDPRESS如何更换模板 wordpress自定义文章模板_WORDPRESS如何更换模板_07

Add the block to your post and WordPress will load your entire group block with all the blocks and settings as you saved them.

将块添加到您的帖子中,WordPress将在保存它们时将所有块和设置加载到整个组块中。

This method allows you to save your custom layouts. However, it does not allow you to change how your theme handles single posts.

此方法使您可以保存自定义布局。 但是,它不允许您更改主题处理单个帖子的方式。

If you would like greater flexibility then continue reading.

如果您想要更大的灵活性,请继续阅读。

(Creating a Custom Single Post Template in WordPress Using Beaver Builder)

The easiest way to create a custom single post template is by using Beaver Builder. It is the best WordPress page builder plugin on the market and allows you to create a post template without writing any code or modify your WordPress theme.

创建自定义单个帖子模板的最简单方法是使用Beaver Builder 。 它是市场上最好的WordPress页面构建器插件 ,可让您创建帖子模板而无需编写任何代码或修改WordPress主题。

The first thing you need to do is install and activate the Beaver Builder plugin. For more details, see our step by step guide on how to install a WordPress plugin.

您需要做的第一件事是安装并激活Beaver Builder插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon activation, you need to visit Settings » Beaver Builder page and switch to the license tab to enter your license key.

激活后,您需要访问设置»Beaver Builder页面,并切换到许可证选项卡以输入许可证密钥。

WORDPRESS如何更换模板 wordpress自定义文章模板_html_08

You can find this information under your account on the Beaver Builder website.

您可以在Beaver Builder网站上的帐户下找到此信息。

By default, Beaver Builder is enabled for Pages in WordPress. You need to make it available for posts as well.

默认情况下,在WordPress中为页面启用Beaver Builder。 您还需要使其可用于帖子。

To do that, switch to the Post Types tab under Settings » Beaver Builder page. From here you need to check the box next to the ‘Posts’ option.

为此,请切换到“ 设置”»“ Beaver Builder”页面下的“帖子类型”选项卡。 在这里,您需要选中“帖子”选项旁边的框。

WORDPRESS如何更换模板 wordpress自定义文章模板_wordpress_09

Don’t forget to click on the ‘Save Post Types’ button to store your changes.

不要忘记单击“保存帖子类型”按钮来存储您的更改。

Now that everything is set up let’s create a custom post template.

现在已经完成所有设置,让我们创建一个自定义帖子模板。

Simply create a new post in WordPress and on the post edit screen click on the ‘Launch Beaver Builder’ button.

只需在WordPress中创建一个新帖子,然后在帖子编辑屏幕上单击“启动Beaver Builder”按钮。

WORDPRESS如何更换模板 wordpress自定义文章模板_wordpress_10

This will open the Beaver Builder interface with a live preview of your website. You can start adding modules and rows to your existing layout by clicking on the Add button at the top right corner of the screen.

这将打开Beaver Builder界面,并提供您网站的实时预览。 您可以通过单击屏幕右上角的“添加”按钮开始向现有布局添加模块和行。

You can also select a template as a starter point. There is even a blank template to give you clean slate to begin with.

您也可以选择模板作为起点。 甚至还有一个空白模板,可让您从头开始。

WORDPRESS如何更换模板 wordpress自定义文章模板_python_11

Once you have chosen a template, you can edit it by simple point and click. You can also add rows and columns and fill them with modules and elements to create your custom layout template.

选择模板后,可以通过简单的指向并单击来对其进行编辑。 您还可以添加行和列,并用模块和元素填充它们以创建自定义布局模板。

WORDPRESS如何更换模板 wordpress自定义文章模板_html_12

Feel free to experiment with different modules and play around with their settings.

随意尝试不同的模块并试用其设置。

Once you have created something that you would like to use as your post template. It is time to save it.

一旦创建了一些您想用作帖子模板的内容。 现在该保存它了。

Beaver Builder allows you to save your layouts as a template and then reuse them with other posts. Simply click on the menu at the top-right corner of the screen and select Save Template.

使用Beaver Builder,您可以将布局另存为模板,然后将其与其他帖子一起使用。 只需单击屏幕右上角的菜单,然后选择保存模板。

WORDPRESS如何更换模板 wordpress自定义文章模板_html_13

You’ll be asked to provide a name for your template. After that click on the Save button to store it.

系统会要求您提供模板名称。 之后,单击“保存”按钮进行存储。

WORDPRESS如何更换模板 wordpress自定义文章模板_html_14

Now, let’s see how to use this custom post template when creating posts.

现在,让我们看看在创建帖子时如何使用此自定义帖子模板。

Simply edit a post or create a new one and then launch the Beaver Builder to edit the post.

只需编辑帖子或创建一个新帖子,然后启动Beaver Builder即可编辑该帖子。

Next, you need to click on the Add button at the top right corner of the screen and then switch to the Templates tab. From here you need to select ‘Saved Templates’ from the Group dropdown menu.

接下来,您需要单击屏幕右上角的“添加”按钮,然后切换到“模板”选项卡。 在这里,您需要从“组”下拉菜单中选择“已保存的模板”。

WORDPRESS如何更换模板 wordpress自定义文章模板_python_15

Beaver Builder will now load your saved custom layout and you can then start adding content for your new article.

现在,Beaver Builder将加载您保存的自定义布局,然后您可以开始为新文章添加内容。

(Using Theme Settings for Custom Single Post Layouts)

Many popular WordPress themes come with built-in settings to customize the appearance of your single post template.

许多流行的WordPress主题都带有内置设置,可自定义单个帖子模板的外观。

If your theme supports these settings, then you’ll be able to find them on the post edit screen. The options available may change depending on the theme you are using.

如果您的主题支持这些设置,则可以在帖子编辑屏幕上找到它们。 可用的选项可能会根据您使用的主题而变化。

For instance, this is how the Astra theme offers customization options when editing a single post.

例如,这是Astra主题在编辑单个帖子时提供自定义选项的方式。

WORDPRESS如何更换模板 wordpress自定义文章模板_python_16

Using these options, you can change sidebars, hide headers, title, menus, and more.

使用这些选项,您可以更改边栏,隐藏标题,标题,菜单等。

On the other hand, many of the top WordPress themes come with ready-to-use templates that you can use.

另一方面,许多顶级WordPress主题都带有您可以使用的即用型模板。

If your theme includes single post templates, then you will find them under the Post Attributes tab while editing a post.

如果您的主题包含单个帖子模板,则在编辑帖子时将在“帖子属性”选项卡下找到它们。

WORDPRESS如何更换模板 wordpress自定义文章模板_java_17

These templates are complete layouts that you can use.

这些模板是可以使用的完整布局。

(Manually Creating Custom Single Post Templates in WordPress)

This method is a bit advanced as it requires you to edit theme files, copy and paste code, and optionally add custom CSS.

此方法有点先进,因为它需要您编辑主题文件,复制和粘贴代码,并可以选择添加自定义CSS。

First, you need to open a plain text editor on your computer like Notepad and paste the following code inside it:

首先,您需要在计算机上打开一个纯文本编辑器(如记事本),并将以下代码粘贴到其中:

<?php
/*
 * Template Name: Featured Article
 * Template Post Type: post, page, product
 */
 
 get_header();  ?>

This code defines a new template called Featured Article and makes it available for post, page, and product post types.

此代码定义了一个名为Featured Article的新模板,并使该模板可用于帖子,页面和产品帖子类型 。

You can save this file as wpb-single-post.php on your desktop.

您可以在桌面上将此文件另存为wpb-single-post.php 。

Next, you need to upload it to your current WordPress theme folder using an FTP client.

接下来,您需要使用FTP客户端将其上传到当前的WordPress主题文件夹中。

After that, you can log in to your WordPress admin area and create or edit a post. Scroll down a little on the post edit screen, and you will notice the new Post Attributes meta box with an option to select the template.

之后,您可以登录到WordPress管理区域并创建或编辑帖子。 在帖子编辑屏幕上向下滚动一点,您会注意到新的帖子属性元框带有选择模板的选项。

WORDPRESS如何更换模板 wordpress自定义文章模板_html_18

You will see your ‘Featured Article’ custom template listed there.

您将在此处看到“特色文章”自定义模板。

Right now your template is essentially empty so selecting it will simply display a white screen.

现在,您的模板实际上是空的,因此选择它只会显示白色屏幕。

Let’s fix this.

让我们解决这个问题。

The easiest way to do that is by copying the code from your theme’s single.php file and use it as a starting point.

最简单的方法是从主题的single.php文件中复制代码并将其用作起点。

Open the single.php file and then copy everything after the get_header() line.

打开single.php文件,然后在get_header()行之后复制所有内容。

Paste this code in your wpb-single-post.php file at the end. Now you can save this file and upload it back to your server.

最后将此代码粘贴到wpb-single-post.php文件中。 现在,您可以保存此文件并将其上传回服务器。

However, this will look exactly the same as your current single post template. You can now start making changes to your custom single post template.

但是,这看起来与您当前的单个帖子模板完全相同。 现在,您可以开始更改自定义的单个帖子模板。

You can add your own custom CSS classes, remove sidebars, create a full-width template or anything you want.

您可以添加自己的自定义CSS类,删除侧边栏,创建全角模板或任何您想要的东西。

(Create Custom Single Post Templates Based on Category)

Want to use a custom single post template based on categories? For example, posts in the travel category can have a different layout than posts in photography.

是否要使用基于类别的自定义单个帖子模板? 例如,旅行类别中的帖子可以与摄影中的帖子具有不同的布局。

Here is how you can do that.

这是您可以执行的操作。

First you need to add this code to your theme’s functions.php file or a site-specific plugin.

首先,您需要将此代码添加到主题的functions.php文件或特定于站点的插件中 。

/*
* Define a constant path to our single template folder
*/
define(SINGLE_PATH, TEMPLATEPATH . '/single');

/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_template');

/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;

/**
* Checks for single template by category
* Check by category slug and ID
*/
foreach((array)get_the_category() as $cat) :

if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';

elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';

endforeach;
}

This code first checks to see if WordPress is requesting a single post. If it is, then it tells WordPress to look for the template in /single/ folder of your WordPress theme.

此代码首先检查WordPress是否正在请求一个帖子。 如果是这样,那么它将告诉WordPress在WordPress主题的/ single /文件夹中查找模板。

Now you need to add template files defined by this code.

现在,您需要添加此代码定义的模板文件。

Connect to your WordPress hosting using an FTP client or File Manager in cPanel and go to /wp-content/themes/your-theme-folder/.

使用cPanel中的FTP客户端或文件管理器连接到WordPress托管 ,然后转到/ wp-content / themes / your-theme-folder /。

Inside your current theme folder, you need to create a new folder called ‘single’.

在当前主题文件夹中,您需要创建一个名为“ single”的新文件夹。

Now you need to open this folder and create a new file inside it. Go ahead and name this file single-cat-{category-slug}. Replace {category-slug} with your actual category slug.

现在,您需要打开此文件夹并在其中创建一个新文件。 继续,将此文件命名为single-cat- {category-slug}。 将{category-slug}替换为您的实际类别标签。

For example, if you have a category called ‘News’, then you will create single-cat-news.php file. If you have a category called ‘Travel Tips’, then create a template single-cat-travel-tips.php, and so on.

例如,如果您有一个名为“新闻”的类别,则将创建single-cat-news.php文件。 如果您有一个名为“旅行提示”的类别,请创建一个模板single-cat-travel-tips.php ,依此类推。

WORDPRESS如何更换模板 wordpress自定义文章模板_html_19

Now, these template files will be totally empty. As a starting point, you can copy the contents of your single.php file from your theme folder and paste them inside each of these templates.

现在,这些模板文件将完全为空。 首先,您可以从主题文件夹中复制single.php文件的内容,并将其粘贴到每个模板中。

After that, you can edit these templates to make your desired changes.

之后,您可以编辑这些模板以进行所需的更改。

Once you are done, you can go to your website and view a post. It will use the template that you have created for the category where this post is filed.

完成后,您可以访问您的网站并查看帖子。 它将使用您为帖子所在的类别创建的模板。

Now let’s suppose you have a post filed in two categories News and Travel Tips. WordPress will automatically show the template for ‘News’ because it appears first in alphabetical order.

现在,假设您在“新闻”和“旅行提示”两个类别中发布了帖子。 WordPress将自动显示“新闻”的模板,因为它首先按字母顺序出现。

On the other hand, if you filed a post in a category and didn’t create a template for that category, then WordPress will fallback to the default single.php template of your theme.

另一方面,如果您在某个类别中提交了帖子,但没有为该类别创建模板,则WordPress将回退到主题的默认single.php模板。

(Create Custom Single Post Template for Specific Authors)

Let’s suppose you want posts written by a specific author to look different on your website. You can do that by using the same technique we showed for categories.

假设您希望特定作者撰写的帖子在您的网站上看起来有所不同。 您可以使用与显示类别相同的技术来做到这一点。

First you will need to add this code to your theme’s functions.php file or a site-specific plugin.

首先,您需要将此代码添加到主题的functions.php文件或特定于站点的插件中 。

/**
* Define a constant path to our single template folder
*/
define(SINGLE_PATH, TEMPLATEPATH . '/single');

/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_author_template');

/**
* Single template function which will choose our template
*/
function my_single_author_template($single) {
global $wp_query, $post;

/**
* Checks for single template by author
* Check by user nicename and ID
*/
$curauth = get_userdata($wp_query->post->post_author);

if(file_exists(SINGLE_PATH . '/single-author-' . $curauth->user_nicename . '.php'))
return SINGLE_PATH . '/single-author-' . $curauth->user_nicename . '.php';

elseif(file_exists(SINGLE_PATH . '/single-author-' . $curauth->ID . '.php'))
return SINGLE_PATH . '/single-author-' . $curauth->ID . '.php';

}

Next, you need to connect to your website using FTP or File Manager in cPanel and then go to /wp-content/themes/your-theme-folder/.

接下来,您需要使用cPanel中的FTP或文件管理器连接到您的网站,然后转到/ wp-content / themes / your-theme-folder /。

If you haven’t already created a folder called /single/ inside it, then let’s go ahead and create it now.

如果尚未在其中创建一个名为/ single /的文件夹,那么让我们继续创建它。

Inside this folder, you need to create a template using the author’s username in the template name. For example, single-author-johnsmith.php.

在此文件夹中,您需要使用模板名称中作者的用户名来创建模板。 例如,single-author-johnsmith.php。

This template will be empty, so you can copy and paste the contents of your theme’s single.php template and use it as a starting point.

该模板为空,因此您可以复制并粘贴主题的single.php模板的内容,并将其用作起点。

You can now visit your website to view a post created by the specific author. It will now use the template you created.

现在,您可以访问您的网站以查看由特定作者创建的帖子。 现在它将使用您创建的模板。

That’s all for now.

目前为止就这样了。

We hope this article helped you learn how to create custom single post templates in WordPress. You may also want to see our article on how to create a custom WordPress theme from scratch without writing any code.

我们希望本文能帮助您学习如何在WordPress中创建自定义单个帖子模板。 您可能还想看一下我们的文章, 该文章有关如何从头开始创建自定义WordPress主题而无需编写任何代码。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/

wordpress 自定义