Function 在函数中包装代码会阻止它工作
我希望将代码包装到函数中(然后将其放入functions.php),这样我就可以在其他地方调用它,但我的代码一旦包装到函数中就会失败Function 在函数中包装代码会阻止它工作,function,wordpress-theming,wordpress,Function,Wordpress Theming,Wordpress,我希望将代码包装到函数中(然后将其放入functions.php),这样我就可以在其他地方调用它,但我的代码一旦包装到函数中就会失败 <?php function getGallery2() { ?> <!-- 1. search for any pages with a custom field of 'test' that have a value of 'yes' --> <?php query_
<?php function getGallery2() { ?>
<!-- 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
<?php query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- 2. echo the test field -->
<?php $link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
<?php $alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
<img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
<?php endwhile;?>
<?php wp_reset_query(); ?>
<?php } ?>
<?php getGallery2(); ?>
我认为这可能是一个范围问题,我是否必须以某种方式将post编号传递给函数?如果我去掉了围绕查询的函数,代码就可以正常工作
我猜代码实际上是不相关的(尽管我可能错了)——这更多地是因为它是一个循环和一个函数
<?php function getGallery2() { ?>
<!-- 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
<?php query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- 2. echo the test field -->
<?php $link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
<?php $alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
<img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
<?php endwhile;?>
<?php wp_reset_query(); ?>
<?php } ?>
<?php getGallery2(); ?>
“alt=”“/>
$post不在函数范围内
您可以将global$post;
添加到函数顶部,也可以将其作为如下参数:
function getGallery2($post){
// code
}
echo getGallery2($post)
函数内的代码只能看到在同一函数内或全局范围内创建的变量。这意味着$post
对象未定义
//
有一点离题,PHP中有很多HTML注释。通过使用PHP,您可以轻松地整理东西
编辑:
函数getGallery2(){
全球$员额;
//1.搜索具有自定义字段“test”且值为“yes”的任何页面-->
查询帖子('meta_key=Gallery-Promotion Gallery照片链接&post_type=page');
while(have_posts()):the_post();
//2.回显测试字段-->
$link=get_post_meta($post->ID,'Gallery-Promotion Gallery Photo link',true);
$alt=get\u post\u meta($post->ID,'Gallery-Promotion Gallery Photo-alt text',true);
回声';
结束时;
wp_reset_query();
}
getGallery2();
我想你会得到类似的东西(未经测试):
希望这能有所帮助:D嗨,吉姆,恐怕还是运气不好。有什么想法吗?
“alt=”“/>
另外,尝试将评论格式化得很好(不是懒惰),但是格式化StackOverflow评论太麻烦了。而且,不知道为什么人们建议在函数中放置一个循环是不正确的——所以也许我应该改用get_template_part?把一个函数放在一个循环中或者把一个循环放在一个函数中没有区别。嘿,Kam,谢谢你的建议。我通常使用get_template_part来代替(不确定它和“require”之间的确切区别,非常确定它做的是相同的事情)。我想知道使用adv/disadv和调用函数之间的区别是什么。也许我会开始一个新的问题来回答这个问题?上面讨论了get_page_模板与调用函数的优缺点。看起来差不多。我个人认为我更喜欢获取页面模板而不是函数,我认为这比在functions.php中混淆视听更清楚-但至少我现在知道如何做到这两个:-DAlso,对于那些阅读者来说,将其放在函数中的一个优点是可以传递变量,就像这样。。。
<?php function getGallery2() { ?>
$global post;
$link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
$alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
<img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
<?php } ?>
require('get-gallery.php');