第一次做wordpress主题,记录些常用模板标签替换,省的之后再用又要到处找。
1、首页index.php <?php get_header(); ?> <title><?php if (is_home()||is_search()) { bloginfo('name'); } else{wp_title(''); echo ' | '; bloginfo('name');} ?> </title>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/assets/css/common.css" type="text/css" /> <img src="<?php bloginfo('template_url'); ?>/assets/images/logo.png" alt="万豪行情"> 导航菜单: <?php wp_nav_menu(); ?> 首页调用栏目文章,例(栏目3,9篇文章): <?php $posts = get_posts( "category=3&numberposts=9" ); ?> <?php if( $posts ) : ?> <ul class="post-list"> <?php foreach( $posts as $post ) : setup_postdata( $post ); ?> <li> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"> <?php echo mb_strimwidth(get_the_title(), 0, 36,"..."); ?> </a> <span class="date"><?php the_time('Y-m-d'); ?></span> </li> <?php endforeach; ?> </ul> <?php endif; ?> 含 时间、链接、标题处理 文字剪切处理可用: <?php echo wp_trim_words( get_the_title(),18);?> <?php echo mb_strimwidth(get_the_title(), 0, 36,"..."); ?> 2、page.php: <?php get_sidebar(); ?> 面包屑导航: <?php get_template_part( 'inc/breadcrumb' ); ?> <?php the_crumbs(); ?> breadcrumb.php <?php function the_crumbs() { if (is_home()) { echo '<div class="crumbs">文章排序:'; echo get_template_part( 'inc/order' ); } if (!is_home()) { echo '<div class="crumbs">现在位置: '; echo '<a title="返回首页" href="'; echo get_settings('home'); echo '">'; echo '首页'; echo "</a>"; } if (is_category()) { echo ' > '; echo get_category_parents( get_query_var('cat') , true , ' > ' ); echo ' 文章 '; } if (is_single()) { echo ' > '; echo the_category(' > ', 'multiple'); echo ' > '; echo ' 正文 '; } if (is_page()) { echo ' > '; echo the_title(); } elseif (is_tag()) {echo ' > ';single_tag_title();echo ' > 文章 ';} elseif (is_day()) {echo ' > ';echo"发表于"; the_time('Y年m月d日'); echo'的文章';} elseif (is_month()) {echo ' > ';echo"发表于"; the_time('Y年m月'); echo'的文章';} elseif (is_year()) {echo ' > ';echo"发表于"; the_time('Y年'); echo'的文章';} elseif (is_author()) {echo ' > ';echo wp_title( ''); echo'发表的文章';} elseif (is_search()) {echo ' > ';echo"搜索结果"; echo'';} elseif (is_404()) {echo ' > ';echo"亲,你迷路了!"; echo'';} echo '</div>'; } ?> 获取内容: <?php if(have_posts()):while(have_posts()):the_post(); ?> <div class="post"> <?php the_content();?> </div> <?php endwhile;endif;?> 3、category.php 栏目页 栏目id <?php $cat_ID = get_query_var('cat'); ?> 栏目名 --- <?php single_cat_title(); ?> 循环数据: <?php if ( have_posts() ) : ?> <ul class="list"> <?php while ( have_posts() ) : the_post(); ?> <li> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"> <?php echo mb_strimwidth(get_the_title(), 0, 80,"..."); ?> </a> <span class="date"><?php the_time('Y-m-d'); ?></span> </li> <?php endwhile; ?> </ul> <?php endif; ?> 分页: <div class='paging'> <?php get_template_part( 'inc/pagenavi' ); ?> <?php pagenavi(); ?> </div> pagenavi.php: <?php function pagenavi( $before = '', $after = '', $p = 3 ) { if ( is_singular() ) return; global $wp_query, $paged; $max_page = $wp_query->max_num_pages; if ( $max_page == 1 ) return; if ( empty( $paged ) ) $paged = 1; echo $before.'<nav id="pagenavi">'." "; if ( $paged > 1 ) p_link( $paged - 1, '上页', '上页' ); if ( $paged > $p + 1 ) p_link( 1, '第一页' ); if ( $paged > $p + 2 ) echo '<span class="pages">...</span>'; for( $i = $paged - $p; $i <= $paged + $p; $i++ ) { if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<span class='page-numbers current'>{$i}</span>" : p_link( $i ); } if ( $paged < $max_page - $p - 1 ) echo '<span class="pages">...</span>'; if ( $paged < $max_page - $p ) p_link( $max_page, '最后一页' ); if ( $paged < $max_page ) p_link( $paged + 1,'下页', '下页' ); echo '</nav>'.$after." "; } function p_link( $i, $title = '', $linktype = '' ) { if ( $title == '' ) $title = "第 {$i} 页"; if ( $linktype == '' ) { $linktext = $i; } else { $linktext = $linktype; } echo "<a class='page-numbers' href='", esc_html( get_pagenum_link( $i ) ), "' title='{$title}'>{$linktext}</a>"; } ?> 4、single.php 文章详情页 获取上级栏目id和名称: $category = get_the_category(); $cat_ID = $category[0]->cat_ID; $cat_NAME = $category[0]->cat_name; <?php the_time('Y-m-d');?> 获取页面内容: <?php echo get_post($post_id)->post_content; ?> 上下篇: <p><?php if(get_previous_post()){previous_post_link('上一篇:%link');}else{echo"上一篇:没有了,已经是最后文章";}?></p> <p><?php if(get_next_post()){next_post_link('下一篇:%link');}else{echo"下一篇:没有了,已经是最新文章";}?></p>
1、调用的内容过滤html标签
<?php
echo wp_trim_words( get_the_title(),50);// 截取50字的文章标题
echo wp_trim_words( get_the_excerpt(),100);// 截取100字的文章摘要
echo wp_trim_words( get_the_content(),500);// 截取500字的文章内容
?>