在本机搭建 wordpress 的一些问题

这里主要是记录一下在本地搭 blog 过程中遇到的一些问题。

这个 blog 使用的是的 wordpress 3.4.x 汉化版,插件有 google xml sitemap,wp-pagenumbers(翻页插件),wp-syntax(代码高亮)。

导入导出最好使用 cpanel 中的备份功能,如果使用 wordpress自带的导入导出功能(wordpress_importer),再次导入的时候反斜杠会丢掉!

安装apache,php和mysql

安装主题

把主题解压到 $WP_ROOT/wp-content/themes/主题名/ 下即可在“外观-主题”中找到。有时用 ftp 上传会出现错误。

修改固定链接

默认的链接名“?p=xxx”很不爽,也不利于搜索引擎检索。可以在“设置-固定链接”中修改成自己想要的格式,这个功能需要 apache 的 mod_rewrite 模块。

这里要提一下的是在本地搭建的时候可能还没有 .htaccess 文件。文件的位置应该是 wordpress 的根目录,也就是和 wp-config.php 同一层。把 .htaccess 文件修改权限为 777,然后在控制版中的“设置-固定链接”修改,保存文件,为防止误改,最后再把权限改回 644。

启用 mod_rewrite

在 /etc/apache2/httpd.conf 中加入一句:

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

然后再在 /etc/apache2/sites-available/default 中找到 $WP_ROOT 的“Directory $WP_ROOT”项,修改其中的“AllowOverride”为“All”,重启 apache 即可。

最近评论中不显示自己的评论(2012.09.15 更新)

修改 wp-includes/default-widgets.php,找到 class WP_Widget_Recent_Comments 中的 function widget(),把

$comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish' ) ) );

改为

$comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'user_id' => 0 ) ) );

备注一下:4.5.3 版本已经能够显示了。

禁止自动生成草稿

参考 这里,修改wp-admin/includes/post.php中的function get_default_post_to_edit(),找到

        if ( $create_in_db ) {
                $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
                $post = get_post( $post_id );
                if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )          
                        set_post_format( $post, get_option( 'default_post_format' ) );
        } else {

把其中的 if 判断改为

        if ( $create_in_db ) {
               // no auto-draft
               global $current_user;
               global $wpdb;
               $post_auto_draft = $wpdb->get_row( "SELECT * FROM $wpdb->posts WHERE post_status = 'auto-draft' AND post_author = $current_user->ID ORDER BY ID ASC LIMIT 1" );
               if ($post_auto_draft) {
                  $post = $post_auto_draft;
               } else {
                  $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
                  $post = get_post( $post_id );
               }

                if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
                        set_post_format( $post, get_option( 'default_post_format' ) );
        } else {

禁止某些字符的替换

我喜欢用 ascii 画图,但是 wordpress 会自作聪明地把连续的横线('---')替换成“&8212;”,这样很多图都没法看了。要去掉这些替换,修改 wp-includes/formatting.php 中的 function wptexturize(),找到

                $static_characters = array_merge( array( '---', ' -- ', '--', ' - ', 'xn–', '...', '``', '\'\'', ' (tm)' ), $cockney );
                $static_replacements = array_merge( array( $em_dash, ' ' . $em_dash . ' ', $en_dash, ' ' . $en_dash . ' ', 'xn--', '…', $opening_quote, $closing_quote, ' ™' ), $cockneyreplace );

把 array 中不需要替换的字符去掉就行了。(4.5.3 版本已经能自动处理了)

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注