# 对 WordPress 2012 主题的一些改动

- Link: https://www.ixiqin.com/2022/01/30/some-changes-to-the-wordpress-2012/
- Published: 2022-01-30T00:13:01+08:00
- Author: 白宦成

文章目录

 [Toggle](https://www.ixiqin.com/2022/01/30/some-changes-to-the-wordpress-2012/#)

 * [加入广告信息](https://www.ixiqin.com/2022/01/30/some-changes-to-the-wordpress-2012/#%e5%8a%a0%e5%85%a5%e5%b9%bf%e5%91%8a%e4%bf%a1%e6%81%af)
 * [首页显示摘要，而非全文](https://www.ixiqin.com/2022/01/30/some-changes-to-the-wordpress-2012/#%e9%a6%96%e9%a1%b5%e6%98%be%e7%a4%ba%e6%91%98%e8%a6%81%ef%bc%8c%e8%80%8c%e9%9d%9e%e5%85%a8%e6%96%87)
 * [摘要显示超过默认长度的数量](https://www.ixiqin.com/2022/01/30/some-changes-to-the-wordpress-2012/#%e6%91%98%e8%a6%81%e6%98%be%e7%a4%ba%e8%b6%85%e8%bf%87%e9%bb%98%e8%ae%a4%e9%95%bf%e5%ba%a6%e7%9a%84%e6%95%b0%e9%87%8f)
 * [加入面包屑展示](https://www.ixiqin.com/2022/01/30/some-changes-to-the-wordpress-2012/#%e5%8a%a0%e5%85%a5%e9%9d%a2%e5%8c%85%e5%b1%91%e5%b1%95%e7%a4%ba)

在上一篇文章当中，我介绍了[我为什么要迁移到 2012 这个主题](https://www.ixiqin.com/2022/01/why-give-up-the-wordpress-2022-theme/)，
在这篇文章中，我将会向你介绍一下我对其做的一些修改。

## 加入广告信息

之前我接受了来自芦笋的广告赞助，作为权益，我答应为芦笋提供相应的广告展出，因此，
我需要通过一些代码的修改，来实现对这部分内容的修改。

具体的修改方式是修改了 WordPress 2012 主题中的 `content.php` 这个文件,在合适的位置
加入如下的代码

    ```shcb-line-numbers
    <!-- ads code start -->
    <?php if(is_single()){ ?>
    <a target="_blank" href="https://lusun.com/invite/20143" style="padding-bottom:10px"><img src="http://ixiqin.test/wp-content/uploads/2022/01/800498db4ecc3ecade82c7dfb0aaded5.png" class="wp-block-image" alt="白宦成邀请你注册芦笋，并赠送你 30 天高级版特权" style="max-width:100%"></img></a>
    <?php }?>
    <!-- ads code end -->
    Code language: HTML, XML (xml)
    ```

文章页面的广告便是如此加上去的。

而侧边栏中的广告代码，则是通过 WordPress 自带的「小工具」功能来实现的，插入图片，
并加入对应的链接即可。

## 首页显示摘要，而非全文

从阅读体验上来讲，在列表页面能看完所有内容自然是不错的，不过因为我的文章比较喜欢
插入代码和图片，如果直接在文章页面展示所有内容，会导致页面看起来非常的混乱，所以
我修改了对应的代码，将其调整为在首页展示摘要，而在内容详情页展示全文。

这里 2012 主题并未加入相对应的功能的开关，因此需要自行代码实现相应的功能。

原代码如下：

    ```shcb-line-numbers
    <?php if ( is_search() ) : // Only display excerpts for search. ?>
    <div class="entry-summary">
    	<?php the_excerpt(); ?>
    </div><!-- .entry-summary -->
    <?php else : ?>
    <div class="entry-content">
    	<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?>
    	<?php
    	wp_link_pages(
    		array(
    			'before' => '<div class="page-links">' . __( 'Pages:', 'twentytwelve' ),
    			'after'  => '</div>',
    		)
    	);
    	?>
    </div><!-- .entry-content -->
    <?php endif; ?>
    Code language: HTML, XML (xml)
    ```

新的代码如下：

    ```shcb-line-numbers
    <?php if (is_search()): // Only display excerpts for search.
    ?>
            <div class="entry-summary">
                <?php the_excerpt(); ?>
            </div><!-- .entry-summary -->
            <?php
    else: ?>
            <?php if (is_single()): ?>
        <div class="entry-content">
                <?php the_content(__('Continue reading <span class="meta-nav">&rarr;</span>', 'twentytwelve')); ?>
                <?php
            wp_link_pages(array('before' => '<div class="page-links">' . __('Pages:', 'twentytwelve'), 'after' => '</div>',));
    ?>
            </div><!-- .entry-content -->
    <?php
        else: ?>
    <div class="entry-summary">
                <?php the_excerpt(); ?>
            </div><!-- .entry-summary -->
    <?php
        endif; // is_single()
    ?>
            <?php
    endif; ?>
    Code language: HTML, XML (xml)
    ```

通过上述的代码实现，来控制了在首页 & 列表页面只显示摘要，而在具体的内容页面，展示
全部内容。

## 摘要显示超过默认长度的数量

WordPress 默认的摘要长度是 55 ，而我写的内容如果只是默认的 55 ，可能看起来会比较
奇怪，所以我会通过代码，将其调整为 100

    ```php
    function custom_excerpt_length( $length ) {
        return 100;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
    Code language: PHP (php)
    ```

## 加入面包屑展示

加入面包屑展示，有助于读者更好的在站内进行不同的导航，因此，借助于 All in One SEO
插件自带的面包屑功能，加入了面包屑。

    ```shcb-line-numbers
    <div id="aioseo_breadcrumbs" style="padding: 10px 0px;">
    <?php if( function_exists( 'aioseo_breadcrumbs' ) ) aioseo_breadcrumbs(); ?>
    </div>
    Code language: HTML, XML (xml)
    ```

## Comments

### By: 青山

- Link: https://www.ixiqin.com/2022/01/30/some-changes-to-the-wordpress-2012/#comment-3650
- Published: 2023-11-27T13:02:06+08:00

现在似乎又是全文加载了。

### By: 白宦成

- Link: https://www.ixiqin.com/2022/01/30/some-changes-to-the-wordpress-2012/#comment-3651
- Published: 2023-11-27T14:28:50+08:00

是的，主题升级后就会自动干掉修改的部分，打算过几天写个插件来控制全文 / 摘要。

### By: 青山

- Link: https://www.ixiqin.com/2022/01/30/some-changes-to-the-wordpress-2012/#comment-3652
- Published: 2023-11-27T16:41:56+08:00

搞个子主题，就没那么麻烦了。

### By: 白宦成

- Link: https://www.ixiqin.com/2022/01/30/some-changes-to-the-wordpress-2012/#comment-3653
- Published: 2023-11-27T17:20:44+08:00

可能还是做个插件更完整一点。子主题做的是一次性工作，做成插件更合理。
