簡體   English   中英

Wordpress主題

[英]Wordpress theme

我希望您查看我的示例Wordpress主題index.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(); ?

我想將上面的if語句轉換為函數,但我不知道如何:

        if(action_is_set()){
            then_do_the_action();
        }else {
            //begin content..etc.
        }

上面的代碼是否有更好的結構?我還在學習PHP和Wordpress。 請幫忙。 謝謝!!。

您可以在主題下的functions.php中編寫該函數。

我覺得創建一個函數action_is_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時它什么都不做的情況下=胡說

經驗法則:永遠不要指望他們會按預期使用它; 總是假設有人會試圖打破你的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM