去掉WordPress的文章页面的侧边栏

WordPress最吸引我的地方就是它的高自由度了。回想起以前坑爹的百度空间,一堆的广告无法屏蔽,页面几乎无法修改,最后连表格编辑的功能都没有,更别提直接写HTML代码了……果断抛弃之。

WP就不一样了,想要什么功能——装插件,主题不喜欢——搜,更重要的:页面和想象的不一样——没问题,改!

那么这里就记录一下最近自定义WP时去掉侧边栏所改动的东西,以免以后换主题了又忘了怎么改的了。

直接进入主题编辑页面,找到single.php,那么就是下面这些东西:

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package Superhero
 * @since Superhero 1.0
 */

get_header(); ?>


<!-- End Comments --> <nav class="post-nav"> <ul class="pager"> <li class="previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '←', 'Previous post link', 'wpstraphero' ) . '</span> %title' ); ?></li> <li class="next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '→', 'Next post link', 'wpstraphero' ) . '</span>' ); ?></li> </ul> </nav><!-- .nav-single --> </div><!-- #content .site-content -->


</div><!-- end row --> <?php get_footer(); ?>

哦哦,原来SideBar是在这里控制的。不过直接把span4的div删掉是不行的,因为这样会在右边留下一大片空白,那么就只好直接修改CSS了?

不过还好这个主题正好提供了一个Full-width, No sidebar的模板,但是貌似在设置里不能直接设置,所以应该是需要我们手动修改PHP的页面了。直接把single.php替换成如下内容(最好把原来的东西备份一下):

<?php 
/**
 * Template Name: Full-width Page Template, No Sidebar
*/

get_header(); ?>
<!-- Marketing messaging and featurettes
    ================================================== -->
    <!-- Wrap the rest of the page in another container to center all the content. -->




<!-- Closes the first div --> <!-- Stop The Loop (but note the "else:" - see next line). --> <?php endwhile; else: ?> <!-- The very first "if" tested to see if there were any Posts to --> <!-- display. This "else" part tells what do if there weren't any. -->


<!--End the loop --> <?php endif; ?> </div> <!-- Begin Comments -->


<!-- End Comments --> </nav><!-- .nav-single --> </div> <!-- end row --> </div> <!-- end container --> <?php get_footer(); ?>

替换了之后页面果然变成全宽的了。不过有一个问题,就是原先前后文章的导航不见了。没关系,原先的single.php中有一段previous_post_link和next_post_link的代码,把前后导航的代码插入到Comments前面(不要放到div标签里面去了,否则按钮就在框框里面了):

	<nav class="post-nav">
            <ul class="pager">
	        <li class="previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '←', 'Previous post link', 'wpstraphero' ) . '</span> %title' ); ?></li>
	        <li class="next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '→', 'Next post link', 'wpstraphero' ) . '</span>' ); ?></li>
            </ul>
        </nav><!-- .nav-single -->

当然也可以把它丢在页面开始,不过显得比较乱就是了,而且一般人都是看完了才会去看下一篇吧……

✏️ 有任何想法?欢迎发邮件告诉老夫:daozhihun@outlook.com