如何将变量从一个函数传递到另一个函数

时间:2019-08-07 作者:ncti

我想将$linker\\u sluger从创建页面的函数转移到创建库的函数,但当我将其设置为全局并尝试转移时,就很清楚了。

function page_creator(){
global $wpdb, $ling, $slugerr, $linker_sluger, $tester;
$linker_sluger= $ling.\'/\'.$slugerr;

$poster_id = $wpdb ->get_results("SELECT ID from wp_posts WHERE post_status=\'publish\' AND post_type=\'post\' ORDER BY ID DESC");

foreach ($poster_id as $posters_id){

    global $post;
    $post = get_post($posters_id->ID);

    $galleries_counter = get_post_galleries($post, false );
    $total_gal = count( $galleries_counter );

    $ile_fotek = _get_total_images($galleries_counter );


    if(has_shortcode( $post->post_content, \'gallery\' ) && $ile_fotek<1){
        echo"za malo fotek mniej niz ".$ile_fotek;
    }
    elseif(has_shortcode( $post->post_content, \'gallery\' )){
        echo "FOTKIbb: ".$ile_fotek;
        if (is_page(376)){
            echo\'<div class="single-album col-md-3 col-sm-12">\';
            echo na_get_gallery_image_urls($posters_id->ID); 
            echo \'</div>\';
            $galeryjka =  na_get_gallery_image_urls($posters_id->ID);

            $title = get_the_title($post);
            $Poster = new PostController;
            $Poster->set_title($title);
            //$Poster->add_category(array(1,2,8));
            $Poster->set_type("page");
            $Poster->set_content("[insert_php]na_get_gallery_image_urls($posters_id->ID);[/insert_php]" );
            $Poster->set_author_id( 1 );
            $Poster->set_post_slug("galeria-".$title);
            $sluger = $Poster->set_post_slug("albums-inside-".$title);
            $Poster->set_page_template( "page-single-gallery.php" );
            $Poster->set_post_state( "publish" );
            $Poster->search(\'title\', \'Old Post\');
            $Poster->update();
            $Poster->create();
            //$Poster->PrettyPrintAll();
            $Poster->get_var(\'slug\');
            $ling = get_home_url();
            $slugerr = sanitize_title($sluger, \'\', \'save\');
            $linker_sluger = $ling.\'/\'.$slugerr;   // transfer this $linker_sluger to bottom function, but when try it clear




        }
        else{
            echo\'<div class="col-md-12 col-sm-12">\';
            echo na_get_gallery_image_urls($posters_id->ID);
            echo \'</div>\';
        }




    }




 }
}


function na_get_gallery_image_urls( $post_id ) {
global $wpdb, $linker_sluger, $ling, $slugerr, $tester;
$post = get_post($post_id);


// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, \'gallery\' ) )
    return;



// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
$title = get_the_title($post_id);

//$poster_guid = $wpdb->get_results("SELECT guid from wp_posts WHERE post_name LIKE (\'albums-inside-%\') ORDER BY guid DESC");
echo \'<h3>\'. $title .\'</h3>\'; 


// Loop through all galleries found
foreach( $galleries as $gallery ) {
    // Loop through each image in each gallery
        foreach( $gallery as $image) {

            if(is_page( 376 )){
                echo \'<div class="single-image page-gallery col-md-12 col-sm-12">\';
                echo \'<a href="\'.$linker_sluger.\'"><img src="\'.$image.\'"></a>\';
                echo \'</div>\';

            }

            elseif(is_page( 5 )){
                echo \'<div class="single-image page-home col-md-12 col-sm-12">\';
                echo \'<a href="\'.$linker_sluger.\'"><img src="\'.$image.\'"></a>\';
                echo \'</div>\';

            }

            else{
                echo \'<div class="single-image page-inside-gallery col-md-3 col-sm-12">\';
                echo \'<a class="foobox" rel="gallery" href="\'.$image.\'"><img src="\'.$image.\'"></a>\';
                echo \'</div>\';  


                }
        }
    }   
}

1 个回复
最合适的回答,由SO网友:moped 整理而成

无论函数中发生了什么(我没有深入研究),如果调用函数,可以传递多个参数

na_get_gallery_image_urls($posters_id->ID, $linker_sluger);
并使用该值<但是有意义吗?一旦你在变量中加入回声$galeryjka 然后你回音?