如何告诉Wordpress类别页面调用特定的侧边栏
我正在尝试引入特定于该类别的样式侧边栏。我有以下有效的代码,但它同时拉入了我的新侧边栏和默认侧边栏。我在这里做错了什么?
来自category.php
<?php get_sidebar();
if ( in_category('39') ) {
include(TEMPLATEPATH . '/sidebar2.php');
} else {
include(TEMPLATEPATH . '/sidebar.php');
}
?>
<?php get_footer(); ?>
I am trying to pull in a styled sidebar specific to the category. I have the following code which works, but it is pulling in BOTH my new sidebar and the default. What am I doing wrong here?
From category.php
<?php get_sidebar();
if ( in_category('39') ) {
include(TEMPLATEPATH . '/sidebar2.php');
} else {
include(TEMPLATEPATH . '/sidebar.php');
}
?>
<?php get_footer(); ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为你调用侧边栏两次;这样做:
Because you're calling sidebar twice; do this:
提供给您的代码有一个小问题。但正如 Eimantas 所建议的,您可以简单地创建一个名为category-39.php 的新文件,它将完美地完成这项工作,如果由于某种原因您仍然想继续使用您的category.php 文件,那么这就是您需要的待办事项:
此代码与您发布的代码之间的区别在于我已删除
此外,我已将 in_category 更改为 is_category。这样做的原因是因为当使用 is_category 查看类别页面本身时,类别列表上会发生变化,而 in_category 只查看当前帖子,因此除了查看 single.php 页面外不会发生变化。
例子:
in_category 将更改以下网址 www.mysite.com/category/stuff/myfirstpost 的侧边栏
但它不会更改此网址 www.mysite.com/category/stuff 的侧边栏
只需使用 is_category 即可解决此问题。
接下来的事情是使用
get_sidebar
();除了包含 sidebar.php 之外,还将运行与侧边栏相关的适当的 WordPress 功能。
get_sidebar('2');另一方面将运行与侧边栏相关的所有适当的 WordPress 功能,同时还会加载 sidebar-2.php。
希望这有帮助,
There is a slight problem with the code being provided to you. But as Eimantas suggested, you could simply make a new file called category-39.php which will accomplish the job perfectly, if though for some reason you are still wanting to continue using your category.php file, then here is what you would need to do:
The difference between this and the code that you posted is that I have removed
Additionally I have changed in_category to is_category. The reason for this is because when looking at the category page itself using is_category will change on the category list, whereas in_category only looks at the current post and therefore will not change according except when looking at the single.php page.
Example:
in_category will change the sidebar for the following url www.mysite.com/category/stuff/myfirstpost
But it will not change the sidebar for this url www.mysite.com/category/stuff
Simply using is_category will fix this problem.
The next thing would be using
and
get_sidebar(); will run the appropriate wordpress functions related to the sidebar in addition to including sidebar.php.
get_sidebar('2'); on the other hand will run all the appropriate wordpresss functions related to the sidebar while also loading sidebar-2.php.
Hope this helps,
从你的代码中删除它:
否则你会调用这个文件“sidebar.php”两次......
如果你查看wp文档 http://codex.wordpress.org/Function_Reference/get_sidebar
你可以这样做:
Remove this from your code:
otherwise you call this file "sidebar.php" twice...
if you look into wp documentation http://codex.wordpress.org/Function_Reference/get_sidebar
you can do it like that:
您应该创建名为category-39.php 的单独模板并进行常见的设计工作。 WP 本身会注意到它应该将此模板应用于 id=39 的类别。不需要 if else 语句。
You should create separate template named category-39.php and do common design stuff. WP itself will notice that it should apply this template to category with id=39. No need for if else statements.