无法使全局变量起作用,如何将函数中的值从函数传递给函数.php

时间:2017-12-11 19:04:32

标签: php wordpress

编辑: 问题是当进行更多帖子的ajax加载时,$ year1或$ year值会重复出现。 $ year的价值实际上是一个月。

现在的样子:

  • 活动1
  • 十一月
  • 活动2
  • 活动3
  • 11月---这里ajax调用发生,月值重复
  • 活动4
  • 活动5
  • 活动6

以及它应该如何:

  • 活动1
  • 十一月
  • 活动2
  • 活动3
  • 活动4
  • 活动5
  • 活动6

到目前为止,我尝试过使用全局变量 Get a variable from one wordpress template file to another 并获取查询变量 https://wordpress.stackexchange.com/questions/96312/get-query-var-vs-global-query-variables

另一个问题是functions.php中的日期以英语返回值,而相同的代码以西班牙语返回日期 在模板中。

模板中的代码:请注意我使用带有11月值的$ test变量来测试porpuses,但它应该是$ year1 我不知道自己做错了什么......

$my_posts = new WP_Query( $args );
        if ( $my_posts->have_posts() ) : 
        ?>

          <div class="my-posts2">



            <?php while ( $my_posts->have_posts() ) : $my_posts->the_post() ?>

              <?php 

             $dateformatstring = 'F';
             $unixtimestamp = strtotime(get_field('start_date'));
             $year1 = date_i18n($dateformatstring, $unixtimestamp);
             $year_check2 = $GLOBALS['year_checkbarcelonafunctions'];

             // If your year hasn't been echoed earlier in the loop, echo it now
             if (($year1 !== $year_check1) && ($year1 !== $year_check2)){
                 echo "<h2 class='year-act text-center'>" . $year1 . "</h2>";
                 }

             // Now that your year has been printed, assign it to the $year_check variable
                $test = 'November';
                $GLOBALS['year_checkbarcelona1'] = $test;

                $year_check1 = $year1;



                ?>

functions.php中的代码:

function load_posts_by_ajax_callback2() {
    check_ajax_referer('load_more_posts2', 'security2');
    $paged = $_POST['page'];
    $today = current_time('Y-m-d');
        $args = array (
              'meta_query'             => array(
                'relation'    => 'AND',
                array(
                  'category'         => 'actividades',
                    'key'       => 'start_date',
                    'value'     => $today,
                    'compare'   => '<',
                    'type'      => 'DATE',
                    ),
                array(
                    'key'   => 'region',
                    'value'   => 'Barcelona',
                    'compare' => '='
                    ),


            ),
            'meta_key'               => 'start_date',
            'orderby'                => 'meta_value',
            'order'                  => 'DSC',
            'posts_per_page' => 3, 
            'paged' => $paged,


        );

        $query = new WP_Query( $args ); ?>
        <?php if ( $query->have_posts() ) : ?>

            <?php while ( $query->have_posts() ) : $query->the_post();?>
                <?php 

             $dateformatstring = 'F';
             $unixtimestamp = strtotime(get_field('start_date'));
             $year = date_i18n($dateformatstring, $unixtimestamp);

           //Example another global
           //$paged = $_POST['page'];
             $year_check1 = $GLOBALS['year_checkbarcelona1'];        


             // If your year hasn't been echoed earlier in the loop, echo it now
             if (($year !== $year_check) && ($year !== $year_check1)){

                 echo "<h2 class='year-act text-center'>" . $year . "</h2>";        
                 }          


             // Now that your year has been printed, assign it to the $year_check variable
             $GLOBALS['year_checkbarcelonafunctions'] = $year;
             $year_check = $year;

             ?>   

换句话说,全局值应该避免重复在执行ajax加载时对活动/事件进行分组的$ year标记。

编辑:我对其他不依赖全局变量的解决方案持开放态度。

Edit3:我在事件模板的循环中使用了这段代码(不起作用):

global $mytest2;
$mytest2 = 'November';

并且在函数load_posts_by_ajax_callback2里面的functions.php中,我试过了

             ob_start();
             global $mytest2;
             echo $mytest2;
             $myStr = ob_get_contents();
             ob_end_clean();                     


             if (($year !== $year_check) && ($year !== $myStr)){

                 echo "<h2 class='year-act text-center'>" . $year . "</h2>";        
                 }

0 个答案:

没有答案