我在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_dump
或print_r
它會響應類似陣列{}(空數組)代替呼應的類別名稱,應該是
query_posts('category_name=Category3&posts_per_page=1');
其中Category3
是所選擇的用戶從後端分類。 我的功能.txt是http://pastebin.ca/2476028 請幫助我,我正在盡力學習wordpress。
不使用query_posts它對性能非常不利([info](http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts- vs-get-posts)) – janw
嘗試'var_dump($ settings)'我想你試着弄錯了一個選項。你使用某種設置框架,你應該說哪一個。 – janw
@janw是的,我在'query_posts('category_name ='。$ settings [「categoriesmain2」]。'&posts_per_page = 1')後面添加了'var_dump($ settings)'';'並在我的後端設置中回顯但不明白爲什麼,它應該指向該類別,因爲其他選項正常工作。像文本/複選框。只有選擇是一個問題。 – Adrian