2013-11-14 31 views
1

我在functions.php的回聲在前端後端的動態選擇的類別如何使用Wordpress

//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[]未正確回顯。 例如

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

如果我添加var_dumpprint_r它會響應類似陣列{}(空數組)代替呼應的類別名稱,應該是

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

其中Category3是所選擇的用戶從後端分類。 我的功能.txt是http://pastebin.ca/2476028 請幫助我,我正在盡力學習wordpress。

+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)'';'並在我的後端設置中回顯但不明白爲什麼,它應該指向該類別,因爲其他選項正常工作。像文本/複選框。只有選擇是一個問題。 – Adrian

回答

2

啊得到了它,我應該擁有的var_dump,現在我這樣做是爲了 的var_dump($設置[ 「categoriesmain2」]),它是呼應字符串(1) 「1」,而不是類別。對於第一個是串(1)爲「0」和 第三個串(1)「2」是指呼應第一選擇, 第二選擇,第三選擇

其做的,由於它是一個數組,您可以將每個值作爲一個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

我編輯了答案 –