客户需要一个网站来迁移到 WordPress。
在这个网站中,每个页面都有一个侧边栏,里面有不同的内容
在某些页面手风琴位于侧边栏下方,在某些页面中只有文本和图像可见
如何在 WordPress 中实现这一点?
如果必须创建模板,有很多页面无法创建
即使对于每个页面不同的侧边栏小部件也是不可能的
指导我实现这一点的方法
可以通过两个步骤将不同的侧边栏(小部件)添加到每个页面:
functions.php为您的主题注册每个页面的侧边栏在您的主题模板中,在您想要小部件区域的位置添加以下代码:
<?php
global $post;
dynamic_sidebar( 'widget_area_for_page_'.$post->post_name );
?>
Run Code Online (Sandbox Code Playgroud)
在functions.php您的主题中添加以下代码块来注册站点中每个页面的侧边栏。请注意,它包括草稿页面等,以便您可以在草稿模式下编辑小部件。
function myTheme_registerWidgetAreas() {
// Grab all pages except trashed
$pages = new WP_Query(Array(
'post_type' => 'page',
'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit'),
'posts_per_page'=>-1
));
// Step through each page
while ( $pages->have_posts() ) {
$pages->the_post();
// Ignore pages with no slug
if ($pages->post->post_name == '') continue;
// Register the sidebar for the page. Note that the id has
// to match the name given in the theme template
register_sidebar( array(
'name' => $pages->post->post_name,
'id' => 'widget_area_for_page_'.$pages->post->post_name,
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
}
}
add_action( 'widgets_init', 'myTheme_registerWidgetAreas' );
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
有一个很棒的插件:
https://wordpress.org/plugins/custom-sidebars/
有时需要在某些帖子或页面的侧边栏上显示不同的元素。现在的主题为您提供了一些放置小部件的区域,但这些区域对于使用相同模板的所有帖子都是通用的。注意:您需要使用接受小部件的主题才能使该插件正常工作。

我想这就是你要找的。
| 归档时间: |
|
| 查看次数: |
19074 次 |
| 最近记录: |