Forums

[resolved] Get pages instead of categories. (6 posts)

  1. stevenoi
    Member
    Posted 2 years ago #

    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.

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    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.

  3. stevenoi
    Member
    Posted 2 years ago #

    Thanks! Worked!

  4. stevenoi
    Member
    Posted 2 years ago #

    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?

  5. MichaelH
    Volunteer
    Posted 2 years ago #

    That's worthy of a new topic.

  6. stevenoi
    Member
    Posted 2 years ago #

    OK ;)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.