How to echo dynamically selected categories of back-end in front end using wordpress

I have in 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

Read More
   <?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(); ?>

The problem is that in index.php the $settings[] is not echoed properly.
For example

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

if i add var_dump or print_r it will echo something like Array {} (an empty array) instead of echoing the category name, should be

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

where Category3 is the selected category from the back-end by the user.
My functions.txt is http://pastebin.ca/2476028
Please help me i am doing my best on learning wordpress.

Related posts

Leave a Reply

1 comment

  1. ah got it where i should have var_dump, now i did it to
    var_dump($settings[“categoriesmain2”]) and it is echoing string(1)
    “1”, instead of the category. For the first one is string(1) “0” and
    third one string(1) “2” that means that is echoing the first select,
    second select, third select

    its doing that because of it is an array, you could get each value as an id then get it’s name using that id .

    UPDATE: Example

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

    This is just an example , you can do the same in yours .