WordPress 主题
我希望您查看我的示例 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为创建函数 action_is_set() 不值得。
您最终会得到:
然而,将开关移至functions.php 中的函数可能会有所帮助。
这看起来类似于:
或者您可以通过将内容部分移动到新的包含文件来使当前页面完全模块化:
我不知道这与 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:
Moving your switch to a function inside functions.php could be beneficial however.
That would look something similar to:
Or you can make this current page completely modular by moving the content section to a new include file:
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.
您可以在主题下的functions.php中编写该函数。
You can write the function in functions.php under the theme.