課程:[https://ke.qq.com/course/448367](https://ke.qq.com/course/448367)
【theme基本顯示】
可以跳過自己搭建wordpress后臺核心函數的調用
直接在twentyseventeen選取functions.php和inc文件夾,實現官方模板使用的wordpress核心函數
添加wordpress主題根目錄函數
```
<?php bloginfo('template_directory'); ?>
// 對應的文件夾為:
<?php bloginfo('template_directory'); ?>/css/
<?php bloginfo('template_directory'); ?>/js/
<?php bloginfo('template_directory'); ?>/images/
```
首頁路徑
```
<?php echo get_option('home'); ?>
```
網站標題
```
<?php bloginfo('name'); ?>
```
網站描述
```
<?php bloginfo('description'); ?>
```
【get_template_part();的使用】
```
<?php get_template_part( 'nav' ); ?>
// Navigation bar (nav.php)
<?php get_template_part( 'nav', '2' ); ?> // Navigation bar #2 (nav-2.php)
<?php get_template_part( 'nav', 'single' ); ?>
// Navigation bar to use in single pages (nav-single.php)
```
【#分拆為header.php和footer.php】
index.php提取的代碼:
```
<?php get_header(); ?>
<?php get_footer(); ?>
```
在wordpress主題中,通常會有`<?php wp_head(); ?>`和`<?php wp_footer(); ?>`這兩個是wordpress插件向主題頭部和尾部加載內容使用的函數。這樣方便在主題指定位置中動態添加代碼,來實現插件的動態配置。
```
<?php wp_head(); ?>
<?php wp_footer(); ?>
```
參考:
[https://www.menglei.net/2721/](https://www.menglei.net/2721/)
【#文章循環輸出】
```
<?php
$categores = new WP_Query('post_type=post&cat=&posts_per_page=12');
if($categores->have_posts()):
while($categores->have_posts()) : $categores->the_post(); ?>
<!-- code... -->
<?php
endwhile;
else:
endif;
?>
```
```
<?php the_title(); ?>
<?php the_time('Y年n月j日, G:i:s'); ?>
<?php the_permalink(); ?>
<?php the_post_thumbnail('product-thumbnails'); ?>
<?php the_category(); ?>
```
funtion.php
```
add_image_size( 'product-thumbnails', 500, 202, true );
add_image_size( 'product-thumbnails-search', 500, 185, true );
```
【#文章內容】
```
<?php
if(have_posts()):
while(have_posts()): the_post();
the_content();
endwhile;
else:
// something can say this is nothing
endif;
?>
<?php previous_post_link('上一頁: %link'); ?><br/>
<?php next_post_link('下一頁: %link'); ?>
```
【#菜單】
```
<li>
<?php
$args = array(
//指定顯示的導航名,如果沒有設置,則顯示第一個
'theme_location' => 'top',
//導航菜單ul標簽的class名
'menu_class' => '',
//導航菜單ul標簽的id名
'menu_id' => "nav"
);
wp_nav_menu($args); ?>
</li>
```
【#分類】
集合:
```
<?php the_category(); ?>
```
可加入分隔符:
```
<?php
$categories = get_the_category();
$separator = ", ";
$output = '';
if($categories)
{
foreach ($categories as $key => $category)
{
$output .= '<a href="' . get_category_link($category->term_id) . ' ">' . $category->cat_name . '</a>'. $separator;
}
echo trim($output, $separator);
}
?>
```
【#留言功能】
```
<?php
if(comments_open() || get_comments_number()):
comments_template();
endif;
?>
```
總分類
```
<?php wp_list_cats('sort_column=name&optioncount=0&hierarchical=0'); ?>
```
最新內容
```
<?php wp_get_archives('type=postbypost&limit=10'); ?>
```
【4.頁面、文章樣式選擇顯示】
設置頁面模板,需要在指定頁面加入如下代碼:
```
<?php
/*
template name:模板名字
*/
?>
```
【指定循環輸出數量文章】
```
<?php
$current_page = max(1, get_query_var('paged')); //當前第幾頁
//查詢參數
$args = array_filter(array(
// 'orderby' => 'title',
// 'order' => 'ASC',
'post_type' => 'post',
'ignore_sticky_posts' => 1 ,
'posts_per_page' => 18,
'paged' => $current_page, //當前頁
));
//開始查詢
$query = new WP_Query($args);
$total_pages = $query->max_num_pages; //總共多少頁
while ($query->have_posts()):
$query->the_post();
?>
<!-- post code -->
<?php endwhile; ?>
<li>
<!-- 輸出分頁 -->
<?php echo paginate_links( array(
'prev_text' => __( '上一頁', 'YChinaTours' ),
'next_text' => __( '下一頁', 'YChinaTours' ),
'screen_reader_text' => null,
'total' => $total_pages, //總頁數
'current' => $current_page, //當前頁數
) ); ?>
</li>
```
【分類和其他類型的文章歸類】
```
<?php if(have_posts()) : ?>
<?php
if( is_category()){
single_cat_title();
}elseif(is_tag()){
single_tag_title();
}elseif(is_author()){
the_post();
echo 'Author Archives:'. get_the_author();
rewind_posts();
}elseif(is_day()){
echo 'Daily Archives:' .get_the_date();
}elseif(is_month()){
echo 'Daily Archives:' .get_the_date('F Y');
}elseif(is_year()){
echo 'Daily Archives:' .get_the_date('Y');
}else{
echo 'Archives';
}
?>
<?php while(have_posts()) : the_post(); ?>
<!-- post code -->
<?php endwhile; ?>
<?php echo paginate_links(); ?>
<?php else : ?>
<center><h2>對不起,沒有找到相關內容,請搜索其他關鍵詞</h2></center>
<?php endif; ?>
```
【搜索頁面】
搜索:
```
<?php the_search_query(); ?>
```
```
<?php if(have_posts()) : ?>
<center>
<h2>搜索結果:<?php the_search_query(); ?></h2>
</center>
<?php while(have_posts()) : the_post(); ?>
<!-- post code -->
<?php endwhile; ?>
<?php echo paginate_links(); ?>
<?php else : ?>
<h2><strong>沒有找到</strong>你想要找的東西 :-(</h2>
<h3>請重新搜索<strong>相關的</strong>關鍵詞 !</h3>
<?php endif; ?>
```
- WordPress平臺的網站開發
- 電商主題開發
- WooCommerce主題開發優化部分
- 首頁開發
- WooCommerce
- 判斷用戶是否登錄
- WordPress Menu
- WooCommerce PayPal Checkout Gateway
- 頁面和文章
- 調用產品和文章
- 判斷屬于哪個頁面
- 相關文章
- 消除文章分享按鈕集底部的文字
- wordpress主題模板和主題開發
- wordpress主題準備
- wordpress主題文件結構
- 豪源主題
- WooCommerce SEO
- 插件開發
- wordpress二次開發
- theme基本顯示
- menu調用
- 分拆為header.php和footer.php
- 頁面、文章樣式選擇顯示
- 面包屑導航 Breadcrumb
- 特色圖
- 閱讀次數統計
- 分頁功能
- Advanced Custom Fields
- Custom Post Type UI
- post type
- 小工具
- 小工具調用
- shortcode
- 文章循環輸出
- 標題和文章限制字數輸出顯示
- WordPress主題theme1開發
- wordpress搭建多站點
- wordpress常用函數
- wordpress循環代碼
- Woocommerce
- Woocommerce支持
- WordPress插件開發
- wordpress會員插件
- WordPress插件使用
- WordPress插件集
- WordPress的核心
- Wordpress原理
- Wordpress要點
- WordPress網站搬家
- WPML
- 服務器
- Cloud 9
- test
- 網站