Home Backend Development PHP Tutorial Specific implementation steps based on wordpress theme production_PHP tutorial

Specific implementation steps based on wordpress theme production_PHP tutorial

Jul 21, 2016 pm 03:10 PM
php wordpress theme code specific make exist based on copy accomplish root step of Table of contents

Copy code The code is as follows:

/*
在根目录 -> wp-content -> themes 下创建mytheme文件夹用来存放创建新主题模板

在mytheme目录下创建 index.php ,style.css 两个文件,在wp后台 外观->主题 中就可以看到刚创建的主题

打开style.css文件输入
*/
?>
/*
Theme Name: 这里填主题名称
Theme URI: 这里填主题介绍的网址,没有就填你的博客网址吧
Description:这里填主题的简短介绍
Author: 作者名
Author URI: 作者的网址
Version: 版本号
Tags: 标签,多个用半角逗号隔开
*/
/*
在后台主题管理中即可看到主题相关信息,css中主题信息内容必须用注释符号括起来

找一个300*225的png图片,命名为 screenshot.png 放在主题目录下(mytheme文件夹中),在主题管理页中即可看到新建主题的预览图片

//==================================================header================================================================
可以把网站相同头内容放在一个头文件中,在主题目录下新建 header.php 文件向其中输入输入 统一的头部内容
在 index.php 或想调用该header.php页面的页面中 输入
*/

get_header(); //get_header()就相当于将header.php中的代码拷贝到当前的php文件

/*
在主题管理页面,该主题实时预览中,默认打开的 index.php 页面中即可引入 header.php 页面的内容
header.php 将会被所有的模板页面(主页、分类页、页面、标签页等)所包含,所以 header.php 中代码应该是动态的。
不同页面的title都是不一样,而且title的设置还会直接影响到SEO的效果,所以这里应该谨慎设置。下面提供一种SEO优化的title写法,
在header.php页面添加
*/
?>
<br><?php<BR>if (is_home ()) { // is_home() 当前页面为主页时返回true<BR> bloginfo ( 'name' ); // 返回站点标题<BR> echo " - ";<BR> bloginfo ( 'description' ); // 返回站点副标题,站点描述<BR>} elseif (is_category ()) { // is_category() 当前页面为分类页时返回true<BR> single_cat_title ();<BR> echo " - ";<BR> bloginfo ( 'name' );<BR>} elseif (is_single () || is_page ()) { // is_single() 当前页面为单文章页时返回true 。 is_page() 当前页面为单页面时返回true<BR> single_post_title ();<BR>} elseif (is_search ()) { // is_search() 当前页面为搜索页时返回true<BR> echo "搜索结果";<BR> echo " - ";<BR> bloginfo ( 'name' );<BR>} elseif (is_404 ()) { // is_404() 当前页面为404页时返回true<BR> echo '页面未找到!';<BR>} else {<BR> wp_title ( '', true );<BR>}<BR>?><br>
/*
以上添加的php代码运用了条件判断,针对不同的页面采用不同title
在 header.php 页面中添加默认 style.css 文件
*/
?>

/*
bloginfo('stylesheet_url');返回的是主题默认style.css文件绝对网址路径,如
http://localhost/wordpress/wp-content/themes/myTheme/style.css
bloginfo('template_url');返回的是主题目录的绝对网址路径,可以用来模板中连接样式图片,如
http://localhost/wordpress/wp-content/themes/mytheme
添加 pingback 通告功能,在header.php页面 标签中里面添加代码:
*/
?>

/*
添加订阅feed链接,在header.php页面 标签中添加:
*/
?>


/*
添加wp_head,有些插件需要在网页头部添加一些js或css,要让这些插件能够正常的工作,也让主题有更好的兼容性,应该添加wp_head()函数
header.php 页面 标签中添加
*/
?>

/*
显示菜单栏,这里只在菜单栏中列出分类页和page页面,可以根据喜好来列出想要的。header.php中
*/
?>


//==================================================footer================================================================
/*
footer.php与header.php差不多,写这个文件的目的也是为了精简代码,提高代码的重用性。
在主题目录中创建 footer.php ,在 index.php 或想调用该footer.php页面的页面中使用
*/
get_footer();//功能和get_header()类似
/*
在footer.php页面添加 wp_footer提高兼容性
*/
wp_footer();
/*
wp_footer()和wp_head()差不多,都是用于提高主题兼容性,毕竟有很多插件要在页脚输出一些东西才能正常工作。
*/
//==================================================sidebar================================================================
/*
在主题目录下新建 sidebar.php 页面,在 index.php 或想调用该sidebar.php页面的页面中添加
*/
get_sidebar();
/*
调用 sidebar.php 页面内容
为使WordPress后台 -> 外观 -> 小工具,可以正常地拖动小工具到侧边栏
在 sidebar.php 页面的列表格式应按如下举例格式
*/
?>

    if (! function_exists ( 'dynamic_sidebar' ) || ! dynamic_sidebar ( 'First_sidebar' )) ://First_sidebar为widget名称,要和functions.php中对应的widget name相同
?>
   

分类目录


   

       
       

   


    if (! function_exists ( 'dynamic_sidebar' ) || ! dynamic_sidebar ( 'Second_sidebar' )) :
?>
   

最新文章


   

        $posts = get_posts ( 'numberposts=6&orderby=post_date' );
    foreach ( $posts as $post ) {
    setup_postdata ( $post );
    echo '
  • ' . get_the_title () . '
  • ';
        }
        $post = $posts [0];
        ?>
       

   


    if (! function_exists ( 'dynamic_sidebar' ) || ! dynamic_sidebar ( 'Third_sidebar' )) :
?>
   

标签云


   


   


    if (! function_exists ( 'dynamic_sidebar' ) || ! dynamic_sidebar ( 'Fourth_sidebar' )) :
?>
   

文章存档


   

       
       

   

/*
同时在主题目录下创建 functions.php 文件内容为
*/
/** widgets */
if( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'First_sidebar', //name就是给widget指定各自的名称,以便在sidebar.php中分别调用.所以只需要给这两个widget取两个名字就好了。
        'before_widget' => '', //定义Widget内容的前后标识符的语句
        'after_widget' => '',
        'before_title' => '

', //定义Widget标题的前后标识符的语句
        'after_title' => '

'
    ));
    register_sidebar(array(
        'name' => 'Second_sidebar',
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '

',
        'after_title' => '

'
    ));
    register_sidebar(array(
        'name' => 'Third_sidebar',
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '

',
        'after_title' => '

'
    ));
    register_sidebar(array(
        'name' => 'Fourth_sidebar',
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '

',
        'after_title' => '

'
    ));
}
/*
这样WordPress后台 -> 外观 -> 小工具,就可以正常地拖动小工具到侧边栏了

制作index.php 文章列表
例子
*/
?>


   
   
   

       
       


       
       


       
 

       
       
       
       
       
       
       

阅读全文


   

   
 

   

   
   

>', 0); ?>


   
   

未找到


   

没有找到任何文章!


   

/*
have_posts(); 判断是否有下一个文章
the_post(); 改变当前文章指向到下一个文章

the_permalink(); 当前指向文章的连接地址
the_title(); 当前指向文章的标题
the_tags('标签:'); 当前指向文章的标签
comments_popup_link('0 条评论', '1 条评论', '% 条评论', '', '评论已关闭'); 显示打印当前指向文章的评论链接
edit_post_link('编辑', ' • ', ''); 当前指向文章,显示打印当前指向文章的编辑链接
the_excerpt(); 当前指向文章,只要在写文章的时候在"摘要"框内填写摘要,在首页显示的就是摘要,如果不填就输出全文!
the_content('阅读全文...'); 用于输出当前指向文章全文,除非在文章中使用了
the_permalink();              返回当前指向文章阅读全文的连接地址
previous_posts_link('<< 查看新文章', 0); 显示打印当前显示列表分页连接(每页文章数量取决于在后台设置每页可显示的文章数量)
next_posts_link('查看旧文章 >>', 0);      显示打印当前显示列表分页连接
the_time('Y年n月j日');显示日期如 1999年5月1日

另外,还有个存档页面的模板archive.php,跟index.php的制作过程完全一样,只不过需要在functions.php里添加一个函数

单文章页single.php,可以根据index.php页往这里添加自己想要显示的内容

page.php 也就是页面,博客上的所有网页都是页面,这里指的页面一个单独的页面,如"关于"、"联系方式"等,可以在WordPress后台 – 页面,进行页面的添加修改等。
可根据之前函数添加本页内容
*/
while (have_posts()) :
    the_post(); update_post_caches($posts);
endwhile;
/*
update_post_caches($posts);  该函数重置文章缓存且未被记录。仅在页面的第一次循环检索到文章子集时,第二次循环可执行基本循环。

常用函数
get_avatar($comment, 48);       获取评论者的gravatar头像,尺寸为48 * 48
comment_reply_link()                 回复留言的链接
get_comment_time('Y-m-d H:i');       获取评论发布时间
edit_comment_link('修改');           管理员修改评论的链接
comment_text()                       输出评论内容

is_user_logged_in()                  判断用户是否登录
wp_login_url( get_permalink() );     博客登录地址
get_comment_author_link()            用于获取评论者博客地址
$comment_author                      读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写用户名
$comment_author_email                读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写Email
$comment_author_url                  读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写博客地址
do_action(‘comment_form', $post->ID) 该函数为某些插件预留
wp_logout_url(get_permalink())       退出登录的链接
*/

/*
创建模板文件
*/


/*
 Template Name: 自建模板
*/

/*
 模板文件中添加如上注释代码,模板文件名任意,在新建页面时模板选择即可显示 自建模板 来使用此模板
可添加想要的模板样式及页面内容,新建页面时只填标题不写内容,相当创建一个页面链接地址,新建页面存在 数据前缀_posts 表中
获取到页面地址后,在写地址时可在后添加参数,则转到该页时可通过$_GET,$_POST接收
可以单独建一个表存储地址,及所属页面类型,及各页面子父级关系,在插件中进行控制


wordpress fixed link
If it is not easy to modify the wordpress fixed link, open the option
#LoadModule rewrite_module modules/mod_rewrite.so in the apache configuration file httpd.conf
Remove the leading #, And change all AllowOverride None to AllowOverride all
If it is not an Apache server, but IIS is used for debugging, then you have to install an "ISAPI_Rewrite3_0069_Lite.msi" filter, and then set PHP as a priority in the site settings. .

Create a widget
Create a new custom file mytool.php in the theme directory with any file name and content
Then add the following code in functions.php
*/
register_sidebar_widget ( "I "Widget", "mytool_fun" ); // "My Gadget" is the name of the gadget displayed in the background, and mytool_fun is the method name for introducing the content of the self-built gadget page
function mytool_fun() {
include ( TEMPLATEPATH . "/mytool.php");
}
/*
You can see the customized gadget in the background gadget. After adding it, you can see the self-built gadget on the front page. Content of the page
*/
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327175.htmlTechArticleCopy the code as follows: ?php /* Create the mytheme folder under the root directory - wp-content - themes. To store and create a new theme template, create index.php and style.css in the mytheme directory...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

How to change the head image of the wordpress theme How to change the head image of the wordpress theme Apr 20, 2025 am 10:00 AM

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance &gt;Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

How to write a header of a wordpress How to write a header of a wordpress Apr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

What to do if there is an error in wordpress What to do if there is an error in wordpress Apr 20, 2025 am 11:57 AM

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

See all articles