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

 

 

 

    

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/400344.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

转载 - 最近对问题

之前看过&#xff0c;可是当时没有细看&#xff0c;今天在网上搜了一下&#xff0c;看了一下别人的思路&#xff0c;毕竟这也是一类问题的经典。过一段时间再将自己对其认识总结。现在先转载别人的思路。 出处&#xff1a;http://blog.csdn.net/sd6264456/article/details/9318…

【51单片机快速入门指南】6.3:DS18B20 单总线数字温度计的多路读取

目录硬知识DS18B20介绍时序初始化时序写时序读时序命令ROM 操作命令ROM 搜索举例存贮器操作命令示例程序DS18B20.cDS18B20.h测试程序定时器中断服务函数单传感器时ID的获取 main.c单传感器读取温度和读取特定ID传感器的温度多路传感器读取普中51-单核-A2 STC89C52 Keil uVisio…

css怎么把背景图片拉伸至100%

div再加上属性&#xff0c; -moz-background-size:100% 100%; background-size:100% 100%; 这样设置后再看网页就会发现背景图片拉伸至100%了。 https://jingyan.baidu.com/article/ceb9fb10a6f68d8cad2ba017.html

SQL多行转多列

--★转换结果如上图 1、首先创建表&#xff1a; CREATE TABLE [成绩表]( [编号] [int]IDENTITY(1,1) NOT NULL, [姓名] [varchar](50)NULL, [语文] [numeric](5, 2)NULL, [数学] [numeric](5, 2)NULL, [英语] [numeric](5, 2)NULL ) ON [PRIMARY] 2、插入测试数据 INSERT INTO …

面向对象程序设计的术语

面向对象程序设计中的术语主要包括类、对象、封装、继承、多态性和消息传递。面向对象的思想通过这些术语得到了具体的体现。 类&#xff1a;是对具有相同类型的对象的抽象。一个对象所包含的数据和代码可以通过类来构造。对象&#xff1a;是运行期的基本实体&#xff0c;它是一…

C#通过FFmpeg获得视频文件参数

C#通过FFmpeg获得视频参数 FFmpeg简介 FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video&#xff08;录制、转换 、音/视频&#xff0c;并可将其转换为音/视频流的完整的&#xff0c;跨平台的j解决方案&#xff09;.FFmpeg的开发…

【51单片机快速入门指南】6.3.1:使用1-WIRE搜索算法搜索单总线上所有DS18B20的ID(基于二叉树)

目录源码OWSearch.c测试程序实验现象Proteus仿真实机测试普中51-单核-A2 STC89C52 Keil uVision V5.29.0.0 PK51 Prof.Developers Kit Version:9.60.0.0 移植自1-WIRE搜索算法 串口部分见【51单片机快速入门指南】3.3&#xff1a;USART 串口通信 DS18B20驱动程序见【5…

背景图片自适应,不重复

body{background:url("assets/myui/img/Starry.jpg") no-repeat;background-size:cover;background-attachment: fixed;} https://blog.csdn.net/qq_42256836/article/details/83720259

webpack那些事儿

Webpack是一个前端资源加载&#xff08;模块加载器&#xff09;兼打包工具。它将根据模块的依赖关系进行静态分析&#xff0c;然后将这些模块按照指定的规则生成对应的静态资源。 grunt/gulp是优化前端开发流程的工具&#xff0c;webpack是一种模块化解决方案&#xff0c;为了解…

卡拉丁发布第四代车用空调滤清器

5月15日&#xff0c;卡拉丁“全澄行动”第四代车用空调滤清器产品发布会在京举行&#xff0c;据悉&#xff0c;该车用空调滤清器PM2.5过滤效率可高达99%。经国家权威检测机构广东省微生物分析检测中心检测&#xff0c;卡拉丁第四代车用空调滤清器对大肠杆菌、金***葡萄球菌抗菌…

【 Grey Hack 】大数四则运算

目录效果加减乘除乘方源码版本&#xff1a;Grey Hack v0.7.3619 - Alpha 在Gs中&#xff0c;位数大于15的整数将以科学计数法显示&#xff0c;故这里提供一种基于字符串加法的四则大数运算算法。由于位数大于10的字符串无法用to_int方法转化为整数&#xff0c;因此本示例中以长…

radio和文字无法对齐

span.my-radio-padding {vertical-align: -3px; } https://blog.csdn.net/qq_29498555/article/details/79487141

uniapp打包小程序上传测试后,使用有的插件显示空白页面,问题解决方【有效 / 最新】

目录 问题1图在微信开发者平台正常能使用 打包上传微信小程序测试问题2图在微信开发者平台正常能使用 打包上传微信小程序测试解决步骤一1.2.3. 解决步骤二打开微信小程序官网看图跨域网址 最后 问题1图 在微信开发者平台正常能使用 未上传小程序&#xff0c;开发过程中&…

django加载本地html

django加载本地html from django.shortcuts import renderfrom django.http import HttpResponse from django.shortcuts import render,render_to_response # Create your views here. def hello(request): return render_to_response("hello.html") 传递数据到htm…

IText 生成页脚页码

做doc文档报表的时候可能遇到这样的需求&#xff1a; 每一个页面需要页码&#xff0c;用IText可以完成这样的需求。 IText生成doc文档需要三个包&#xff1a;iTextAsian.jar&#xff0c;iText-rtf-2.1.4.jar&#xff0c;iText-2.1.4.jar 代码亲测无错&#xff0c;如下所示&…

Java知多少(68)面向字符的输出流

面向字符的输出流都是类 Writer 的子类&#xff0c;其类层次结构如图 10-5 所示。 图10-5 Writer的类层次结构图表 10-3 列出了 Writer 的主要子类及说明。 表 10-3 Writer 的主要子类类名功能说明CharArrayWriter写到字符数组的输出流BufferedWriter缓冲输出字符流PipedWriter…

Linux虚拟机的替代品:Docker与WSL2上手笔记

目录安装Docker可能出现的问题内核需更新Linux 内核更新包将 WSL 2 设置为默认版本An error occurred安装镜像使用Microsoft Store安装所选的 Linux 分发手动安装镜像及文件夹的共享Docker run 命令Windows Terminal的安装在Windows Terminal中直接运行已有的容器Windows 10 20…

Mac/Linux/Centos终端中上传文件到Linux云服务器

Mac/Linux/Centos终端中上传文件到Linux云服务器 1、mac上传文件到Linux服务器 scp 文件名 用户名服务器ip:目标路径如&#xff1a;scp /Users/test/testFile testwww.linuxidc.com:/test/ 2、mac上传文件夹到Linux服务器&#xff0c;与上传文件相比多加了-r scp -r 文件夹目录…

flask需求文件requirements.txt的创建及使用

1.简介 Python项目中必须包含一个 requirements.txt 文件&#xff0c;用于记录所有依赖包及其精确的版本号用以新环境部署。 2.进入虚拟环境然后输入pip freeze > requirements.txt 每次安装或者升级了包之后最好也一并使用这个命令更新这个文件。 需求文件的内容示例…

DHT(Distributed Hash Table,分布式哈希表)

DHT(Distributed Hash Table&#xff0c;分布式哈希表)类似Tracker的根据种子特征码返回种子信息的网络。 DHT全称叫分布式哈希表(Distributed Hash Table)&#xff0c;是一种分布式存储方法。 在不需要服务器的情况下&#xff0c;每个客户端负责一个小范围的路由&#xff0c;并…