• 欢迎来到主题派官网,主题源码,插件,模板下载。所有主题及插件资源不负责技术支持,售出不退!
酷奇酷奇  2023-04-16 17:59 主题派 隐藏边栏 |   抢沙发  0 
文章评分 0 次,平均分 0.0

第一次做 wordpress 主题,记录些常用模板标签替换,省的之后再用又要到处找。

<span>1、首页 index.php </span><span><?</span><span>php get_header(); </span><span>?></span> <span><</span><span>title</span><span>></span><?php if (is_home()||is_search()) { bloginfo('name'); } <em id="__mceDel">else{wp_title(''); echo ' | '; bloginfo('name');} </em><em id="__mceDel"><em id="__mceDel"><em id="__mceDel"><em id="__mceDel">?> <span></</span><span>title</span><span>></span></em></em></em></em>
<em id="__mceDel"> <span><</span><span>link </span><span>rel</span><span>="stylesheet"</span><span> href</span><span>="<?php bloginfo('template_url'); ?>/assets/css/common.css"</span><span> type</span><span>="text/css"</span> <span>/></span> <span><</span><span>img </span><span>src</span><span>="<?php bloginfo('template_url'); ?>/assets/images/logo.png"</span><span> alt</span><span>="万豪行情"</span><span>></span><span> 导航菜单: </span><span><?</span><span>php wp_nav_menu(); </span><span>?></span><span> 首页调用栏目文章,例(栏目 3,9 篇文章): </span><span><?</span><span>php $posts = get_posts( "category=3&numberposts=9" ); </span><span>?></span> <span><?</span><span>php if( $posts ) : </span><span>?></span> <span><</span><span>ul </span><span>class</span><span>="post-list"</span><span>></span> <span><?</span><span>php foreach( $posts as $post ) : setup_postdata( $post ); </span><span>?></span> <span><</span><span>li</span><span>></span> <span><</span><span>a </span><span>href</span><span>="<?php the_permalink() ?>"</span><span> rel</span><span>="bookmark"</span><span> title</span><span>="<?php the_title(); ?>"</span><span>></span> <span><?</span><span>php echo mb_strimwidth(get_the_title(), 0, 36,"..."); </span><span>?></span> <span></</span><span>a</span><span>></span> <span><</span><span>span </span><span>class</span><span>="date"</span><span>></span><span><?</span><span>php the_time('Y-m-d'); </span><span>?></span><span></</span><span>span</span><span>></span> <span></</span><span>li</span><span>></span> <span><?</span><span>php endforeach; </span><span>?></span> <span></</span><span>ul</span><span>></span> <span><?</span><span>php endif; </span><span>?></span><span> 含 时间、链接、标题处理 文字剪切处理可用: </span><span><?</span><span>php echo wp_trim_words( get_the_title(),18);</span><span>?></span> <span><?</span><span>php echo mb_strimwidth(get_the_title(), 0, 36,"..."); </span><span>?></span><span> 2、page.php: </span><span><?</span><span>php get_sidebar(); </span><span>?></span><span> 面包屑导航: </span><span><?</span><span>php get_template_part( 'inc/breadcrumb' ); </span><span>?></span> <span><?</span><span>php the_crumbs(); </span><span>?></span><span> breadcrumb.php </span><span><?</span><span>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 ' &gt; '; echo get_category_parents( get_query_var('cat') , true , ' &gt; ' ); echo ' 文章 '; } if (is_single()) { echo ' &gt; '; echo the_category(' &gt; ', 'multiple'); echo ' &gt; '; echo ' 正文 '; } if (is_page()) { echo ' &gt; '; echo the_title(); } elseif (is_tag()) {echo ' &gt; ';single_tag_title();echo ' &gt; 文章 ';} elseif (is_day()) {echo ' &gt; ';echo"发表于"; the_time('Y 年 m 月 d 日'); echo'的文章';} elseif (is_month()) {echo ' &gt; ';echo"发表于"; the_time('Y 年 m 月'); echo'的文章';} elseif (is_year()) {echo ' &gt; ';echo"发表于"; the_time('Y 年'); echo'的文章';} elseif (is_author()) {echo ' &gt; ';echo wp_title( ''); echo'发表的文章';} elseif (is_search()) {echo ' &gt; ';echo"搜索结果"; echo'';} elseif (is_404()) {echo ' &gt; ';echo"亲,你迷路了!"; echo'';} echo '</div>'; } </span><span>?></span><span> 获取内容: </span><span><?</span><span>php if(have_posts()):while(have_posts()):the_post(); </span><span>?></span> <span><</span><span>div </span><span>class</span><span>="post"</span><span>></span> <span><?</span><span>php the_content();</span><span>?></span> <span></</span><span>div</span><span>></span> <span><?</span><span>php endwhile;endif;</span><span>?></span><span> 3、category.php 栏目页 栏目 id </span><span><?</span><span>php $cat_ID = get_query_var('cat'); </span><span>?></span><span> 栏目名 --- </span><span><?</span><span>php single_cat_title(); </span><span>?></span><span> 循环数据: </span><span><?</span><span>php if ( have_posts() ) : </span><span>?></span> <span><</span><span>ul </span><span>class</span><span>="list"</span><span>></span> <span><?</span><span>php while ( have_posts() ) : the_post(); </span><span>?></span> <span><</span><span>li</span><span>></span> <span><</span><span>a </span><span>href</span><span>="<?php the_permalink() ?>"</span><span> rel</span><span>="bookmark"</span><span> title</span><span>="<?php the_title(); ?>"</span><span>></span> <span><?</span><span>php echo mb_strimwidth(get_the_title(), 0, 80,"..."); </span><span>?></span> <span></</span><span>a</span><span>></span> <span><</span><span>span </span><span>class</span><span>="date"</span><span>></span><span><?</span><span>php the_time('Y-m-d'); </span><span>?></span><span></</span><span>span</span><span>></span> <span></</span><span>li</span><span>></span> <span><?</span><span>php endwhile; </span><span>?></span> <span></</span><span>ul</span><span>></span> <span><?</span><span>php endif; </span><span>?></span><span> 分页: </span><span><</span><span>div </span><span>class</span><span>='paging'</span><span>></span> <span><?</span><span>php get_template_part( 'inc/pagenavi' ); </span><span>?></span> <span><?</span><span>php pagenavi(); </span><span>?></span> <span></</span><span>div</span><span>></span><span> pagenavi.php: </span><span><?</span><span>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">'."\n"; 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."\n"; } 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>"; } </span><span>?></span><span> 4、single.php 文章详情页 获取上级栏目 id 和名称: $category = get_the_category(); $cat_ID = $category[0]->cat_ID; $cat_NAME = $category[0]->cat_name; </span><span><?</span><span>php the_time('Y-m-d');</span><span>?></span><span> 获取页面内容: </span><span><?</span><span>php echo get_post($post_id)->post_content; </span><span>?></span><span> 上下篇: </span><span><</span><span>p</span><span>></span><span><?</span><span>php if(get_previous_post()){previous_post_link('上一篇:%link');}else{echo"上一篇:没有了,已经是最后文章";}</span><span>?></span><span></</span><span>p</span><span>></span> <span><</span><span>p</span><span>></span><span><?</span><span>php if(get_next_post()){next_post_link('下一篇:%link');}else{echo"下一篇:没有了,已经是最新文章";}</span><span>?></span><span></</span><span>p</span><span>></span></em>

 

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 字的文章内容
?>

 

「点点赞赏,手留余香」

还没有人赞赏,快来当第一个赞赏的人吧!

酷奇给酷奇打赏
×
予人玫瑰,手有余香
  • 2
  • 5
  • 10
  • 20
  • 50
2
支付
wordpress 主题模板常用内容调用代码 为主题派网版权所有,转载请注明出处,所有资源不提供技术支持。
酷奇
酷奇 关注:0    粉丝:0
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到 常用语
扫一扫二维码分享
×
艾瑞克网