【发布时间】:2013-06-07 07:01:48
【问题描述】:
我正在使用 wordpress 和购买的模板建立一个网站。我在选项/页面创建中添加了一些功能。可以在选项中设置一般元描述,并在创建页面时为每个页面设置元描述。
虽然我对 PHP 完全陌生,但我还是设法将所有必要的内容添加到我的代码中。这并不难,而且效果很好。我的问题是:我做得对吗?如何优化我的解决方案?我的方法有什么缺点?
HTML (header.php):
<?php
// Defining a global variable
global $page_meta_description;
// Initializing the variable with the set value from the page
$page_meta_description= get_post_meta($post->ID, MTHEME . '_page_meta_description', true);
// Add meta tag if the variable isn't empty
if ( $page_meta_description != "" ) { ?>
<meta name="description" content="<?php echo $page_meta_description; ?>" />
<?php }
// Otherwise add globally set meta description
else if ( of_get_option('main_meta_description') ) { ?>
<meta name="description" content="<?php echo of_get_option('main_meta_description'); ?>" />
<?php }
// Set global meta keywords
if ( of_get_option('main_meta_keywords') ) { ?>
<meta name="keywords" content="<?php echo of_get_option('main_meta_keywords'); ?>" />
<?php } ?>
【问题讨论】:
-
不要使用GLOBALS还有其他的方法来传递变量。
-
如果变量没有在函数中使用,则不必使用
global关键字。 -