使用函数中定义的变量

使用函数中定义的变量

本文介绍了范围:使用函数中定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在函数.php文件中定义了某些变量:

  add_action('the_post','paginate_slide '); 

函数paginate_slide($ post){

全局$ pages,$ multipage,$ numpages; $()
$ b if(is_single()&&; get_post_type()=='post'){

$ multipage = 1;
$ id = get_the_ID();
$ custom = array();
$ pages = array();
$ i = 1;

foreach(get_post_custom_keys()as $ key)
if(false!== strpos($ key,'slide'))
$ custom [$ key] = get_post_meta( $ id,$ key,true);

while(isset($ custom [slide {$ i} -title])){

$ page ='';
$ tzTitle = $ custom [slide {$ i} -title];
$ tzImage = $ custom [slide {$ i} -image];
$ tzDesc = $ custom [slide {$ i} -desc];
$ tzEmbed = $ custom [slide {$ i} -embed];

$ page =< h2> {$ tzTitle}< / h2>< img src ='{$ tzImage}'/>;
$ pages [] = $ page;
$ i ++;
}

$ numpages = count($ pages);
}
}

我想输出一些这些变量像这样的template.php文件:<?php echo $ tzDesc; ?> 但我似乎无法让它工作。根据我对的理解,为了调用这些变量在另一个地方,我需要在全局范围内定义它们,并将它们称为全局函数,就像我做了 $ pages,$ multipage,$ numpages; 一样。这应该允许我将这些变量插入到我需要的地方。问题是当我将它们从函数中取出并在全局范围内定义它们时,整个函数停止工作。



如何构造这个结构,以便我可以调用<?php echo $ tzDesc; ?> 在网站的任何地方,并让它回声定义的信息?



我不知道这是否重要,但这是一个如果你想使用<?php echo $ tzDesc;

?>> 无论如何,您需要将 $ tzDesc 定义为全局变量。但是,我不建议这样做,因为全局变量被认为是很差的编程习惯。

更好的解决方案是将 paginate_slide()添加 $ tzDesc (以及其他值)到 $ post 对象。这样,只要您调用 the_post(),就可以访问这些变量。如果你走这条路线,一定要命名空间变量:

  $ post-> ns_tzDesc = $ tzDesc; 


I have a function in a functions.php file that defines certain variables:

add_action( 'the_post', 'paginate_slide' );

function paginate_slide( $post ) {

    global $pages, $multipage, $numpages;

    if( is_single() && get_post_type() == 'post' ) {

    $multipage = 1;
    $id = get_the_ID();
    $custom = array();
    $pages = array();
    $i = 1;

    foreach( get_post_custom_keys() as $key )
        if ( false !== strpos( $key, 'slide' ) )
            $custom[$key] = get_post_meta( $id, $key, true);

    while( isset( $custom["slide{$i}-title"] ) ) {

        $page = '';
        $tzTitle = $custom["slide{$i}-title"];
        $tzImage = $custom["slide{$i}-image"];
        $tzDesc = $custom["slide{$i}-desc"];
        $tzEmbed = $custom["slide{$i}-embed"];

        $page = "<h2>{$tzTitle}</h2><img src='{$tzImage}' />";
        $pages[] = $page;
        $i++;
    }

    $numpages = count( $pages );
    }
}

I'd like to output some of these variables in a template.php file like so: <?php echo $tzDesc; ?> but I can't seem to get it to work. From what I understand about the variables scope, in order to call these variables in another place I need to define them within the global scope and call them as global in this function like I did the $pages, $multipage, $numpages;. That should allow me to plug those variables in where I need them. The problem is when I take them out of the function and define them above within the global scope the entire function stops working.

How do I need to structure this so I can call <?php echo $tzDesc; ?> anywhere in the site and have it echo the defined info?

I don't know if this matters but this is on a WordPress site.

解决方案

If you want to use <?php echo $tzDesc; ?> anyway, you would need to define $tzDesc as a global variable. However, I don't recommend doing so as global variables are considered poor programming practice.

A better solution would be to have the paginate_slide() add $tzDesc (and other values) to the $post object. That way you have access to these variables anytime you call the_post(). If you go this route, be sure to namespace you variables:

$post->ns_tzDesc = $tzDesc;

这篇关于范围:使用函数中定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:38