WORDPRESS模版制作常用标签收集

  • title标题

<title><?php if ( is_home() ) {bloginfo('name');}
elseif ( is_category() ) {single_cat_title(); echo " - "; bloginfo('name');}
elseif (is_single() || is_page() ) {single_post_title(); echo " - "; bloginfo('name');}
elseif (is_search() ) {single_post_title(); echo " - "; bloginfo('name');}
elseif (is_search() ) {echo "搜索结果"; echo " - "; bloginfo('name');}
elseif ( is_author() ) {_e('会员');_e(trim(wp_title('',0)));_e('的个人中心 - ');bloginfo('name'); }
elseif (is_404() ) {echo '404-页面未找到!';}
else {wp_title('',true);} ?> </title>
  • 关键词/描述
<?php
$description = '';
$keywords = '';
if (is_home() || is_page()) {
   // 将以下引号中的内容改成你的主页description
   $description = "一个前端开发工程师的自白:心向远方,何必苟且!";
   // 将以下引号中的内容改成你的主页keywords
   $keywords = "前端开发工程师,前端开发,JavaScript教程,jQuery,前端库,CSS3,HTML5";
}
elseif (is_single()) {
   $description1 = get_post_meta($post->ID, "description", true);
   $description2 = str_replace("\n","",mb_strimwidth(strip_tags($post->post_content), 0, 200, "…", 'utf-8'));
   // 填写自定义字段description时显示自定义字段的内容,否则使用文章内容前200字作为描述
   $description = $description1 ? $description1 : $description2;
   
   // 填写自定义字段keywords时显示自定义字段的内容,否则使用文章tags作为关键词
   $keywords = get_post_meta($post->ID, "keywords", true);
   if($keywords == '') {
      $tags = wp_get_post_tags($post->ID);    
      foreach ($tags as $tag ) {        
         $keywords = $keywords . $tag->name . ", ";    
      }
      $keywords = rtrim($keywords, ', ');
   }
}
elseif (is_category()) {
   // 分类的description可以到后台 - 文章 -分类目录,修改分类的描述
   $description = category_description();
   $keywords = single_cat_title('', false);
}
elseif (is_tag()){
   // 标签的description可以到后台 - 文章 - 标签,修改标签的描述
   $description = tag_description();
   $keywords = single_tag_title('', false);
}
$description = trim(strip_tags($description));
$keywords = trim(strip_tags($keywords));
?><meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
  • 分类标签

输出所有分类(包括子分类)

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

但是这样往往不是我们想要的,很多时候我们只输出顶级栏目或者指定栏目,所以下面以云库网导航为例给大家分享下是如何实现的,代码如下:

<nav>
    <div class="container">
        <div class="row">
            <ul class="main-nav oh">
                <li><a href="<?php echo get_option('home'); ?>" title="首页"><span>网站首页</span></a></li>
                <?php
                $args=array(
                'orderby' => 'id',
                'order' => 'ASC',
                'include' => '1,9,14'
                );
                $categories=get_categories($args);
                foreach($categories as $category) {
                    echo '<li><a class="nav-link" href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "%s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>';
                }
                ?>
                <li><a href="http://zhaoxixiong.com" title="朝夕熊博客" target="_blank"><span>朝夕熊</span></a></li>
            </ul>
        </div>
    </div>
</nav>

其中'include' => '1,9,14'就是指定显示的栏目ID.

  • 置顶标签
<ul>
    <?php
    $sticky = get_option('sticky_posts');
    rsort( $sticky );//对数组逆向排序,即大ID在前
    $sticky = array_slice( $sticky, 0, 1);//输出置顶文章数,请修改5,0不要动,如果需要全部置顶文章输出,可以把这句注释掉
    query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
    if (have_posts()) :while (have_posts()) : the_post();
    ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; endif; ?>
</ul>

为置顶内容加标识(如图片):

<?php if(is_sticky()) {echo '<img src="'.get_bloginfo('template_directory').'/images/top.gif" alt="置顶文章" title="置顶文章" />';} ?>
  • 首页/列表页
<!-- 如果是首页则显示置顶文章 -->
<?php if(is_home()){?>
<div class="main-hot row animated bounceInDown">
<?php
$sticky = get_option('sticky_posts');
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 1);
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
if (have_posts()) :
while (have_posts()) : the_post();
?>
<article class="hot-content">
<h2 class="article-title-hot"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute( array( 'before' => '', 'after' => '' ) ); ?>"><?php the_title(); ?></a></h2>
<div class="hot-descrition mt25">
<p><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 240,"..."); ?></p>
</div>
<div class="hot-read-more oh">
<a href="<?php the_permalink(); ?>" rel="nofollow">阅读全文</a>
<?php the_category(','); ?>
</div>
</article>
<?php endwhile; endif; wp_reset_query();?>
</div>
<?php } ?>
<!-- 循环出最新文章 -->
<div class="article-list-wrap">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( have_img($post->post_content) ): ?>
<?php get_template_part( 'template-parts/content', 'img' ); <!-- 如果文章有缩略图则调用此模板(content-img) -->
else : ?>
<?php get_template_part( 'template-parts/content', 'noimg' ); ?> <!-- 如果文章没有缩略图则调用此模板(content-noimg) -->
<?php endif ?>
<?php endwhile; else: ?><p><?php _e('Sorry, no posts matched your criteria.'); ?></p><?php endif; ?>
</div>
<div class="prenext oh">
<?php par_pagenavi(9); ?>
</div>
  • 文章列表

置顶文章

<?php
$sticky = get_option('sticky_posts');
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 1);
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
if (have_posts()) :
while (have_posts()) : the_post();
?>
<ul>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
</ul>
<?php endwhile; endif; wp_reset_query(); ?>

注意:如果最后不加wp_reset_query();会导致页面其实地方显示数受到影响。

最新文章

<?php $rand_posts = get_posts('numberposts=8&orderby=post_dat'); foreach( $rand_posts as $post ) : ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <?php echo mb_strimwidth(get_the_title(), 0, 50, '…'); ?> </a></li>
<?php endforeach; ?>

随机文章

<ul>
    <?php $rand_posts = get_posts('numberposts=8&orderby=rand'); foreach( $rand_posts as $post ) : ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <?php echo mb_strimwidth(get_the_title(), 0, 50, '…'); ?> </a></li>
    <?php endforeach; ?>
</ul>

热评文章

<ul>
    <?php $postslist = get_posts('numberposts=8&order=DESC&orderby=comment_count'); foreach( $postslist as $post ) : ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo mb_strimwidth(get_the_title(), 0, 50, '…'); ?></a></li>
    <?php endforeach; ?>
</ul>
  • 内容页
<?php while ( have_posts() ) : the_post();?>
<?php get_template_part( 'template-parts/content', 'page' ); ?>  <!-- 调用 content.php 模板 --> 
<?php endwhile;?>

content.php模板代码如下:

<article>
    <header class="article-header">
    <!-- 文章标题 -->
    <h1 class="article-title"><?php the_title( sprintf( '<a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a>' ); ?></h1>
    <h6 class="article-info">
    <!-- 文章作者 -->
    <span><i class="fa fa-user"></i><?php the_author(); ?></span>
    <!-- 发表时间 -->
    <span><i class="fa fa-clock-o"></i><?php echo get_the_time('Y-m-d'); ?></span>
    <!-- 所属分类 -->
    <span><i class="fa fa-list"></i><?php the_category(', '); ?></span>
    <span></span>
    </h6>
    </header>
    <div class="article-content">
    <!-- 文章内容 -->
    <?php
    /* translators: %s: Name of current post */
    the_content("Continue reading " . the_title('', '', false));
    ?>
    </div>
    <footer>
       <div class="article-tag mt35">
        <i class="fa fa-crosshairs"></i><?php the_tags('文章关键词:',' • ','<br />'); ?>
       </div>
        <!-- 上一篇|下一篇 -->
        <div class="article-prenext bdt">
            <div><i class="fa fa-angle-up"></i>上一篇:<strong><?php previous_post_link('%link'); ?></strong></div>
           <div><i class="fa fa-angle-down"></i>下一篇:<strong><?php next_post_link('%link'); ?></strong></div>
        </div>
    </footer>
</article>

上面这个是一个完整的内容页主区块的模板,参考里面的标签来就好了。其中第一段代码中的调用 content.php 模板,其实如果你喜欢的话,也可以直接把 content.php 模板里面的代码直接替换掉第二行代码<?php get_template_part( 'template-parts/content', 'page' ); ?>也是可以的,我这样写只是想告诉你可以通过这个函数调用模板而已。

  • 面包屑导航

这是一个相当简单的面包屑导航,如果你不是很在意,很抠细节,那么这个也就够用了。

<!-- 列表页 -->
<a title="首页" href="<?php echo get_option('home'); ?>/">首页</a> > <?php the_category(', '); ?>
<!-- 内容页 -->
<ul>
    <li><i class="fa fa-folder-open-o"></i><a title="网站首页" href="<?php echo get_option('home'); ?>/">网站首页</a> <?php the_category(', '); ?> <?php the_title( sprintf( '<a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a>' ); ?></li>
</ul>

说完了简单的面包屑导航,肯定少不了比较复杂接地气的方法。代码如下:

<?php
function cmp_breadcrumbs() {
$delimiter = '»'; // 分隔符
$before = '<span class="current">'; // 在当前链接前插入
$after = '</span>'; // 在当前链接后插入
if ( !is_home() && !is_front_page() || is_paged() ) {
echo '<div id="crumbs">'.__( '<i class="fa fa-folder-open-o"></i>' , 'cmp' );
global $post;
$homeLink = home_url();
echo ' <a href="' . $homeLink . '">' . __( '网站首页' , 'cmp' ) . '</a> ' . $delimiter . ' ';
if ( is_category() ) { // 分类 存档
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0){
$cat_code = get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' ');
echo $cat_code = str_replace ('<a','<a', $cat_code );
}
echo $before . '' . single_cat_title('', false) . '' . $after;
} elseif ( is_day() ) { // 天 存档
echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
echo $before . get_the_time('d') . $after;
} elseif ( is_month() ) { // 月 存档
echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
echo $before . get_the_time('F') . $after;
} elseif ( is_year() ) { // 年 存档
echo $before . get_the_time('Y') . $after;
} elseif ( is_single() && !is_attachment() ) { // 文章
if ( get_post_type() != 'post' ) { // 自定义文章类型
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a> ' . $delimiter . ' ';
echo $before . get_the_title() . $after;
} else { // 文章 post
$cat = get_the_category(); $cat = $cat[0];
$cat_code = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
echo $cat_code = str_replace ('<a','<a', $cat_code );
echo $before . get_the_title() . $after;
}
} elseif ( !is_single() && !is_page() && get_post_type() != 'post' ) {
$post_type = get_post_type_object(get_post_type());
echo $before . $post_type->labels->singular_name . $after;
} elseif ( is_attachment() ) { // 附件
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID); $cat = $cat[0];
echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> ' . $delimiter . ' ';
echo $before . get_the_title() . $after;
} elseif ( is_page() && !$post->post_parent ) { // 页面
echo $before . get_the_title() . $after;
} elseif ( is_page() && $post->post_parent ) { // 父级页面
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
echo $before . get_the_title() . $after;
} elseif ( is_search() ) { // 搜索结果
echo $before ;
printf( __( 'Search Results for: %s', 'cmp' ), get_search_query() );
echo $after;
} elseif ( is_tag() ) { //标签 存档
echo $before ;
printf( __( 'Tag Archives: %s', 'cmp' ), single_tag_title( '', false ) );
echo $after;
} elseif ( is_author() ) { // 作者存档
global $author;
$userdata = get_userdata($author);
echo $before ;
printf( __( 'Author Archives: %s', 'cmp' ), $userdata->display_name );
echo $after;
} elseif ( is_404() ) { // 404 页面
echo $before;
_e( 'Not Found', 'cmp' );
echo $after;
}
if ( get_query_var('paged') ) { // 分页
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
echo sprintf( __( '( Page %s )', 'cmp' ), get_query_var('paged') );
}
echo '</div>';
}
}
?>

把上面的代码放到 function.php 文件里,然后在模板里把下面这段代码放在你需要显示的地方就可以了,非常地简单。

<?php if(function_exists('cmp_breadcrumbs')) cmp_breadcrumbs();?>
  • 分页导航
<?php
function par_pagenavi($range = 9){
global $paged, $wp_query;
if ( !$max_page ) {$max_page = $wp_query->max_num_pages;}
if($max_page > 1){if(!$paged){$paged = 1;}
if($paged != 1){echo "<a href='" . get_pagenum_link(1) . "' class='extend' title='跳转到首页'> 返回首页 </a>";}
previous_posts_link(' 上一页 ');
if($max_page > $range){
if($paged < $range){for($i = 1; $i <= ($range + 1); $i++){echo "<a href='" . get_pagenum_link($i) ."'";
if($i==$paged)echo " class='current'";echo ">$i</a>";}}
elseif($paged >= ($max_page - ceil(($range/2)))){
for($i = $max_page - $range; $i <= $max_page; $i++){echo "<a href='" . get_pagenum_link($i) ."'";
if($i==$paged)echo " class='current'";echo ">$i</a>";}}
elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){
for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){echo "<a href='" . get_pagenum_link($i) ."'";if($i==$paged) echo " class='current'";echo ">$i</a>";}}}
else{for($i = 1; $i <= $max_page; $i++){echo "<a href='" . get_pagenum_link($i) ."'";
if($i==$paged)echo " class='current'";echo ">$i</a>";}}
next_posts_link(' 下一页 ');
if($paged != $max_page){echo "<a href='" . get_pagenum_link($max_page) . "' class='extend' title='跳转到最后一页'> 最后一页 </a>";}}
}
?>

把上面这个函数放到 function.php 文件里,调用方法也非常地简单,在需要显示面包屑导航的地方添加这行代码就可以了<?php par_pagenavi(9); ?>

  • 日志归档
<div class="widget-wrap widget-list">
    <h3 class="widget-title"><i class="fa fa-history"></i>日志归档</h3>
    <ul class="archive-list">
    <?php $args = array(
    'type' => 'monthly',
    'limit' => '',
    'format' => 'html',
    'before' => '',
    'after' => '',
    'show_post_count' => ture,
    'echo' => 1,
    'order' => 'DESC'
    ); ?>
   <?php wp_get_archives( $args ); ?>
   </ul>
</div>
  • 博客信息
<div class="widget-wrap widget-list">
    <h3 class="widget-title"><i class="fa fa-users"></i>网站统计</h3>
        <ul class="archive-list">
            <li>文章:<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish; ?></li>
            <li>总评论:<?php $total_comments = get_comment_count(); echo $total_comments['approved'];?></li>
            <li>标签数:<?php echo $count_tags = wp_count_terms('post_tag'); ?></li>
        </ul>
     <p><i class="fa fa-spinner fa-spin fa-pulse"></i>最后更新: <?php the_modified_date('j\<\s\u\p\>S\<\/\s\u\p\> M Y'); ?></p>