Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:12:43+00:00 2026-05-29T06:12:43+00:00

I am trying to create an options page for my theme, where on the

  • 0

I am trying to create an options page for my theme, where on the options page there is a dropdown list that displays all of the category names, with the option value as the ID number of the category, so that on the dropdown itself, it displays all of the category names, but then when you select your category, and echo this in the frontend, it echos the categories ID.

The code I have at the moment displays the list of category names, but also echos the name onto the frontend. I have tried to modify it for the ID number, but I’ve had no luck.

So just to summarize, on the options page, it needs to display the category names in the dropdown, but on the frontend it should echo the category’s ID number.

EDIT: This is the complete code I am using to create the options page – this all sits inside functions.php:

<?php
$themename = "TGH 2012";
$shortname = "tgh";

$categories = get_categories('hide_empty=0&orderby=name');
    $wp_cats = array();

    foreach ($categories as $category_list ) {
           $wp_cats[$category_list->cat_id] = $category_list->cat_name;
    }

    array_unshift($wp_cats, "Choose a category");


global $options;
$options = array (

    array(  "name" => "Homepage Options",
            "type" => "title"),

    array(  "type" => "open"),


array(  "name" => "Pick Categories",
        "desc" => "Choose a category from the list to do some interesting stuff.",
        "id" => $shortname."_categories",
        "type" => "select",
        "options" => $wp_cats,
        "std" => ""),

array(  "type" => "close")

);

function mytheme_add_admin() {

global $themename, $shortname, $options;

if ( $_GET['page'] == basename(__FILE__) ) {

    if ( 'save' == $_REQUEST['action'] ) {

            foreach ($options as $value) {
                update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }

            foreach ($options as $value) {
                if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }

            header("Location: themes.php?page=functions.php&saved=true");
            die;

    } else if( 'reset' == $_REQUEST['action'] ) {

        foreach ($options as $value) {
            delete_option( $value['id'] ); }

        header("Location: themes.php?page=functions.php&reset=true");
        die;

    }
}

add_theme_page($themename." Options", "".$themename." Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');

    }

    function mytheme_admin() {

    global $themename, $shortname, $options;

    if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
    if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';

?>
<div class="wrap">
<h2><?php echo $themename; ?> settings</h2>
<form method="post">
  <?php foreach ($options as $value) { 

    switch ( $value['type'] ) {

        case "open":
        ?>
  <table width="100%" border="0" style="background-color:#eef5fb; padding:10px;">
    <?php break;

        case "close":
        ?>
  </table>
  <br />
  <?php break;

        case "title":
        ?>
  <table width="100%" border="0" style="background-color:#dceefc; padding:5px 10px;">
  <tr>
    <td colspan="2"><h3 style="font-family:Georgia,'Times New Roman',Times,serif;"><?php echo $value['name']; ?></h3></td>
  </tr>
  <?php break;

        case 'text':
        ?>
  <tr>
    <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
    <td width="80%"><input style="width:400px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" /></td>
  </tr>
  <tr>
    <td><small><?php echo $value['desc']; ?></small></td>
  </tr>
  <tr>
    <td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <?php 
        break;

        case 'textarea':
        ?>
  <tr>
    <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
    <td width="80%"><textarea name="<?php echo $value['id']; ?>" style="width:400px; height:200px;" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>
</textarea></td>
  </tr>
  <tr>
    <td><small><?php echo $value['desc']; ?></small></td>
  </tr>
  <tr>
    <td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <?php 
        break;

        case 'select':
        ?>
  <tr>
    <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
    <td width="80%"><select style="width:240px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
        <?php foreach ($value['options'] as $option) { ?>
        <option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>
        <?php } ?>
      </select></td>
  </tr>
  <tr>
    <td><small><?php echo $value['desc']; ?></small></td>
  </tr>
  <tr>
    <td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <?php
        break;

        case "checkbox":
        ?>
  <tr>
    <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
    <td width="80%"><? if(get_settings($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = ""; } ?>
      <input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> /></td>
  </tr>
  <tr>
    <td><small><?php echo $value['desc']; ?></small></td>
  </tr>
  <tr>
    <td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <?php         break;


} 
}
?>

  <!--</table>-->

  <p class="submit">
    <input name="save" type="submit" value="Save changes" />
    <input type="hidden" name="action" value="save" />
  </p>
</form>
<form method="post">
  <p class="submit">
    <input name="reset" type="submit" value="Reset" />
    <input type="hidden" name="action" value="reset" />
  </p>
</form>
<?php
}

add_action('admin_menu', 'mytheme_add_admin'); ?>
<?php
if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '',
        'after_title' => '',
    ));

?>

The following code is then placed at the bottom of “header.php”:

<?php global $options;
foreach ($options as $value) {
    if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
}
?>

And then, this is the code I am using to show the saved variable on the front-end:

<?php echo $tgh_categories; ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:12:43+00:00Added an answer on May 29, 2026 at 6:12 am

    Is this how it really appears? If so, change cat_id to cat_ID

    foreach ($categories as $category_list ) {
           $wp_cats[$category_list->cat_id] = $category_list->cat_name;
    }
    

    EDIT

    After this:

        foreach ($categories as $category_list ) {
                   $wp_cats[$category_list->cat_ID] = $category_list->cat_name;
            }
    

    Add:

    $wp_ids = array();
    
    foreach ($categories as $category_list ) {
           $wp_ids[$category_list->cat_ID] = $category_list->cat_ID;
    }
    

    Change this:

        array(  "name" => "Pick Categories",
        "desc" => "Choose a category from the list to do some interesting stuff.",
        "id" => $shortname."_categories",
        "type" => "select",
        "options" => $wp_cats,
        "std" => ""),
    

    To this:

        array(  "name" => "Pick Categories",
        "desc" => "Choose a category from the list to do some interesting stuff.",
        "id" => $shortname."_categories",
        "cid" => wp_ids,
        "type" => "select",
        "options" => $wp_cats,
        "std" => ""),
    

    Also change this:

       case 'select':
        ?>
         <tr>
           <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
           <td width="80%"><select style="width:240px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
               <?php foreach ($value['options'] as $option) { ?>
               <option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>
               <?php } ?>
             </select></td>
         </tr>
         <tr>
           <td><small><?php echo $value['desc']; ?></small></td>
         </tr>
         <tr>
           <td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td>
         </tr>
         <tr>
           <td colspan="2">&nbsp;</td>
         </tr>
         <?php
               break;
    

    To this:

       case 'select':
        ?>
         <tr>
           <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
           <td width="80%"><select style="width:240px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
               <?php foreach ($value['options'] as $option) { ?>
               <option value="<?php echo $value['cid']; ?>" <?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>
               <?php } ?>
             </select></td>
         </tr>
         <tr>
           <td><small><?php echo $value['desc']; ?></small></td>
         </tr>
         <tr>
           <td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td>
         </tr>
         <tr>
           <td colspan="2">&nbsp;</td>
         </tr>
         <?php
               break;
    

    EDIT

    My bad, I forgot a $

    Change this:

    "cid" => wp_ids,
    

    To:

    "cid" => $wp_ids,
    

    NEW EDIT

    Change this:

     <tr>
       <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
       <td width="80%"><select style="width:240px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
           <?php foreach ($value['options'] as $option) { ?>
           <option value="<?php echo $value['cid']; ?>" <?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>
           <?php } ?>
         </select></td>
     </tr>
    

    To this:

     <tr>
       <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
       <td width="80%"><select style="width:240px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
           <?php 
    
    $categories = get_categories('hide_empty=0&orderby=name');
    
    foreach ($categories as $category_list ) {
    
      ?>
           <option value="<?php echo $category_list->cat_ID; ?>" <?php if ( get_settings( $value['id'] ) == $category_list->cat_name) { echo ' selected="selected"'; } elseif ($category_list->cat_name == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $category_list->cat_name; ?></option>
           <?php } ?>
         </select></td>
     </tr>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a page where there are two dropdown menus, and then
I'm trying to create a bulk edit page for an app that I'm working
hello i am using joomla and i am trying to create an option that
I'm trying to create an options menu in a C# forms project, and I'm
I am trying to create a trapezoid using user inputted options. I know my
I am trying to find the best option to create a simple progress bar
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create my first iPhone app that would play back audio. When I
The basic idea is to create a page that gives the user the ability
I'm trying to create a pretty-print html page. I need to force a page-break,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.