如何告诉Wordpress类别页面调用特定的侧边栏

发布于 2024-08-18 04:42:24 字数 306 浏览 4 评论 0原文

我正在尝试引入特定于该类别的样式侧边栏。我有以下有效的代码,但它同时拉入了我的新侧边栏和默认侧边栏。我在这里做错了什么?

来自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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离去的眼神 2024-08-25 04:42:24

因为你调用侧边栏两次;这样做:

<?php

if ( in_category('39') ) {
include(TEMPLATEPATH . '/sidebar2.php');

} else {
include(TEMPLATEPATH . '/sidebar.php');

}
?>

Because you're calling sidebar twice; do this:

<?php

if ( in_category('39') ) {
include(TEMPLATEPATH . '/sidebar2.php');

} else {
include(TEMPLATEPATH . '/sidebar.php');

}
?>
醉城メ夜风 2024-08-25 04:42:24

提供给您的代码有一个小问题。但正如 Eimantas 所建议的,您可以简单地创建一个名为category-39.php 的新文件,它将完美地完成这项工作,如果由于某种原因您仍然想继续使用您的category.php 文件,那么这就是您需要的待办事项:

if ( is_category('39') ) {
  get_sidebar('2');
} else {
  get_sidebar();
}
?>
<?php get_footer(); ?>

此代码与您发布的代码之间的区别在于我已删除

<?php get_sidebar(); ?>

此外,我已将 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('2');

get_sidebar

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:

if ( is_category('39') ) {
  get_sidebar('2');
} else {
  get_sidebar();
}
?>
<?php get_footer(); ?>

The difference between this and the code that you posted is that I have removed

<?php get_sidebar(); ?>

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

get_sidebar('2');

and

get_sidebar();

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,

私藏温柔 2024-08-25 04:42:24

从你的代码中删除它:

get_sidebar();

否则你会调用这个文件“sidebar.php”两次......

如果你查看wp文档 http://codex.wordpress.org/Function_Reference/get_sidebar

你可以这样做:

<?php
if ( in_category('39') ) :
    get_sidebar('two'); //this will include sidebar-two.php
else :
    get_sidebar();
endif;
?>

Remove this from your code:

get_sidebar();

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:

<?php
if ( in_category('39') ) :
    get_sidebar('two'); //this will include sidebar-two.php
else :
    get_sidebar();
endif;
?>
清风疏影 2024-08-25 04:42:24

您应该创建名为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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文