2011-08-23 142 views
0

我想你回顾我的示例WordPress主题index.php代码。WordPress的主题

<?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(action_is_set()){ 
      then_do_the_action(); 
     }else { 
      //begin content..etc. 
     } 

有没有的我上面的代码??我还是一个更好的结构学习PHP和Wordpress。 请帮助。谢谢!!。

+0

从mythemeshop购买任何主题都可享受六折优惠。访问http://tech-papers.org/mythemeshop-coupon-code/ –

回答

1

我不觉得这将是值得努力创建一个函数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

经验法则:永远不要期望他们会按照预期使用它;总是假定有人会试图破坏你的代码。

1

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

+0

谢谢,请问如何在functions.php中编写函数? ?:-( – Dan

+0

@Dan这与代码正常函数没什么不同,你可以在functions.php中定义一些函数,并且可以在你的模板php文件中使用它们,并且你可以使用add_action/add_filter来改变wordpress的动作像the_content这样的函数。 – xdazz