【问题标题】:How to echo dynamically selected categories of back-end in front end using wordpress如何使用wordpress在前端回显动态选择的后端类别
【发布时间】:2013-11-27 07:07:46
【问题描述】:

我在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[] 没有正确回显。 例如

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

如果我添加 var_dumpprint_r 它将回显类似 Array {}(一个空数组)而不是回显类别名称,应该是

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

其中Category3 是用户从后端选择的类别。 我的functions.txt是http://pastebin.ca/2476028 请帮助我,我正在尽最大努力学习 wordpress。

【问题讨论】:

  • 不要使用 query_posts 这对性能非常不利 (info)
  • 尝试var_dump($settings) 我认为您尝试选择错误的选项。你使用某种设置框架,你应该说是哪一个。
  • @janw 是的,我在query_posts('category_name='.$settings["categoriesmain2"].'&amp;posts_per_page=1'); 之后添加了var_dump($settings),并且正在呼应我所有的后端设置,但不明白为什么,它应该指向类别,对于另一个选项工作正常。喜欢文本/复选框。仅用于选择是一个问题。
  • 啊,我应该有 var_dump,现在我做到了var_dump($settings["categoriesmain2"]),它正在回显string(1) "1",而不是类别。第一个是string(1) "0",第三个是string(1) "2",这意味着它与第一个选择、第二个选择、第三个选择相呼应。

标签: php arrays wordpress


【解决方案1】:

啊,我应该有 var_dump,现在我做到了 var_dump($settings["categoriesmain2"]) 它正在回显 string(1) “1”,而不是类别。第一个是 string(1) "0" 和 第三个字符串(1)“2”表示正在回显第一个选择, 第二次选择,第三次选择

这样做是因为它是一个数组,您可以将每个值作为 id 获取,然后使用该 id 获取它的名称。

更新:示例

// 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) ).'")';

这只是一个例子,你可以在你的例子中做同样的事情。

【讨论】:

  • 嗯.. 我刚刚开始。你能帮我一些示例代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
  • 2013-02-03
相关资源
最近更新 更多