• Resolved stevenoi

    (@stevenoi)


    Hi, I got a little snippet of a theme options page. It displays a select multiple list of all categories you got.

    <?php
    	$news_categories = get_categories();
    	$news_categories[] = false;
    ?>
    
    <select id="<?php echo $data['category']; ?>" name="<?php echo $data['category']; ?>[]" multiple="multiple" style="height:150px;">
    <?php foreach($news_categories as $cat) : ?>
    <option value="<?php echo $cat->term_id; ?>" <?php if(is_array($val['category']) && in_array($cat->term_id, $val['category'])) echo ' selected="selected"'; ?>>
    <?php echo $cat->name; ?>
    </option>
    <?php endforeach; ?>
    </select>

    So I want to display instead of categories, pages. I changed somethings and now it looks like

    <?php
    	$news_categories = get_pages();
    	$news_categories[] = false;
    ?>
    
    <select id="<?php echo $data['category']; ?>" name="<?php echo $data['category']; ?>[]" multiple="multiple" style="height:150px;">
    <?php foreach($news_categories as $page) : ?>
    <option value="<?php echo $page->term_id; ?>" <?php if(is_array($val['category']) && in_array($page->term_id, $val['category'])) echo ' selected="selected"'; ?>>
    <?php echo $page->name; ?>
    </option>
    <?php endforeach; ?>
    </select>

    What is wrong in this code?

    – Steven.

Viewing 5 replies - 1 through 5 (of 5 total)
  • For one, pages don’t have term_ids or names, so $page->term_id and $page->name would be wrong.
    After this line:

    <?php foreach($news_categories as $page) : ?>

    put this

    echo "<pre>"; print_r($page); echo "</pre>";

    That will show you all the values in $page…you should see the $page->ID and $page->post_title among others.

    Thread Starter stevenoi

    (@stevenoi)

    Thanks! Worked!

    Thread Starter stevenoi

    (@stevenoi)

    One more question, when I want to get this option ( <?php echo get_option('catexclude');?> ) I need to place a “,” after each page name, so I can put them under exclude=. How can I do this?

    That’s worthy of a new topic.

    Thread Starter stevenoi

    (@stevenoi)

    OK ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get pages instead of categories.’ is closed to new replies.