【问题标题】:WordPress - Theme Options Page with Category ListWordPress - 带有类别列表的主题选项页面
【发布时间】:2012-02-25 08:47:04
【问题描述】:

我正在尝试为我的主题创建一个选项页面,在选项页面上有一个显示所有类别名称的下拉列表,选项值作为类别的 ID 号,以便在下拉列表中它本身会显示所有类别名称,但是当您选择类别并在前端回显它时,它会回显类别 ID。

我目前的代码显示类别名称列表,但也将名称回显到前端。我试图修改它的 ID 号,但我没有运气。

所以总结一下,在选项页面上,它需要在下拉列表中显示类别名称,但在前端它应该回显类别的 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; ?>

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    真的是这样吗?如果是这样,请将cat_id 更改为cat_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>
    

    【讨论】:

    • 编辑后的代码显示类别 ID,然后显示类别名称,但不再在选择中列出它们。相反,它出现在管理页面的顶部,并显示以下错误消息:警告:无法修改标头信息 - 标头已由 /wp-includes/functions 中的(输出开始于 /themes/gh2012/functions.php:72)发送。 php 在第 861 行'
    • 我只是确保您能够提取 ID。由于它使用 $category->term_id 进行拉取,因此您网站上的代码似乎无法显示例程传递的信息。请参阅上面的EDIT 2
    • 我没有看到您的第二次编辑 - 它没有发布还是您只是更改了现有代码?
    • 我刚刚发现了一些可能会影响问题的附加代码,我编辑了我的原始帖子并添加了它,对不起!
    • 我现在已经发布了我的functions.php的全部内容 - 我希望这将有助于进一步证明这个问题。
    【解决方案2】:

    您是否考虑过使用Options Framework

    它消除了制作管理面板的所有麻烦。作为奖励,还有内置用于类别下拉列表的功能。

    【讨论】:

    • 我同意。 Options Framework 和 wp_dropdown_categories (documentation) 是迄今为止最好和最简单的方法。
    • 在这种情况下,我建议在选项框架文件中查看它们对类别列表的使用情况。
    【解决方案3】:

    我一直在寻找同样的东西,但没有找到真正回答你想要做的事情的地方,甚至没有这个线程。至少在这里它们很接近,但它们仍然会破坏您提供的问题代码中的某些功能。但是,我已经为此工作了几个小时,而且通常情况下,您工作的时间越长,解决方案就越简单。

    我假设您正在尝试让我们 WP_Query 从您的新选项页面中选择的类别中选择和显示帖子,就像我一样。我发现,如果您使用名称,WP_Query 不喜欢递归地筛选类别,但如果您使用的是 cat_ID……即使使用 get_cat_ID 也不喜欢。所以首先,这是我的 WP_Query,它出现在我的 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().
                ?>
    

    这个 sn-p 从我的主题选项页面中获取 cat_ID 并将其放入 $feat1

    对您的functions.php 原始文件的唯一两项更改是:

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

    到这里:

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

    然后从此修改您的选择&lt;option&gt;标签:

    <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 相关联,我认为这就是您要查找的内容。很抱歉,如果这太长、太晚或不是您想要的,但这是我第一次为该网站做出贡献,正是这篇文章让我开始寻找解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      相关资源
      最近更新 更多