这里介绍一下主题index.php代码,它是生成的是网站首页显示的内容,还是以默认模板default为例。下面是index.php的完整代码:
<?php /** * The main template file * * This is the most generic template file in a WordPress theme * and one of the two required files for a theme (the other being style.css). * It is used to display a page when nothing more specific matches a query. * E.g., it puts together the home page when no home.php file exists. * * @link https://developer.wordpress.org/themes/basics/template-hierarchy/ * * @package cenote */ $archive_layout = get_theme_mod( 'cenote_archive_style', 'tg-archive-style--masonry' ); get_header(); ?> <div id="primary" class="content-area"> <main id="main" class="site-main"> <?php if ( have_posts() ) : if ( 'tg-archive-style--masonry' === $archive_layout ) : ?> <div class="cenote-content-masonry cenote-content-masonry--animated"> <div id="cenote-content-masonry"> <?php endif; /* Start the Loop */ while ( have_posts() ) : the_post(); /* * Include the Post-Format-specific template for the content. * If you want to override this in a child theme, then include a file * called content-___.php (where ___ is the Post Format name) and that will be used instead. */ get_template_part( 'template-parts/content', get_post_format() ); endwhile; if ( 'tg-archive-style--masonry' === $archive_layout ) : ?> </div> </div> <!-- /.cenote-content-masonry --> <?php endif; // show pagination. get_template_part( 'template-parts/pagination/pagination' ); else : get_template_part( 'template-parts/content', 'none' ); endif; ?> </main><!-- #main --> </div><!-- #primary --> <?php get_sidebar(); get_footer();
先说下三个函数get_header(); get_sidebar(); get_footer();分别输出各自的模板代码如header.php、sidebar.php、footer.php;相应的模板代码生成后加入相应位置。
第5行的if (have_posts()) : 为判断是否含有日志;
第7行的 while (have_posts()) : 为当有日志时,进行循环操作;the_post(); 为对单个循环中的一篇日志进行操作。
第9行:post_class();输出个性化的日志class——官方参考例如置顶文章回输出不同的class,可以加入参数自定义生成的类名如:post_class(’myclass’);;the_ID();生成日志id数字号。
第10行:the_permalink()为每篇日志对应内容页的链接,the_title();为日志标题;
第11行:the_time(__(’F jS, Y’, ‘kubrick’)为日志发布时间;
第14行:the_content(__(’Read the rest of this entry »’, ‘kubrick’));为日志内容;
第17行:the_tags(__(’Tags:’, ‘kubrick’) . ‘ ‘, ‘, ‘, ‘<br />’); 为日志所在标签;printf(__(’Posted in %s’, ‘kubrick’), get_the_category_list(’, ‘));为日志所在分类;
第20行: endwhile; 结束循环;
第23行: next_posts_link(__(’« Older Entries’, ‘kubrick’))上一篇日志链接;
第24行: previous_posts_link(__(’Newer Entries »’, ‘kubrick’)) ?>下一篇日志链接;
第27行: else : 为没有日志时候的情况,
第29行:php _e(’Not Found’, ‘kubrick’);为没有发现日志
第30行:php _e(’Sorry, but you are looking for something that isn’t here.’, ‘kubrick’);为相关提示;
第31行:include (TEMPLATEPATH . “/searchform.php”);插入搜索表单模板;
第33行:endif;结束判断。
修改wordpress主题的index.php小方法:
- 可以先新建一个page页面,也就是后台的新建页面,然后在后台的设置阅读里面可以设定首页显示一个静态页面。可以选择自建页面和文章页。
- 可以修改你的首页模板,将里面的循环文章的调用和文章的代码删除,之后再换成你的代码,这点话就要求有一定的代码能力了。
贴上主题文件夹路径吧,怕有些不知道的,
就这样,有问题可评论留言、
Hello, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam
responses? If so how do you prevent it, any plugin or
anything you can advise? I get so much lately it’s driving me crazy so any help is very much appreciated.
Howdy! This is kind of off topic but I need some advice from an established blog.
Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick.
I’m thinking about setting up my own but I’m not sure where to start.
Do you have any points or suggestions? Appreciate it