WordPress模板开发教程

页面模板文件:一个完整的主题中必须包含index.php和style.css
首页模板-index.php,这个模板决定你博客的主页长什么样子

文章模板-single.php,这个模板决定你单篇博客长什么样子

搜索模板-search.php,这个决定你搜索博客之后的文章列表页长什么样子

存档模板-archive.php,这个决定你通过分类,标签等分类过滤之后的文章列表长什么样子

错误页面-404.php,做技术的,这个就不用解释啦

  

 
获取模板路径:
<? echo get_template_directory_uri() ?> 模板路径
<?php bloginfo(’stylesheet_url’); ?> 模板style.css路径
get_template_directory() 文件路径
 
 
常用函数:
bloginfo('name');
bloginfo('url');
bloginfo(’description’);
get_the_ID():文章或页面或者循环中,可以使用该函数直接获取
the_ID():
 

is_page($page):是否为某一个页面

参数:$page 页面ID,页面标题或者页面别名,可选,默认值为空

返回的值:逻辑值(true/false)

 

is_sticky($post_ID):判断当前页面类型是否为置顶文章,$post_ID (可选) 文章ID

is_singular() :非列表页

is_home() || is_front_page()   ==== 判断是否主页

is_single() ==== 文章页
is_page() ==== 页面
is_category() ==== 文章分类页,eg:is_category(4)  、 is_category(‘movies’)
is_tag() ====文章标签页
is_archive() ==== 归档页
is_404() ==== 404页
is_search() ==== 搜索结果页
is_feed() ==== 订阅页

home_url() === 获取根域名

add_query_arg(array()) ===获取页面别名,如果带上根域名:home_url(add_query_arg(array()))

 

获取网站信息:bloginfo( $show ); 同:get_bloginfo($show),区别在于后者返回数据,而不是打印出来。

$show (字符串string) (可选)你需要输出的信息的关键词, 默认: name
'name' - 显示在 设置 > 常规 中设置的“站点标题”。 该数据是从 wp_options 这个数据表中检索到的 "blogname"记录。
'description' - 显示在 设置 > 常规 中设置的“副标题”。该数据是从 wp_options 这个数据表中检索到的 "blogdescription" 记录。
'wpurl' - 显示在 设置 > 常规 中设置的 “WordPress 地址 (URL)”。该数据是从 wp_options 这个数据表中检索到的 "siteurl" 记录。 可以考虑使用 site_url() 来代替,尤其是在使用 子目录路径方式,而不是使用 子域名 来配置多站点时(bloginfo将返回根网站的URL,而不是子站点的URL)。
'siteurl' / 'url' - 显示在 设置 > 常规 中设置的 “站点地址(URL)”)”。该数据是从 wp_options 这个数据表中检索到的 "home"记录。 可以考虑使用 home_url() 代替。
'admin_email' - 显示在 设置 > 常规 中设置的 “电子邮件地址”。该数据是从 wp_options 这个数据表中检索到的 "admin_email"记录。
'charset' - 显示在 设置 > 常规 中设置的“页面和feed的编码”。该数据是从 wp_options 这个数据表中检索到的"blog_charset" 记录。(注:3.5.1+好像已经没有这个选项了)
'version' - 显示你当前使用的 WordPress 版本。该数据是在 wp-includes/version.php 检索到的 $wp_version  这个字段的值。
'html_type' - 显示WordPress HTML 页面中的内容类型(默认: "text/html")。该数据可以从 wp_options 这个数据表中检索到的 "html_type" 记录。主题和插件可以通过使用 pre_option_html_type 过滤器覆盖默认值。
'text_direction' - 显示 WordPress HTML 页面的文本方向。可以考虑使用 is_rtl() 代替。
'language' - 显示WordPress的语言。
'stylesheet_url' - 显示当前使用的主题的 CSS文件(通常为 style.css)路径。可以考虑使用 get_stylesheet_uri() 代替。
'stylesheet_directory' - 显示当前使用的主题的样式表路径。可以考虑使用 get_stylesheet_directory_uri() 代替。
'template_url' / 'template_directory' - 当前主题的 URL 路径 。在子主题中, get_bloginfo('template_url') 和 get_template() 都将返回父主题的目录。可以考虑使用 get_template_directory_uri() (用于父主题目录)或get_stylesheet_directory_uri() (用于子主题目录)代替。
'pingback_url' - 显示通告文件 XML-RPC 的URL (xmlrpc.php)
'atom_url' - 显示 Atom feed URL (/feed/atom)
'rdf_url' - 显示 RDF/RSS 1.0 feed URL (/feed/rfd).
'rss_url' - 显示 RSS 0.92 feed URL (/feed/rss).
'rss2_url' - 显示 RSS 2.0 feed URL (/feed).
'comments_atom_url' - 显示评论的 Atom feed URL (/comments/feed).
'comments_rss2_url' - 显示评论的 RSS 2.0 feed URL (/comments/feed).

 

 读取列表: wordpress获取分类下文章列表四种方法

 读取文章略缩图,就是文章中包含的图片,自动提取出来:

//文章列表缩略图
if( !function_exists('my_auto_thumbnail')){
    function emtx_auto_thumbnail($pID,$thumb='thumbnail') {
        $blogimg = FALSE;
        if (has_post_thumbnail()) {
            $blogimg = wp_get_attachment_image_src(get_post_thumbnail_id($pID),$thumb);
            $blogimg = $blogimg[0];
        } 
        elseif ($postimages = get_children("post_parent=$pID&post_type=attachment&post_mime_type=image&numberposts=0")) {
        foreach($postimages as $postimage) {
            $blogimg = wp_get_attachment_image_src($postimage->ID, $thumb);
            $blogimg = $blogimg[0];
        }
        } 
        elseif (preg_match('/<img [^>]*src=["|\']([^"|\']+)/i', get_the_content(), $match) != FALSE) {
            $blogimg = $match[1];
        }
        if($blogimg) {
            $blogimg = '<aclass="img-thumbnail" href="'. get_permalink().'"><img src="'.$blogimg.'" alt="'.get_the_title().'" /></a>';
        }
        return $blogimg;
    }
}
使用方法:
if(my_auto_thumbnail($post->ID)){   echo my_auto_thumbnail($post->ID); }

 

 分类:

属性:

single_cat_title( '', false ) 或者 the_archive_title('<b>','</b>'):获取分类名称

get_the_archive_description:分类描述

遍历列表:

<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<?php the_title(); the_permalink(); ?>
这里显示列表数据
<?php endwhile; ?>
这里调用分页代码
<?php endif; ?>

 分页:

系统自带分页:
                            the_posts_pagination( array(
                                'mid_size' => 3,
                                'prev_text' =>'上一页',
                                'next_text' =>'下一页',
                                'before_page_number' => '第',
                                'after_page_number' => '页',
                            ) ); 
自定义分页:加入 functions.php

//自定义分页
function my_fenye() {
    $p = 3;
    if ( is_singular() ) return;
    global $wp_query, $paged;
    $max_page = $wp_query->max_num_pages;
    if ( $max_page == 1 ) return; 
    echo '<ul class="pagination">';
    if ( empty( $paged ) ) $paged = 1;
    if ( $paged > 1 ) my_to_link( 1, '首页' );
    // echo '<li class="page-item">' . get_previous_posts_link('上一页') . '</li>';
    for( $i = $paged - $p; $i <= $paged + $p; $i++ ) { 
    if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<li class='page-item active'><a>{$i}</a></li>" : my_to_link( $i );
    }
    // echo '<li class="page-item">' . get_next_posts_link('下一页') . '</li>';
    if ( $paged != $max_page ) my_to_link( $max_page, '尾页' );
    echo '<li class="page-item disabled"><a  class="page-link" href="#">共 '.$max_page.' 页</a></li>';
    echo '</ul>';
}

function my_to_link( $i, $title = '' ) {
    if ( $title == '' ) $title = "{$i}";
    echo "<li class='page-item'><a href='", esc_html( get_pagenum_link( $i ) ), "'>{$title}</a></li>";
}


调用:
<?php my_fenye();?>

 

 

 

 

 

 


 常用函数:

https://blog.wpjam.com/function_reference/

https://www.beizigen.com/tag/wordpress-template-tags

 

一整套WordPress模板制作的教程:https://www.cnblogs.com/ec04/p/12170585.html

WordPress 主题教程:从零开始制作 WordPress 主题: https://blog.wpjam.com/article/wp-theme-lessons/

wordpress CMS主题制作 :http://wanlimm.com/fenlei/wordpress_kanfa/wordpress_themes/

3个小时学会wordpress模板制作:https://blog.csdn.net/ytkah/article/details/79103341

posted on 2021-04-16 08:52  飞哥100  阅读(1522)  评论(0)    收藏  举报

导航