dream752614590
2019-01-22 07:33
浏览 71
已采纳

我们可以更改WordPress主题代码来创建自定义单个页面吗?

I am using the wordpress theme: Corporate Blue as a standard template. Now keeping the header and the footer php files same, I am trying to create other custom pages(including the home-page), and I am getting confused as to how to combine different files like index.php, header.php, footer.php, page.php, single.php. Also do I have to create any additional php files in my source folder to for any extra pages?

So far I have just changed the look and feel of the website by making changes in the style.css, I also made a custom-homepage.php in which I included the header and footer using and , also I am running everything on a localhost as of now.

图片转代码服务由CSDN问答提供 功能建议

我使用wordpress主题:Corporate Blue作为标准模板。 现在保持页眉和页脚php文件相同,我正在尝试创建其他自定义页面(包括主页),我对如何组合不同的文件如index.php,header.php,footer感到困惑。 php,page.php,single.php。 我还需要在源文件夹中创建任何额外的php文件以用于任何额外的页面吗?

到目前为止,我刚刚通过更改样式来更改网站的外观和感觉 .css,我还制作了一个custom-homepage.php,其中包含了页眉和页脚的使用,而且我现在正在本地主机上运行所有内容。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongniechi7825 2019-01-22 07:55
    已采纳

    If you want to customize templates with existing theme, that supports remote updates, the best practice is to start with creating a child theme, that would take existing theme ( in this case - Corporate Blue ) as parent theme.

    This way, you avoid possible overwrites during theme updates, and also allows you to override template files that exist in parent theme, by placing templates files of same name in child theme directory.

    There are few steps to achieve this:

    1. Create child theme
    2. Clone/Override or create page template files in child theme
    3. Clone/Override or create template files in child theme for post types single views, post type archives, taxonomy, front page, etc.

    1. Child Theme

    To create child theme, you need to create at least style.css within child theme directory. In your case, it would look something like this:

    /*
     Theme Name:   Corporate Blue Child
     Theme URI:    http://themeurl.com/your-theme-slug/
     Description:  Corporate Blue Child Theme
     Author:       Your Name
     Author URI:   http://example.com
     Template:     corporate-blue
     Version:      1.0.0
     License:      GNU General Public License v2 or later
     License URI:  http://www.gnu.org/licenses/gpl-2.0.html
     Tags:         tags, that, describe, your, theme
     Text Domain:  corporate-blue-child
    */
    

    This would tell WordPress that this theme depends on Corporate Blue theme, and to look up for template files in parent theme, if template files of same name don't exist in child theme.

    However, this wouldn't include parent theme styles. To include parent theme's css, assuming that main stylesheet file is style.css, create a functions.php file ( which WordPress loads automatically ), and place this code:

    The key part here is get_template_directory_uri(), that tells WordPress to basic directory to look up for file is parent theme directory. And this function looks up for Template tag in style.css of child theme.

    For more details see: https://developer.wordpress.org/themes/advanced-topics/child-themes/

    Keep in mind that child theme should be activated, not parent theme.

    2. Custom page template files in child theme

    After that you need to figure out what you need of custom pages. Easiest way to create custom page template is to clone page.php or index.php, and rename it. In the top of the cloned file, place php comment with Template Name:

    <?php /* Template Name: Example Template */ ?>
    

    That would allow WordPress to list all page templates for global use when adding/editing page. It would show them in drop down, usually on right sidebar when adding/editing page.

    There are also page templates for specific pages. If you know page ID or page slug, you can add either one as suffix to template name:

    page-{slug}.php
    page-{ID}.php 
    

    This way, WordPress would recognize and load such template for specific page.

    See more about custom page templates: https://developer.wordpress.org/themes/template-files-section/page-template-files/

    3. Template files in child theme

    Templates for post types, archives, category pages, front page, blog archive page, etc.

    You need to learn more about template hierarchy:
    https://developer.wordpress.org/themes/basics/template-hierarchy/

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongwen3410 2019-01-22 07:48

    Yes, we can change the wordpress theme's files, but don't do this if you don't have any knowledge of wordpress structure and php. It might break your theme.

    If you only want to create custom pages without changing the functionality, here is the guide you should follow.

    https://www.wpbeginner.com/wp-themes/how-to-create-a-custom-page-in-wordpress/

    It will help you to create the custom pages in wordpress.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题