Note that there are some explanatory texts on larger screens.

plurals
  1. POWordPress - Theme Options Page with Category List
    text
    copied!<p>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.</p> <p>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. </p> <p>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.</p> <p>EDIT: This is the complete code I am using to create the options page - this all sits inside functions.php:</p> <pre><code>&lt;?php $themename = "TGH 2012"; $shortname = "tgh"; $categories = get_categories('hide_empty=0&amp;orderby=name'); $wp_cats = array(); foreach ($categories as $category_list ) { $wp_cats[$category_list-&gt;cat_id] = $category_list-&gt;cat_name; } array_unshift($wp_cats, "Choose a category"); global $options; $options = array ( array( "name" =&gt; "Homepage Options", "type" =&gt; "title"), array( "type" =&gt; "open"), array( "name" =&gt; "Pick Categories", "desc" =&gt; "Choose a category from the list to do some interesting stuff.", "id" =&gt; $shortname."_categories", "type" =&gt; "select", "options" =&gt; $wp_cats, "std" =&gt; ""), array( "type" =&gt; "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&amp;saved=true"); die; } else if( 'reset' == $_REQUEST['action'] ) { foreach ($options as $value) { delete_option( $value['id'] ); } header("Location: themes.php?page=functions.php&amp;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 '&lt;div id="message" class="updated fade"&gt;&lt;p&gt;&lt;strong&gt;'.$themename.' settings saved.&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt;'; if ( $_REQUEST['reset'] ) echo '&lt;div id="message" class="updated fade"&gt;&lt;p&gt;&lt;strong&gt;'.$themename.' settings reset.&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt;'; ?&gt; &lt;div class="wrap"&gt; &lt;h2&gt;&lt;?php echo $themename; ?&gt; settings&lt;/h2&gt; &lt;form method="post"&gt; &lt;?php foreach ($options as $value) { switch ( $value['type'] ) { case "open": ?&gt; &lt;table width="100%" border="0" style="background-color:#eef5fb; padding:10px;"&gt; &lt;?php break; case "close": ?&gt; &lt;/table&gt; &lt;br /&gt; &lt;?php break; case "title": ?&gt; &lt;table width="100%" border="0" style="background-color:#dceefc; padding:5px 10px;"&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;h3 style="font-family:Georgia,'Times New Roman',Times,serif;"&gt;&lt;?php echo $value['name']; ?&gt;&lt;/h3&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php break; case 'text': ?&gt; &lt;tr&gt; &lt;td width="20%" rowspan="2" valign="middle"&gt;&lt;strong&gt;&lt;?php echo $value['name']; ?&gt;&lt;/strong&gt;&lt;/td&gt; &lt;td width="80%"&gt;&lt;input style="width:400px;" name="&lt;?php echo $value['id']; ?&gt;" id="&lt;?php echo $value['id']; ?&gt;" type="&lt;?php echo $value['type']; ?&gt;" value="&lt;?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?&gt;" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;small&gt;&lt;?php echo $value['desc']; ?&gt;&lt;/small&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;"&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;?php break; case 'textarea': ?&gt; &lt;tr&gt; &lt;td width="20%" rowspan="2" valign="middle"&gt;&lt;strong&gt;&lt;?php echo $value['name']; ?&gt;&lt;/strong&gt;&lt;/td&gt; &lt;td width="80%"&gt;&lt;textarea name="&lt;?php echo $value['id']; ?&gt;" style="width:400px; height:200px;" type="&lt;?php echo $value['type']; ?&gt;" cols="" rows=""&gt;&lt;?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?&gt; &lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;small&gt;&lt;?php echo $value['desc']; ?&gt;&lt;/small&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;"&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;?php break; case 'select': ?&gt; &lt;tr&gt; &lt;td width="20%" rowspan="2" valign="middle"&gt;&lt;strong&gt;&lt;?php echo $value['name']; ?&gt;&lt;/strong&gt;&lt;/td&gt; &lt;td width="80%"&gt;&lt;select style="width:240px;" name="&lt;?php echo $value['id']; ?&gt;" id="&lt;?php echo $value['id']; ?&gt;"&gt; &lt;?php foreach ($value['options'] as $option) { ?&gt; &lt;option&lt;?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?&gt;&gt;&lt;?php echo $option; ?&gt;&lt;/option&gt; &lt;?php } ?&gt; &lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;small&gt;&lt;?php echo $value['desc']; ?&gt;&lt;/small&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;"&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;?php break; case "checkbox": ?&gt; &lt;tr&gt; &lt;td width="20%" rowspan="2" valign="middle"&gt;&lt;strong&gt;&lt;?php echo $value['name']; ?&gt;&lt;/strong&gt;&lt;/td&gt; &lt;td width="80%"&gt;&lt;? if(get_settings($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = ""; } ?&gt; &lt;input type="checkbox" name="&lt;?php echo $value['id']; ?&gt;" id="&lt;?php echo $value['id']; ?&gt;" value="true" &lt;?php echo $checked; ?&gt; /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;small&gt;&lt;?php echo $value['desc']; ?&gt;&lt;/small&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;"&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;?php break; } } ?&gt; &lt;!--&lt;/table&gt;--&gt; &lt;p class="submit"&gt; &lt;input name="save" type="submit" value="Save changes" /&gt; &lt;input type="hidden" name="action" value="save" /&gt; &lt;/p&gt; &lt;/form&gt; &lt;form method="post"&gt; &lt;p class="submit"&gt; &lt;input name="reset" type="submit" value="Reset" /&gt; &lt;input type="hidden" name="action" value="reset" /&gt; &lt;/p&gt; &lt;/form&gt; &lt;?php } add_action('admin_menu', 'mytheme_add_admin'); ?&gt; &lt;?php if ( function_exists('register_sidebar') ) register_sidebar(array( 'before_widget' =&gt; '&lt;li id="%1$s" class="widget %2$s"&gt;', 'after_widget' =&gt; '&lt;/li&gt;', 'before_title' =&gt; '', 'after_title' =&gt; '', )); ?&gt; </code></pre> <p>The following code is then placed at the bottom of "header.php":</p> <pre><code>&lt;?php global $options; foreach ($options as $value) { if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); } } ?&gt; </code></pre> <p>And then, this is the code I am using to show the saved variable on the front-end:</p> <pre><code>&lt;?php echo $tgh_categories; ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload