我似乎错过了一些东西......变量Foreach的PHP变量

时间:2013-12-12 01:54:51

标签: php variables dynamic

目前我有一些循环可以为wordpress中的不同部分生成选项。

在每个已发布网页的主题选项中创建一个部分。 选项包括为每个部分设置背景的区域。

  • 创建每个选项部分? - 检查
  • 根据页面标题为每个部分创建一个ID? - 检查
  • 根据存储在数据库中的内容获取后台设置,匹配生成的选项ID? - 这是我被困的地方......看到下面的代码,也许我错过了什么。

以下是在主题选项面板中生成选项部分的代码。 目前我只专注于标记为background_image的那个。

global $post;
$args = array(
              'sort_order' => 'ASC',
              'sort_column' => 'menu_order',
              'post_type' => 'page',
              'post_status' => 'publish'
              ); 
$pages = get_pages($args); 
foreach ( $pages as $page ) {
    $page_title = $page->post_title;
    $trim_it_up = preg_replace('/[^\w]+/', '_', $page_title); //remove any punctuation, spaces, etc. from the title. 
    $time_it_up = strtolower($trim_it_up);
    $options[] = array(
                       'name' => __(  $page_title . ' Page', 'endlyss'),
                       'class' => 'inner_section_header');
    $options[] = array(
                       'name' => __($page_title . ' Background Color One (Gradient)' , 'endlyss'),
                       'id' => $page_title . '_bg_color_one',
                       'std' => '$background_defaults',
                       'type' => 'color');

    $options[] = array(
                       'name' => __($page_title . ' Background Color Two (Gradient)' , 'endlyss'),
                       'id' => $page_title . '_bg_color_two',
                       'std' => '$background_defaults',
                       'type' => 'color');

    $options[] = array(
                       'name' => __('Background Image', 'endlyss'),
                       'id' => $trim_it_up . '_background_image',
                       'std' => '',
                       'type' => 'upload');
}

动态CSS文件中的php应该从生成的选项中提取每个变量:

global $post;
$args = array(
              'sort_order' => 'ASC',
              'sort_column' => 'menu_order',
              'post_type' => 'page',
              'post_status' => 'publish'
              ); 
$pages = get_pages($args); 
foreach ( $pages as $page ) {
    $page_title = $page->post_title;
    $trim_it_up = preg_replace('/[^\w]+/', '_', $page_title); //remove any punctuation, spaces, etc. from the title. 
    $trim_it_up = strtolower($trim_it_up);
    ${'{$trim_it_up}_section_background_image'} = of_get_option( $trim_it_up .'_background_image');
};

最后,php应该为每个部分的背景生成CSS。

$args = array(
              'sort_order' => 'ASC',
              'sort_column' => 'menu_order',
              'post_type' => 'page',
              'post_status' => 'publish'
              ); 
unset($trim_it_up);
$pages = get_pages($args);  //Store a list of pages according to the above arguments
foreach ( $pages as $page ) { //begin a loop to set the background for the element based on it's dynamic ID
    $page_title = $page->post_title; //Get the Page title to use later
    $trim_it_up = preg_replace('/[^\w]+/', '_', $page_title); //remove any punctuation, spaces, etc. from the title. 
    $trim_it_up = strtolower($trim_it_up);
    echo "#" . $trim_it_up . "{
    background-image: url(" . ${'{$trim_it_up}_section_background_image'} . ");
    }
    ";
}

这是我得到的输出:

#home{
    background-image: url();
}
#services{
    background-image: url();
}
#portfolio{
    background-image: url();
}
#contact{
    background-image: url();
}

我想要的是:每个背景图片网址都应该填充从主题选项中存储的网址。 没有错误消息,只是在某个地方,某些东西没有加起来......我无法弄清楚在哪里。

任何帮助将不胜感激。谢谢。


发现解决方案: 将第二组和第三组代码合并为一组,并提出以下内容:

$args = array(
    'sort_order' => 'ASC',
    'sort_column' => 'menu_order',
    'post_type' => 'page',
    'post_status' => 'publish'
    ); 
$pages = get_pages($args); 
foreach ( $pages as $page ) { 
    $page_title = $page->post_title;
    $trim_it_up = preg_replace('/[^\w]+/', '_', $page_title);
    $trim_it_up = strtolower($trim_it_up);
    $background_image_url = of_get_option($trim_it_up . '_background_image');
    echo "#" . $trim_it_up . "{
        background-image: url(" . $background_image_url . ");
    }";
}

基本上将of_get_option带入for_each循环,而不是在循环之外创建它。这样,我没有为每个变量添加一个不同的变量,只是每次都会重复使用的变量。

1 个答案:

答案 0 :(得分:1)

如果使用变量变量的代码与分配它们的代码的函数不同,则需要使变量成为全局变量。

另一个问题是你的变量变量周围有错误的引号。您使用单引号,但变量仅在双引号内插入。

foreach ( $pages as $page ) {
    $page_title = $page->post_title;
    $trim_it_up = preg_replace('/[^\w]+/', '_', $page_title); //remove any punctuation, spaces, etc. from the title. 
    $trim_it_up = strtolower($trim_it_up);
    global ${"{$trim_it_up}_section_background_image"};
    ${"{$trim_it_up}_section_background_image"} = of_get_option( $trim_it_up .'_background_image');
};

,第二个必须做:

foreach ( $pages as $page ) { //begin a loop to set the background for the element based on it's dynamic ID
    $page_title = $page->post_title; //Get the Page title to use later
    $trim_it_up = preg_replace('/[^\w]+/', '_', $page_title); //remove any punctuation, spaces, etc. from the title. 
    $trim_it_up = strtolower($trim_it_up);
    global ${"{$trim_it_up}_section_background_image"};
    echo "#" . $trim_it_up . "{
    background-image: url(" . ${"{$trim_it_up}_section_background_image"} . ");
    }
    ";
}

但是,您可以只使用单个关联数组,而不是使用变量变量。然后你可以在每个函数的顶部声明这个变量global,或者从一个函数返回它并将它作为参数传递给另一个函数。

第一个功能:

global $section_background_image;
$section_background_image = array();
foreach ( $pages as $page ) {
    $page_title = $page->post_title;
    $trim_it_up = preg_replace('/[^\w]+/', '_', $page_title); //remove any punctuation, spaces, etc. from the title. 
    $trim_it_up = strtolower($trim_it_up);
    $section_background_image[$trim_it_up] = of_get_option( $trim_it_up .'_background_image');
};

第二功能:

global $section_background_image;
foreach ( $pages as $page ) { //begin a loop to set the background for the element based on it's dynamic ID
    $page_title = $page->post_title; //Get the Page title to use later
    $trim_it_up = preg_replace('/[^\w]+/', '_', $page_title); //remove any punctuation, spaces, etc. from the title. 
    $trim_it_up = strtolower($trim_it_up);
    global ${"{$trim_it_up}_section_background_image"};
    echo "#" . $trim_it_up . "{
    background-image: url(" . $section_background_image[$trim_it_up] . ");
    }
    ";
}