1

好的,我刚刚使用指南开发了我的第一个自定义 Wordpress 主题 - 我仍然是菜鸟。我没有使用小部件来填充侧边栏,我想用我自己的代码和图像自己填充它。

所以假设我希望我制作的第一个侧边栏只显示在主页上,然后我制作的第二个侧边栏显示在其他所有页面上。我可以使用 if 语句吗?

<?php

if(is_home()){

echo

"<div class="row">
            <div class="span3">
                <div id="search-box"><img id="search_text" src="http://localhost/Dorsa/wp-content/themes/Bootstrap/img/text_findyourhome.png">
                </div>
            </div>
        </div>


        <div class="row">
            <div class="span3">
                <img class="box_shadow" src="http://localhost/Dorsa/wp-content/themes/Bootstrap/img/box-shadow.png" style="padding-left:0;">
            </div>
        </div>

        <div class="row">
            <div class="span3"><div id="news" style="margin-top:275px;margin-bottom:200px">News</div></div>
         </div>"
         }

         else
         {
         echo
         "sidebar 2 code - didn't write it yet"
         }       
?>
4

2 回答 2

1

如果您只想在主页中显示一个侧边栏,而在其余页面中显示另一个侧边栏,则可以使用 @ChrisHerbert 所说的 if 语句。新的解决方案是创建一个新的模板和侧边栏。

  1. 首先找到您的sidebar.php文件并复制它。
  2. 使用wordpress 命名约定命名侧边栏,例如:sidebar-secondary.php.
  3. 根据您的需要在 sidebar-secondary.php 中进行更改。
  4. 制作一个新模板并将您的侧边栏用作<?php get_sidebar(‘secondary’); ?>

编辑

假设您想要一个侧边栏作为主页并为所有其他侧边栏分开,您可以执行以下操作。

  1. sidebar-secondary.php如上所述创建。
  2. 主模板通常是index.php. 这通常包含您主页的侧边栏。你会发现一些类似的代码<?php get_sidebar() ?>
  3. 如果您想在页面上显示辅助边栏,请打开您的页面模板page.php。找到该行get_sidebar()并将其替换为get_sidebar('secondary'). 现在您的所有页面都将使用辅助边栏。如果您的任何页面使用不同的模板,您需要执行相同的操作。
  4. 要在您的单个帖子页面(用户单击博客部分中的 readmore 后显示的页面)中显示辅助侧边栏,请打开single.php并再次查找并替换get_sidebar()get_sidebar('secondary').

您现在可以为主页和其他页面设置不同的样式和使用侧边栏。

请注意,如果您只希望主页中的侧边栏不同而页面的其余部分相同,您也可以在主模板文件中使用条件index.php

if( is_home() ){
 get_sidebar(); //your main sidebar for homepage
} else {
 get_sidebar('secondary'); //your sidebar for other pages
}
于 2013-08-02T03:41:29.983 回答
0

模板绝对是要走的路,但是假设您的“主页”页面是您的博客页面,您的解决方案应该可以工作。如果您已将主页设置为静态页面(在仪表板的“阅读”部分中),则应使用该is_front_page()功能而不是is_home().

我还建议使用替代语法并在标记之前关闭 PHP 标记,如下所示:

<?php if ( is_home() ) : ?>

    <div class="row">
        <div class="span3">
            <div id="search-box"><img id="search_text" src="http://localhost/Dorsa/wp-content/themes/Bootstrap/img/text_findyourhome.png">
            </div>
        </div>
    </div>

    <div class="row">
        <div class="span3">
            <img class="box_shadow" src="http://localhost/Dorsa/wp-content/themes/Bootstrap/img/box-shadow.png" style="padding-left:0;">
        </div>
    </div>

    <div class="row">
        <div class="span3"><div id="news" style="margin-top:275px;margin-bottom:200px">News</div></div>
    </div>

<?php else: ?>

    <!-- sidebar 2 code - didn't write it yet" -->

<?php endif; ?>
于 2013-08-02T03:30:48.407 回答