2013-11-14 6 views
1

를 사용하여 프론트 엔드에서 백엔드의 동적 선택 범주를 반향하는 방법내가 functions.php에있는 워드 프레스

//Example of select field 
    $this->settings['select'] = array(
     'section' => 'general', 
     'title' => __('Example Select'), 
     'desc' => __('This is a description for the drop-down.'), 
     'type' => 'select', 
     'std'  => '', 
     'choices' => array(
      'choice1' => 'Choice 1', 
      'choice2' => 'Choice 2', 
      'choice3' => 'Choice 3' 
     ) 
    ); 

//Here i have managed to echo the categories dynamically in the back-end 
$args = array(
      'orderby' => 'name', 
      'order' => 'ASC', 
      'hide_empty' => 0 
     ); 
$categories = get_categories($args); 
$categories_name = array(); 
foreach($categories as $category){ 
    $categories_name[] = $category->name; 
} 

    $this->settings['categoriesmain1'] = array(
      'section' => 'general', 
      'title' => __('Select Left Block Category'), 
      'desc' => __('Here you can select the main category for main left block.'), 
      'type' => 'select', 
      'std'  => '', 
      'choices' => $categories_name // this returns the category names in a select field 
     ); 
    $settings = get_option('mytheme_options'); 
    $my_choices_cat1 = $settings['categoriesmain1']; 

    $this->settings['categoriesmain2'] = array(
      'section' => 'general', 
      'title' => __('Select Center Block Category'), 
      'desc' => __('Here you can select the main category for main center block.'), 
      'type' => 'select', 
      'std'  => '', 
      'choices' => $categories_name 
     ); 
    $settings = get_option('mytheme_options'); 
    $my_choices_cat2 = $settings['categoriesmain2']; 

    $this->settings['categoriesmain3'] = array(
      'section' => 'general', 
      'title' => __('Select Right Block Category'), 
      'desc' => __('Here you can select the main category for main right block.'), 
      'type' => 'select', 
      'std'  => '', 
      'choices' => $categories_name 
     ); 
    $settings = get_option('mytheme_options'); 
    $my_choices_cat3 = $settings['categoriesmain3']; 

index.php를

<?php $settings = get_option('mytheme_options'); 
     query_posts('category_name='.$settings["categoriesmain1"].'&posts_per_page=1'); 
while (have_posts()) : the_post(); ?> 
    <div class="boxes-third boxes-first"> 
     <div class="boxes-padding"> 
      <div class="bti"> 
       <div class="featured-images"><?php the_post_thumbnail(array(300,300)); ?></div> 
       <div class="featured-titles"><?php echo get_the_title($post->ID); ?></div> 
      </div> 
      <div class="featured-text"><?php the_content('',FALSE,''); ?></div> 
     </div> 
     <span class="box-arrow"></span> 
    </div> 
    <?php endwhile; ?> 
    <?php wp_reset_query(); ?> 

    <?php $settings = get_option('mytheme_options'); 
     query_posts('category_name='.$settings["categoriesmain2"].'&posts_per_page=1'); 
while (have_posts()) : the_post(); ?> 
    <div class="boxes-third"> 
     <div class="boxes-padding"> 
      <div class="bti"> 
       <div class="featured-images"><?php the_post_thumbnail(array(300,300)); ?></div> 
       <div class="featured-titles"><?php echo get_the_title($post->ID); ?></div> 
      </div> 
      <div class="featured-text"><?php the_content('',FALSE,''); ?></div> 
     </div> 
     <span class="box-arrow"></span> 
    </div> 
    <?php endwhile; ?> 
    <?php wp_reset_query(); ?> 

    <?php $settings = get_option('mytheme_options'); 
     query_posts('category_name='.$settings["categoriesmain3"].'&posts_per_page=1'); 
while (have_posts()) : the_post(); ?> 
    <div class="boxes-third boxes-last"> 
     <div class="boxes-padding"> 
      <div class="bti"> 
       <div class="featured-images"><?php the_post_thumbnail(array(300,300)); ?></div> 
       <div class="featured-titles"><?php echo get_the_title($post->ID); ?></div> 
      </div> 
      <div class="featured-text"><?php the_content('',FALSE,''); ?></div> 
     </div> 
     <span class="box-arrow"></span> 
    </div> 
    <?php endwhile; ?> 
    <?php wp_reset_query(); ?> 

문제는 점에서 index.php를 $settings[]이 제대로 에코되지 않습니다. Category3가 선택되고 난 var_dump 또는 print_r가 배열 {} (빈 배열) 같은 것을 메아리 추가 예

query_posts('category_name='.$settings["categoriesmain3"].'&posts_per_page=1');, 

대신 카테고리 명을 반향 ,

query_posts('category_name=Category3&posts_per_page=1'); 

되어야 카테고리를 사용자가 백엔드에서 내 functions.txt는 http://pastebin.ca/2476028 입니다. 제발 저를 도울 때 WordPress에 최선을 다하고 있습니다. 내가 위해서 var_dump를해야하는 위치

+2

query_posts를 사용하지 마십시오. 성능이 매우 좋지 않습니다 ([info] (http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts- vs-get-posts)) – janw

+0

'var_dump ($ settings)'시도 틀린 옵션을 얻으려고 노력합니다. 당신은 어떤 종류의 설정 프레임 워크를 사용합니다. 어느 것이 든 말해야합니다. – janw

+0

@janw 예를 들어, query_posts ('category_name ='. $ settings [ "categoriesmain2"]. & posts_per_page = 1 ');'뒤에'var_dump ($ settings)'를 추가하고 내 모든 백엔드 설정을 그 이유를 이해하지 못한다면, 다른 옵션이 제대로 작동하기 때문에 범주를 지적해야합니다. 텍스트/체크 박스와 같습니다. select만이 문제입니다. – Adrian

답변

2

아 이제 내가 위해서 var_dump로했다 ($ 설정 [ "categoriesmain2"])과 (1) "1"대신 카테고리의 문자열을 울리는, 그것은을 얻었다. 첫 번째는 캐릭터 (1) "0"과 세번째 스트링 (1)이기 때문 "2"즉 즉, 이 선택 두 번째, 세 번째는가 그것 때문에 고

선택한 1 선택 반향 수단 배열 인 경우 각 값을 ID로 가져온 다음 해당 ID를 사용하여 이름을 가져올 수 있습니다.

UPDATE : 예이 그냥 예입니다

// an array contains cats ids 
$categories_ids = array(1,5,7,3); 

// sql stmnt to get all info about each id 
$sql = 'select * from categories where id in("'.implode('",'", array_values($categories_ids)).'")'; 

, 당신은 당신에 동일한 기능을 수행 할 수 있습니다.

+0

음 ... 나는 처음부터 꽤있다. 몇 가지 예제 코드로 나를 도울 수 있습니까? – Adrian

+0

나는 대답을 편집했다. –