wordpress主题制作常用到的方法函数

页面编码:<meta charset=”<?php bloginfo( ‘charset’ ); ?>” />;
页面标题:wp_title( ‘|’, true, ‘right’ );
博客名称:bloginfo( ‘name’ );
博客描述:<?php bloginfo( ‘description’ ); ?>
博客样式:<link rel=”stylesheet” media=”all” href=”<?php bloginfo( ‘stylesheet_url’ ); ?>” />
博客主页地址:<a href=”<?php echo home_url( ‘/’ ); ?>”></a>
博客主题地址:<link rel=”shortcut icon” type=”image/x-icon” href=”<?php bloginfo(‘template_url’)?>/img-res/favicon.ico” />
博客导航,一步生成 :<?php wp_nav_menu( array( ‘container_class’ => ‘menu-header’, ‘theme_location’ => ‘primary’ ) ); ?>
全局当前页码:global $page, $paged;
wordpress官方列举的全局变量列表: http://codex.wordpress.org/Global_Variables

自定义输出分类:

<?php
    $args=array(
        'orderby' => 'name',
        'order' => 'ASC'
    );
    $categories = get_categories($args);
    foreach($categories as $category) {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
    }
?>

搜索表单一步生成:<?php get_search_form(); ?>

在Loop中自定义文章显示:

<article  id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

    <section>
        <?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?>
            <?php the_excerpt(); ?>
        <?php else : ?>
            <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?>
            <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
        <?php endif; ?>

        <p class="t-c">
            <a class="btn" href="<?php the_permalink(); ?>">继续阅读~</a>
            <a class="btn" href="<?php comments_link(); ?>"><?php comments_number( '参与评论~', '评论(%)', '评论(%)' ); ?></a>
        </p>
    </section>

    <aside>
        <?php edit_post_link( __( '[Edit]', 'twentyten' ), '<span class="r-f">', '</span>' ); ?>
        <time><?php the_time('Y.m.d');?></time>
        <span>分类: <?php the_category(', '); ?></span>
        <span>标签: <?php the_tags('',',',''); ?>
        </span>
    </aside>
</article>

不用插件,自己实现数字显示的分页页码:

<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<p class="page panel-light">
<?php
    $args = array(
        'base'         => '%_%',
        'format'       => '/page/%#%',
        'total'        => 1,
        'current'      => $page,
        'end_size'     => 2,
        'mid_size'     => 5,
        'prev_next'    => false,
     );
    echo paginate_links( $args )
?>
</p><?php endif; ?>

自定义搜索表单,在主题文件夹下新建名为searchform.php的文件,即可替换wp默认表单:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <label class="screen-reader-text" for="s" />搜索:</label>
    <input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
    <input type="submit" id="searchsubmit" title="搜索" value="" />
</form>

分页,不需要插件,利用wordpress自带的函数paginate_links实现分页显示,再封装一下作为新函数,在functions.php中添加:

//自定义的分页函数,可以用于各种页面
function show_pagination() {
    global $wp_query, $wp_rewrite, $paged;
    $current = max(1, $paged); 

    $pagination = array(
        'base' => @add_query_arg('paged','%#%'),
        'format' => '',
        'total' => $wp_query->max_num_pages, //必须,总页数,默认为1是不会显示页码的
        'current' => $current,
        'end_size'     => 2,
        'mid_size'     => 5,
        'prev_next'    => false
    ); 

    if( $wp_rewrite->using_permalinks() )
        $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . 'page/%#%/', 'paged'); 

    if( !empty($wp_query->query_vars['s']) )
        $pagination['add_args'] = array('s'=>get_query_var('s')); 

    if ( $wp_query->max_num_pages > 1 ) :
        echo '<p class="pager panel-light">'.paginate_links($pagination).'</p>';
    endif;
}

获取评论者头像Avatar的图片地址,而不是直接获取标签,修改get_avatar()为get_avatar_url(),加入到functions.php文件:

if ( !function_exists( 'get_avatar_url' ) ) :

function get_avatar_url( $id_or_email, $size = '96', $default = '', $alt = false ) {
	if ( ! get_option('show_avatars') )
		return false;

	if ( false === $alt)
		$safe_alt = '';
	else
		$safe_alt = esc_attr( $alt );

	if ( !is_numeric($size) )
		$size = '96';

	$email = '';
	if ( is_numeric($id_or_email) ) {
		$id = (int) $id_or_email;
		$user = get_userdata($id);
		if ( $user )
			$email = $user->user_email;
	} elseif ( is_object($id_or_email) ) {
		// No avatar for pingbacks or trackbacks
		$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
		if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
			return false;

		if ( !empty($id_or_email->user_id) ) {
			$id = (int) $id_or_email->user_id;
			$user = get_userdata($id);
			if ( $user)
				$email = $user->user_email;
		} elseif ( !empty($id_or_email->comment_author_email) ) {
			$email = $id_or_email->comment_author_email;
		}
	} else {
		$email = $id_or_email;
	}

	if ( empty($default) ) {
		$avatar_default = get_option('avatar_default');
		if ( empty($avatar_default) )
			$default = 'mystery';
		else
			$default = $avatar_default;
	}

	if ( !empty($email) )
		$email_hash = md5( strtolower( $email ) );

	if ( is_ssl() ) {
		$host = 'https://secure.gravatar.com';
	} else {
		if ( !empty($email) )
			$host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) );
		else
			$host = 'http://0.gravatar.com';
	}

	if ( 'mystery' == $default )
		$default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
	elseif ( 'blank' == $default )
		$default = includes_url('images/blank.gif');
	elseif ( !empty($email) && 'gravatar_default' == $default )
		$default = '';
	elseif ( 'gravatar_default' == $default )
		$default = "$host/avatar/s={$size}";
	elseif ( empty($email) )
		$default = "$host/avatar/?d=$default&s={$size}";
	elseif ( strpos($default, 'http://') === 0 )
		$default = add_query_arg( 's', $size, $default );

	if ( !empty($email) ) {
		$out = "$host/avatar/";
		$out .= $email_hash;
		$out .= '?s='.$size;
		$out .= '&d=' . urlencode( $default );

		$rating = get_option('avatar_rating');
		if ( !empty( $rating ) )
			$out .= "&r={$rating}";

		$avatar = $out;
	} else {
		$avatar = $default;
	}

	return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
}
endif;

http://cssor.com/wordpress-theme-common-functions.html

参与评论

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

*

全部分类
Books(4)code(7)database(6)html&css(24)java(11)JavaScript(49)jQuery(24)linux(20)python(1)React(1)share(1)soft(4)solution(53)thinking(17)vim(9)WordPress(8)前端优化(12)拓展(33)服务器(33)移动开发(4)自然(22)