wordpress 主题教程-笔记
前言:代碼參考 ,如無特別說明,下面所說的文件,都在 主題目錄下。
?https://blog.wpjam.com/m/wp-theme-lesson-3-starting-indexphp/
https://github.com/laughing2/wp-theme-tutorial
主題制作步驟
1. 制作好 前端頁面
2.??在你本地安裝的 WordPress 主題文件夾下?(應該在?wordpress/wp-content/themes),創建一個新的文件夾,命名為你的主題名 比如 ( mystyle)。
3. 上傳 index.php (前端首頁, 包括所引入的文件,如css、js的文件,都原來的相對路徑,放在主題目錄下,這里是 mystyle?)
? ? ?修改原來的相對路徑 ( 將原來的相對路徑 添加前綴代碼 <?php bloginfo('template_url');?>/ )
4. 上傳 style.css ( wordpress 主題樣式)
5. 把index.php 頁頭部份,制作成 header.php ,并上傳。
6. index.php? 文件, 用? <?php get_header(); ?>? ? ?,將 頁頭部份引入。
7.?把index.php 頁腳部份,制作成 footer.php ,并上傳。
8.??index.php? 文件, 用? <?php get_footer(); ?> ? ?,將 頁腳部份引入。
9.首頁常用函數?
9.1 顯示某個分類的名稱? ?注: $my_index_a1_cat? ?為 分類的 ID
<?php $my_index_a1_cat = 6;?> <?php echo get_cat_name($my_index_a1_cat); ?>9.2 某個分類列表鏈接?
<?php echo get_category_link($my_index_a1_cat);?>9.3 顯示某個分類前5篇文章? 如:分類ID為6,的前5篇文章
<?php $posts = get_posts( "category=6&numberposts=5" ); ?> <?php if( $posts ) : ?> <ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?> <li class="my-index-arlist-paddingtop"> <a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php ODD_title(31); ?></a> </li> <div class="my-xuxian"></div> <?php endforeach; ?> </ul> <?php endif; ?>9.4 自定義顯示標題的字數 。? 注:在主題的 functions.php 文件中添加,方便其他地方調用。
//自定義顯示文章標題的字數長度 //然后在需要調用文章標題的地方使用下面的代碼即可: //<!--?php ODD_title(20); ?--> function ODD_title($char) {$title = get_the_title($post->ID);//$title = substr($title,0,$char);//$title = mb_substr($title,0,$char,'utf-8');$content_str = $title;$title = mb_substr($title,0,$char,'utf-8');if (mb_strlen($content_str,'utf-8') > $char ) {$title = mb_substr($title,0,$char,'utf-8').'…';}echo $title; }9.5 更多鏈接。 注:這里更多,指向某個分類的文章列表
<div class="my-index-more"><a href="<?php echo get_category_link($my_index_a1_cat);?>" >[更多]</a> </div>9.6 顯示某一篇文章的標題和鏈接
<h2><a href="<?php echo get_permalink(152); ?>"><?php echo get_post($index_id)->post_title; ?></a></h2>9.7 顯示某一篇文章的內容 。注:這里沒有用到 functions.php
<p><?php query_posts( 'showposts=5&p=152'); ?><?php while (have_posts()) : the_post(); ?><?php //the_content(); the_excerpt(); //echo mb_strimwidth(strip_tags(apply_filters( 'the_content', the_content()), 0, 20, "...")); ?><?php endwhile;wp_reset_query();?> </p>10. functions.php 常用功能
<?php //讓WordPress首頁自動顯示文章第一張圖片 spricamacuk function catch_that_image() {global $post, $posts;$first_img = '';ob_start();ob_end_clean();$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);$first_img = $matches [1] [0];if(empty($first_img)){ //Defines a default image$first_img = "images/default.jpg";}return $first_img;}//自定義顯示文章標題的字數長度 //然后在需要調用文章標題的地方使用下面的代碼即可: //<!--?php ODD_title(20); ?--> function ODD_title($char) {$title = get_the_title($post->ID);//$title = substr($title,0,$char);//$title = mb_substr($title,0,$char,'utf-8');$content_str = $title;$title = mb_substr($title,0,$char,'utf-8');if (mb_strlen($content_str,'utf-8') > $char ) {$title = mb_substr($title,0,$char,'utf-8').'…';}echo $title; }//控制wordpress博客首頁博文顯示內容字數 function Excerpt_Content($max_length,$content_id) { // $title_str = get_the_title(); // $content_str = get_post( $content_id )->post_content; $content_str = get_post( $content_id )->post_excerpt; if (mb_strlen($content_str,'utf-8') > $max_length ) { $content_str = mb_substr($content_str,0,$max_length,'utf-8').'…'; } return $content_str; } ?>11. 404頁
<?php get_header(); ?><div class="campl-row campl-content campl-recessed-content"><div class="campl-wrap clearfix"><div class="campl-column9 campl-main-content"><h1 class="page-title" style="text-align: center"><?php _e( '404!頁面找不到. ', 'spricamacuk' ); ?></h1><!--<p><?php _e( '頁面找不到. 重新搜索?', 'spricamacuk' ); ?></p><?php get_search_form(); ?>--><p><br /><br /><br /><br /><br /><br /><br /><br /></p></div></div></div><!-- footer --><?php get_footer(); ?>12.?文章歸檔 (archive.php)
<?php get_header(); ?><div class="campl-row campl-content campl-recessed-content"><div class="campl-wrap clearfix"><div class="campl-column9 campl-main-content"><!-- 輸入主循環代碼 --><?php query_posts( 'showposts=10&cat=6'); ?><?php if(have_posts()) : ?><h1><?php echo single_cat_title( '', false ); ?></h1><?php while(have_posts()) : the_post(); ?><div class="post" id="post-<?php the_ID();?>"><li style="padding-top:3px;font-size:18px;line-height: 50px;"><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title();?><?php the_time( 'Y-m-d'); ?></a></li></div><?php endwhile; ?><?php else : ?><div class="post"><h2><?php _e( 'Not Found'); ?></h2></div><?php endif; ?></div><style>.homepage .campl-recessed-secondary-content { margin-top: -5%; }</style><div class="campl-column3 campl-secondary-content campl-recessed-secondary-content"><!-- sidebar --><?php get_sidebar(); ?></div></div></div><!-- 底部 --><?php get_footer(); ?></div></body></html>13.?分類目錄模板 (category.php)
<?php get_header(); ?><div class="campl-row campl-content campl-recessed-content"><div class="campl-wrap clearfix"><div class="campl-column9 campl-main-content"><!-- 輸入主循環代碼 --><?php if(have_posts()) : ?><?php $category=g et_the_category(); ?><h1><?php echo $category[0]->cat_name; ?></h1><?php while(have_posts()) : the_post(); ?><div class="post" id="post-<?php the_ID();?>"><h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2><div class="entry"><?php the_excerpt(); ?></div></div><?php endwhile; ?><?php else : ?><div class="post"><h2><?php _e( 'Not Found'); ?></h2></div><?php endif; ?></div><style>.homepage .campl-recessed-secondary-content { margin-top: -5%; }</style><div class="campl-column3 campl-secondary-content campl-recessed-secondary-content"><!-- sidebar --><?php get_sidebar(); ?></div></div></div><!-- 底部 --><?php get_footer(); ?>14.??單獨頁面 (page.php)
<?php get_header(); ?><div id="container"><!-- 輸入主循環代碼 --><?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?><div class="post" id="post-<?php the_ID();?>"><h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2><div class="entry"><?php the_content(); ?><!-- 定制 page.php --><?php link_pages( '<strong>Pages:</strong>', '', 'number'); ?><?php edit_post_link( 'Edit', '', ''); ?></div></div><?php endwhile; ?><?php else : ?><div class="post"><h2><?php _e( 'Not Found'); ?></h2></div><?php endif; ?></div><?php get_sidebar(); ?><!-- 底部 --><?php get_footer(); ?></div></body></html>15.?搜索結果 (search.php)
<?php get_header(); ?><div class="campl-row campl-content campl-recessed-content"><div class="campl-wrap clearfix"><div class="campl-column9 campl-main-content"><!-- 輸入主循環代碼 --><?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?><div class="post" id="post-<?php the_ID();?>"><h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2><div class="entry"><?php the_excerpt(); ?></div></div><?php endwhile; ?><?php else : ?><div class="post"><h2><?php _e( 'Not Found'); ?></h2></div><?php endif; ?></div><style>.homepage .campl-recessed-secondary-content { margin-top: -5%; }</style><div class="campl-column3 campl-secondary-content campl-recessed-secondary-content"><!-- sidebar --><?php get_sidebar(); ?></div></div></div><!-- 底部 --><?php get_footer(); ?></div></body></html>16.?搜索框 (searchform.php)
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/"><div><input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" size="15" /><br /><input type="submit" id="searchsubmit" value="Search" /></div> </form>17.?文章頁面 (single.php)
<?php get_header(); ?><div class="campl-row campl-content campl-recessed-content"><div class="campl-wrap clearfix"><div class="campl-column9 campl-main-content"><?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?><div class="campl-content-container campl-sub-column-right-border"><h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2><p><?php the_content(); ?><?php link_pages( '<strong>Pages:</strong>', '', 'number'); ?></p></div><?php endwhile; ?><?php else : ?><div class="post"><h2><?php _e( 'Not Found'); ?></h2></div><?php endif; ?></div><style>.homepage .campl-recessed-secondary-content { margin-top: -5%; }</style><div class="campl-column3 campl-secondary-content campl-recessed-secondary-content"><!-- sidebar --><?php get_sidebar(); ?></div></div></div><!-- footer --><?php get_footer(); ?>X. 更新目錄權限?chmod -R 777 wordpress
? ?參考:?https://jingyan.baidu.com/article/fd8044fa2e7af35031137af2.html
?
?
?
? ??
?
?
?
總結
以上是生活随笔為你收集整理的wordpress 主题教程-笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【会议记录】软件工程课程设计第一次会议
- 下一篇: VS编译错误:winsock.h win