Php Wordpress主题

Php Wordpress主题,php,wordpress,themes,Php,Wordpress,Themes,我想让您回顾一下我的示例Wordpress主题索引.php代码 <?php /* Template name: Homepage */ get_header(); ?> <?php if(isset($_GET['action'])): $action = $_GET['action']; switch($action){ case "sendmail": include("sendmail.php"); bre

我想让您回顾一下我的示例Wordpress主题索引.php代码

<?php
/*
Template name: Homepage
*/
get_header();
?>
<?php

    if(isset($_GET['action'])):
        $action = $_GET['action'];
        switch($action){
            case "sendmail": include("sendmail.php"); break;
            case "mailsent" : include("thanks.php"); break;
        }
    else:
?>
    <!-------// Begin Content ---------->
    <?php if (have_posts()): ?>
    <?php while(have_posts()): the_post(); ?>
        <tr>
            <td class="contentarea">
                <h1><?php the_title(); ?></h1>
                <p> <?php the_content(); ?></p>
            </td>
        </tr>
    <?php endwhile; ?>
    <?php else: ?>
        <tr>
            <td class="contentarea">
                <h1>Page not Found!</h1>
                <p>Sorry, you are looking a page that is not here! </p>
                <?php get_search_form(); ?>
            </td>
        </tr>
    <?php endif; ?> 
        <!-------// End Content ----------> 
        <tr>
        <!--begin contact form -->  
            <td class="contactarea" height="200">
                <?php include("contact_area.php"); ?>
            </td>
        <!--end contact form -->     
        </tr>
<?php endif;?>
<?php get_footer(); ?
上面的代码有更好的结构吗?我还在学习PHP和Wordpress。
请,请帮忙。谢谢

您可以在functions.php主题下编写函数。

我觉得创建函数action\u is\u set()不值得

你最终会得到:

function action_is_set() {
    return isset($_GET['action']);
}
但是,将开关移到functions.php中的函数可能会有所帮助

这看起来类似于:

function do_action() {
    switch($_GET['action']) {
        case 'sendmail':
            include('sendmail.php');
            break;
    }
}
或者,您可以通过将内容部分移动到新的包含文件,使当前页面完全模块化:

<?php
get_header();

switch($_GET['action']) {
    case 'sendmail':
        include('sendmail.php');
        break;
    case 'mailsent':
        include('thanks.php');
        break;
    default:
        include('content.php');
}

get_footer();
?>

我不知道WordPress的最佳实践如何,但在交换机中设置默认情况是一个很好的实践,尤其是在这样一个场景中,如果用户访问yourdomain.com/?action=blah,则默认情况将一事无成


经验法则:永远不要期望他们会按预期使用它;始终假设有人试图破坏您的代码。

@Dan这与代码的正常功能没有什么不同。您可以在functions.php中定义一些函数,并可以在模板php文件中使用它们。此外,您还可以使用add_action/add_filter更改wordpress功能的操作,如内容。从mythemeshop购买的任何主题均可享受60%的折扣。访问
<?php
get_header();

switch($_GET['action']) {
    case 'sendmail':
        include('sendmail.php');
        break;
    case 'mailsent':
        include('thanks.php');
        break;
    default:
        include('content.php');
}

get_footer();
?>