2012-02-02 125 views
4

我想爲我的主題創建一個選項頁面,其中的選項頁面上有一個下拉列表,顯示所有類別名稱,選項值爲ID號該類別,以便在下拉菜單中顯示所有類別名稱,但是當您選擇類別並在前端回顯時,它會回顯類別標識。WordPress - 主題選項頁面與類別列表

我現在的代碼顯示了類別名稱列表,但也回顯了名稱到前端。我試圖修改它的身份證號碼,但我沒有運氣。

因此,只需總結一下,在選項頁面上,它需要在下拉列表中顯示類別名稱,但在前端應該回顯類別的ID號。

編輯:這是我使用創建的選項頁面的完整代碼 - 這一切坐鎮內線的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' => '', 
    )); 

?> 

下面的代碼,然後放置在「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']); } 
} 
?> 

然後,這是我用來顯示在前端保存變量的代碼:

<?php echo $tgh_categories; ?> 

回答

2

這是真的嗎?如果是這樣,改變cat_idcat_ID

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

EDIT

在此之後:

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

地址:

$wp_ids = array(); 

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

更改此:

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

而且改變這種:

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 '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

我不好,我忘了$

更改此:

"cid" => wp_ids, 

要:

"cid" => $wp_ids, 

NEW EDIT

更改此:

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

剛剛嘗試過這一點,恐怕它不工作:( – 2012-02-02 17:36:07

+0

看我上面編輯 – seanbreeden 2012-02-02 19:05:58

+0

編輯後的代碼顯示類別ID,然後顯示類別名稱,但不再列出它們在select中,而是出現在管理頁面的頂部,並在下面顯示一條錯誤消息:警告:無法修改標題信息 - 已在(線路861上的/wp-includes/functions.php中的(輸出在/themes/gh2012/functions.php:72處開始輸出) – 2012-02-02 20:21:37

1

你看過使用Options Framework

它使得管理面板的所有麻煩。作爲獎勵,還有內置的類別下拉列表功能。

+0

我同意。選項框架和'wp_dropdown_categories'([documentation](http://codex.wordpress.org/Function_Reference/wp_dropdown_categories))是迄今爲止最好也是最簡單的方法。 – Simon 2012-02-08 18:37:28

+0

我寧願找到我現有代碼的解決方案。 – 2012-02-10 18:52:10

+0

在這種情況下,我會建議在使用類別列表時查看選項框架文件。 – MBL 2012-02-11 01:21:29

0

我一直在尋找同樣的東西,並沒有發現哪裏實際回答你想要做的事情,甚至沒有這個線程。至少在這裏他們很接近,但是他們仍然在提供的問題代碼中破壞了一些功能。但是,我一直在爲此工作幾個小時,而且通常情況下,您工作的時間越長,解決方案的結果就越簡單。

我假設你正試圖讓我們WP_Query選擇並顯示來自在你的新選項頁面中選擇的類別的帖子,就像我一樣。我發現如果你使用的是名字,WP_Query不喜歡通過類別遞歸篩選,但如果你使用的是cat_ID,即使使用get_cat_ID也不會。因此,首先,這裏是出現在我的面前,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(). 
      ?> 

該片段從我的主題選項頁採取CAT_ID,並將其放入$feat1

需要的功能只有兩個變化我WP_Query 。從最初的PHP正在改變這一點:

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

這樣:

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

,然後從這個修改您的選擇<option>標籤:

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

我不必在我的header.php中放入任何東西來實現這個功能。因此,它所做的不是使用cat_name,而是使用cat_ID,但在下拉列表中填充用戶友好的cat_name,同時仍將每個條目與cat_ID關聯,我認爲這是您要查找的內容。我很抱歉,如果這很長,晚,或不是你想要的東西,但這是我第一次爲該網站做出貢獻,而這個帖子讓我開始尋找解決方案。