In WordPressAdd custom sidebars for different pages or posts, which can help improve the flexibility and user experience of a website. By configuring exclusive sidebars for different types of content, it is possible to display more relevant information, such as customized widgets, recommended content, advertisements, and so on. Two ways to implement this will be described below: through plugins to simplify the operation, or using custom code to increase flexibility.
![图片[1]-如何为每个 WordPress 页面或帖子添加不同的侧边栏-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/09/2024090406143487.jpg)
Method 1: Use of plug-ins (recommended)
Using plug-ins is the most convenient way, especially for users who are not familiar with programming, plug-ins can provide flexible condition settings and an easy-to-operate interface. The following will focus on how to use Content Aware Sidebars plugin to enable different pages or posts to have their own sidebars.
Step 1: Install and activate the plugin
- Log in to the WordPress backend.
- In the left menu, click Plugins > Install New PluginThe
- Type in the search box Content Aware SidebarsWhen you find the plug-in, click on the mountingand then click activateThe
![图片[2]-如何为每个 WordPress 页面或帖子添加不同的侧边栏-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/09/2024090406153839.png)
Step 2: Create a Custom Sidebar
When the plugin is activated, a new option appears in the left menu Sidebars. We will go through the following steps to create a custom sidebar for some specific pages or posts.
- In the WordPress backend, click Sidebars > Add New, start creating a new sidebar.
![图片[3]-如何为每个 WordPress 页面或帖子添加不同的侧边栏-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/09/2024090406185521.png)
- Sidebar Name: Name the new sidebar. For ease of administration, it is recommended that the naming include the name of the page or post, e.g. "About Us Sidebar".
- Conditions: This is the core functionality of the plugin and allows you to set display rules for each page, post, category or other condition.
![图片[4]-如何为每个 WordPress 页面或帖子添加不同的侧边栏-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/09/2024090406220670.png)
- Post Type: Select which page types or post types to display the sidebar on. For example, you can select Pages, and then further select a specific page, such as "About Us".
![图片[5]-如何为每个 WordPress 页面或帖子添加不同的侧边栏-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/09/2024090406230938.png)
- Taxonomies: It is possible to have the same sidebar for all posts in a category or tag.
![图片[6]-如何为每个 WordPress 页面或帖子添加不同的侧边栏-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/09/2024090406251953.png)
- Date: You can also set the sidebar to be displayed on a specific date or time period for events, promotions and other scenarios.(for) instance::
- Let's say you want to make a case for "About Us"The page creates a unique sidebar that you can use in the Post Type > Pages Select "About Us" page to ensure that this sidebar is only displayed on that page.
![图片[7]-如何为每个 WordPress 页面或帖子添加不同的侧边栏-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/09/2024090406285835.png)
- When the configuration is complete, click Create Perform the creation.
Step 3: Assign Widgets to Customized Sidebars
- After saving the new sidebar, go to Appearance > Gadgets(Appearance > Widgets).
- You will see the customized sidebar you just created on the widget page (e.g. "About Us Sidebar").
![图片[8]-如何为每个 WordPress 页面或帖子添加不同的侧边栏-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/09/2024090406334041.png)
- Drag and drop the desired widgets (e.g. text, menus, social links, etc.) into that sidebar.draw attention to sth.: You can add different widgets depending on the needs of different pages, making sure that each page's sidebar content is relevant to its main content.
- strike (on the keyboard) Save Save.
Step 4: View Results
After completing the above steps, you can go to the front-end of the website and browse the set pages or posts. The new customized sidebar will be displayed automatically based on the set criteria, while other pages will still display the default sidebar.
Advantages of Plug-ins and Tips for Using Them
- Conditional logic is powerful: The plugin's conditional settings allow you the flexibility to display different sidebars in various situations. Whether it's by page, category, user role, or date setting, you can easily manage your site's sidebar layout.
- No need to write code: Even if you don't know how to code, you can use the plugin to complete complex sidebar configurations.
- Compatible with most themes::Content Aware Sidebars The plugin works with most WordPress themes without causing conflicts or breaking page layouts.
- Advanced Settings: Multiple conditions can be combined to create more granular display rules. For example, a sidebar can be set for all articles belonging to a particular category, while other unique sidebars can be added to some of these specific pages.
- Import/export function: If you have multiple sites, or need to reuse the sidebar settings on different sites, the plugin provides theImport/Exportfeatures that save a lot of time.
Method 2: Use customized code
If you have some knowledge of the code, or want to control the display of the sidebar more flexibly, you can modify the theme files to realize different pages or posts load different sidebars. Here are the specific steps:
Step 1:Creating subtopics
Directly modifying the theme files may result in losing your custom modifications when the theme is updated. To avoid this, it is recommended to create a child theme.
![图片[9]-如何为每个 WordPress 页面或帖子添加不同的侧边栏-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/09/2024090406494054.png)
Step 2: Modify the template file
Find the current topic in the single.php
file (for displaying individual posts) or page.php
file (used to display the page), add conditional statements to dynamically load different sidebars based on the ID or slug of the page or post.
![图片[10]-如何为每个 WordPress 页面或帖子添加不同的侧边栏-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/09/2024090406521182.png)
Sample code:
phpCopy code
if (is_single('your-post-slug')) {
get_sidebar('custom-sidebar');
} else {
get_sidebar();
}
In the code above:
your-post-slug
is the slug for the particular post you wish to apply the custom sidebar to.custom-sidebar
is the custom name that this sidebar is named after.
Next, you need to create a theme directory named sidebar-custom-sidebar.php
file, which is your custom sidebar template. In this file, you can add any HTML or PHP code to define the content of the sidebar.
Step 3: Using Conditional Labels
![图片[11]-如何为每个 WordPress 页面或帖子添加不同的侧边栏-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/09/2024090406575074.png)
WordPress provides various conditional tags to help you load different sidebars depending on the page or post. If you want to load a sidebar file (sidebar.php
) in which sidebars are loaded dynamically according to different pages, here are some common conditional tags:
is_page()
: Checks if the current page is a particular page.is_single()
: Checks if the current article is a particular article.is_category()
: Checks if the current page is a certain category.is_tag()
: Checks if the current page is a certain tagged page.
For example, you can use the following code to load different sidebars for different categories:
phpCopy code
if (is_category('news')) {
get_sidebar('news-sidebar');
} else if (is_category('blog')) {
get_sidebar('blog-sidebar'); } else { if (is_category('blog')) { get_sidebar('blog-sidebar')
} else {
get_sidebar();
}
Step 4: Save and View Results
After modifying the template file, save the changes and view the site. Based on the conditions you set, the page or post will load the appropriate custom sidebar.
summarize
Through plug-ins such as Content Aware SidebarsThis program allows users to flexibly add different sidebars to each WordPress page or post without writing code, and is suitable for webmasters who do not have programming knowledge. If you have some understanding of programming, you can also customize the code to manually configure different sidebar display rules.
Link to this article:https://www.361sale.com/en/18692The article is copyrighted and must be reproduced with attribution.
No comments