WordPress 主题

发布于 2024-11-30 16:06:45 字数 1735 浏览 0 评论 0原文

我希望您查看我的示例 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。 拜托,请帮忙。谢谢!!。

I would like you to review on my sample Wordpress theme index.php code.

<?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(); ?

I want to turn my if statement above as a function something like but I don't know how:

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

Is there a better structure of my code above??I'm still learning both PHP and Wordpress.
Please , please help. Thanks!!.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东走西顾 2024-12-07 16:06:45

我认为创建函数 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=blah 则不会执行任何操作的情况下

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

I don't feel it would be worth the effort to create a function action_is_set().

You would end up with:

function action_is_set() {
    return isset($_GET['action']);
}

Moving your switch to a function inside functions.php could be beneficial however.

That would look something similar to:

function do_action() {
    switch($_GET['action']) {
        case 'sendmail':
            include('sendmail.php');
            break;
    }
}

Or you can make this current page completely modular by moving the content section to a new include file:

<?php
get_header();

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

get_footer();
?>

I don't know how this goes with the best practices for WordPress, but it is a good practice to have a default case in a switch especially in a scenario where it would do nothing if for instance they went to yourdomain.com/?action=blah

Rule of thumb: Never expect they will use it as intended; always assume that someone will try to break your code.

吃不饱 2024-12-07 16:06:45

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

You can write the function in functions.php under the theme.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文