分类导航

最新文章

按月份

wordpress技巧不完全收集

目录
[隐藏]

摘要

平时收集的一些wordpress长用技巧,有模板、数据库、功能等方面的。

按分类提取文章

必须将$posts暂时保存起来,否则,提取的过程会破坏页面本身的数据。

<?php
$tmp_posts = $posts;
$posts = get_posts( "category=4&numberposts=10" ); ?>
<?php if( $posts ) : ?>
<ul>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endforeach; ?>
</ul>
<?php endif;
$posts = $tmp_post;
?>

禁用文章版本管理

方法:打开 WordPress 根目录下的 wp-config.php 文件,添加如下行即可:

define('WP_POST_REVISIONS', false);

禁用版本管理功能以后,之前已经生成的版本记录还是存在的,通过以下 SQL 语句可以删除:

DELETE FROM wp_postmeta WHERE post_id IN (SELECT id FROM wp_posts WHERE post_type = 'revision');
DELETE FROM wp_term_relationships WHERE object_id IN (SELECT id FROM wp_posts WHERE post_type='revision');
DELETE FROM wp_posts WHERE post_type='revision';

每页显示数量

<?php $posts = query_posts($query_string . '&orderby=date&showposts=5');?>

还可以添加条件判断,如下:当页面为分类列表页时,显示15条。

<?php
if (is_category()) {
    $posts = query_posts($query_string . '&orderby=date&showposts=15');
}
?>

批量替换文章内链接地址

当域名进行更改时,可以使用此SQL语句。

UPDATE `{PREFIX}_posts` SET `post_content`=replace( `post_content` , '{FIND}', '{REPLACE}') WHERE `post_content` LIKE '%{FIND}%';

分页显示评论

类似于插件pagenavi的样子,显示多页索引

<?php
if (get_option('page_comments')) {
    $comment_pages = paginate_comments_links('echo=0');
    if ($comment_pages) {
?>
        <div class="navigation">
            <?php echo $comment_pages; ?>
        </div>
<?php
    }
}
?>

提取文章自定义域

已有the_post时:

post_custom('名称');

单独提取某文章的所有自定义域

get_post_custom( $post_id );

文章列表同时排除置顶的

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$sticky=get_option('sticky_posts');
$args=array(
    'cat'=>$cat,
    'caller_get_posts'=>1,
    'post__not_in' => $sticky,
    'paged'=>$paged,
);
query_posts($args);

提取文章中的图片

<?php
if (have_posts()) :
while (have_posts()) : the_post();
    preg_match_all( '/<img[^\>]*\ />/', $post->post_content, $aPics );
    $numOfPics = count($aPics[0]);
    if ( $numOfPics > 0 )
        foreach ( $aPics[0] as $img )
            $imga[]=$img;
    endwhile;
endif;
?>

定义自己的搜索框

通过filter滤镜将表单的结构重置,注意:字段名不可以改变,就是所有的name属性和值都要保留。
打开主题文件夹中的functions.php,没有请自行创建,添加以下代码

function my_search_form($form) {
    $form = '<div><div class="searchform" id="searchform"><form method="get" action="' . get_option('home') . '/" >
    <label class="hidden" for="s">' . __('Search for:') . '</label>
    <input type="text" autocomplete="off" value="' . attribute_escape(apply_filters('the_search_query', get_search_query())) . '" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="'.attribute_escape(__('Search')).'" />
    </form></div></div>';
    return $form;
}
add_filter('get_search_form', 'my_search_form');

如果已经安装了某些表单插件或者做过处理,可以将add_filter添加一个参数,如下

add_filter('get_search_form', 'my_search_form', 200);

自定义评论模板

wordpress中的自带方法wp_list_comments提供了回调,可以根据自己的需求来调整评论模版的结构或样式。
首先在 WordPress 主题的 functions.php 中添加对评论样式定义的 mytheme_comment 函数,默认值如下:

function mytheme_comment($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment;
?>
    <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
    <div id="comment-<?php comment_ID(); ?>">
        <div class="comment-author vcard">
            <?php echo get_avatar($comment,$size='32',$default='<path_to_url>' ); ?>
            <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
        </div>
        <?php if ($comment->comment_approved == '0') : ?>
            <em><?php _e('Your comment is awaiting moderation.') ?></em><br />
        <?php endif; ?>
        <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','') ?></div>
        <?php comment_text() ?>
        <div class="reply">
            <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
        </div>
    </div>
<?php
}

调用方法:在 WordPress 主题的 comments.php 中用 callback 回调函数引入之前定义的 mytheme_comment 函数

<ul class="commentlist">
    <?php wp_list_comments('type=comment&callback=mytheme_comment'); ?>
</ul>