WordPress - Theme options page with a list of categories

I’m trying to create a parameter page for my topic, where on the parameter page there is a drop-down list that displays all category names, with the parameter value as the category identification number, so in the drop-down menu it displays all category names, but then when you select your category and echo of this in the external interface, it displays category identifiers.

My code currently displays a list of category names, but also an echo name on the interface. I tried changing it for an ID number, but I was out of luck.

So, just to summarize on the options page, you need to display the category names in the drop-down list, but on the interface it should repeat the category identifier.

EDIT: this is the complete code that I use to create the parameters page - all this is 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 so, this is the code that I use to show the stored variable in the interface:

 <?php echo $tgh_categories; ?> 
+4
source share
3 answers

What does it look like? If so, change cat_id to cat_id

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

EDIT

Thereafter:

  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" => ""), 

For 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; 

For 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 $

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> 

For 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> 
+2
source

Have you discussed using Options ?

All you need to do is admin panel. And as a bonus, there are features built-in for the drop-down lists of categories.

+1
source

I was looking for the same thing, and did not find what the fact that you are trying to do, even this thread, actually answers. At least they are close here, but they still violate some of the functionality of your question code. However, I have been working on this for several hours, and as a rule, the longer you work on it, the easier the solution will be.

I made the assumption that you are trying to use WP_Query to select and display messages from a category selected on your new options page, like me. I found that WP_Query doesn't like recursively sifting categories if you use a name, but it will if you use cat_ID ... even when using get_cat_ID. So, firstly, here is my WP_Query, which appears on my front-page.php

 <?php $feat1= get_option('twp_feat_cat'); //this is the id of your option in the mega array you setup in functions.php $args=array('cat' => $feat1,'post_type' => 'post','post_status' => 'publish','posts_per_page' => 2,'caller_get_posts'=> 1); $my_query = null; $my_query = new WP_Query($args); $post_counter = 0; //so I can style last post differently with css if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); $post_counter++; ?> <article id="post-<?php the_ID(); ?>" <?php if ($post_counter == 1) post_class('fourcol first clearfix'); elseif ($post_counter == count( $posts )) post_class('fourcol last clearfix'); else post_class('fourcol clearfix'); ?> role="article"> <header class="article-header"> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> </header> <!-- end header section --> <section class="entry-content clearfix"> <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'original' ); $url = $thumb['0']; echo do_shortcode( '[rimg src="' . $url . '"]' ); ?> <?php the_excerpt(); ?> </section> <!-- end article section --> <footer class="article-footer"> <p class="tags"><?php //the_tags('<span class="tags-title">' . __('Tags:', 'bonestheme') . '</span> ', ', ', ''); ?></p> </footer> <!-- end article footer --> </article> <?php endwhile; } wp_reset_query(); // Restore global post data stomped by the_post(). ?> 

This snippet takes cat_ID from my theme options page and puts it in $feat1

Only two changes needed for your functions.php from the original change this:

 $wp_cats[$category_list->cat_id] = $category_list->cat_name; 

:

 $wp_cats[$category_list->cat_ID] = $category_list->cat_ID; 

and then change your <option> "→ tag:

 <option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option> 

:

 <option value="<?php echo $option;?>" <?php if (get_settings( $value['id'] ) == $option) { echo 'selected="selected"'; } ?>><?php echo get_cat_name($option); ?></option> 

I did not need to insert something into my .php header to make this work. So what does this mean, instead of using cat_name, it uses cat_ID, but fills in the user name cat_ in the drop-down list, but still associates each entry with cat_ID, which, I think, is what you were looking for. I apologize if this was a long, late or not quite what you were looking for, but this is my first contribution to the creation of the site, and it was this post that started me when I was looking for a solution.

0
source

Source: https://habr.com/ru/post/1394438/


All Articles