【问题标题】:Custom sidebar in single.phpsingle.php 中的自定义侧边栏
【发布时间】:2012-08-12 18:08:26
【问题描述】:

我使用此代码在 wordpress 中显示自定义 single.php 页面(按类别) 而且效果很好。

 get_header(); ?>

        <div id="container">
            <div id="content" role="main">

            <?php
              $post = $wp_query->post;

              if (in_category('4')) {
                  include(TEMPLATEPATH.'/loop-single-recept.php');
              } elseif (in_category('7')) {
                  include(TEMPLATEPATH.'/loop-single.php');
              } else {
                  include(TEMPLATEPATH.'/loop-single.php');
              }
                ?>


            </div><!-- #content -->
        </div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

但我也想在每个 single.php 页面中显示自定义侧边栏。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以创建多个侧边栏文件,然后使用&lt;?php get_sidebar() ?&gt; 中的(通常为空)参数调用它们

    例如,您创建一个替代侧边栏并将其命名为 sidebar-alternative.php,然后使用 &lt;?php get_sidebar('alternative'); ?&gt; 调用它

    所以你的代码可能是;

            <?php
    
              if (in_category('4')) {
                  get_sidebar();
              } elseif (in_category('7')) {
                  get_sidebar('alternative');
              } else {
                  get_sidebar('alternative');
              }
    
             ?>
    

    顺便说一句,我不是在做问题,但你应该考虑接受你的一些答案,因为这会让人们无法回答 - Stackoverflow Answers

    【讨论】:

      【解决方案2】:

      有时 in_category() 似乎不起作用,我喜欢从 $wp_query 变量中获取类别名称并包含我自己的文件:

      if($wp_query->query['category_name'] == 'your-category-name-1') {
      
          include_once('sidebar-one.php');
      
      } else if($wp_query->query['category_name'] == 'your-category-name-2') {
      
          include_once('sidebar-two.php');
      
      } else {
      
          include_once('sidebar-three.php');
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        相关资源
        最近更新 更多