在后端创建设置,以便在前端仅打印选定的类别

时间:2015-11-24 作者:Sonali

我正在制作一个插件小部件。在后端,当我单击仅在前端显示的选定类别时。我只是做代码检查类别,但在那之后我不知道该怎么做。

            if(isset($_POST[\'exclude_main\']))
            {
                //print_r($_POST);
                //echo "sonali";
                $chk=implode(\',\',$_POST[\'exclude_main\']);
//print_r($chk);exit;
//header("location:http://localhost/wordpress/wp/wordpress/?value=".$chk);
            }?>

<?php
/*
Plugin Name: Thanks Plugin
*/


class wpt_widget extends WP_Widget
{

    function __construct()
    {
        parent::__construct(
            \'wpt_widget\',
            __(\'Thanks widget\', \'wpt_widget_domain\'),

        // Widget description
        array( \'description\' => __( \'Thanks widget\', \'wpt_widget_domain\' ), )
    );
    }
     public function actionWpPrintStyles()
    {
        if (!(false === is_active_widget(false, false, $this->id_base, true)))
        {
            wp_register_style(\'avhec-widget\', AVHEC_PLUGIN_URL . \'/css/avh-ec.widget.css\', array(), $this->core->version);
            wp_enqueue_style(\'avhec-widget\');
        }
    }
        // Creating widget front-end
        // This is where the action happens
    public function widget( $args, $instance )
    {
             extract($args);

        $selectedonly = $instance[\'selectedonly\'];

        $title = apply_filters( \'widget_title\', $instance[\'title\'] );
             $style = empty($instance[\'style\']) ? \'list\' : $instance[\'style\'];
             $included_cats = \'\';
        if ($instance[\'post_category\'])
        {
            $post_category = unserialize($instance[\'post_category\']);
            $children = array();
            if (!$instance[\'selectedonly\'])
            {
                foreach ($post_category as $cat_id)
                {
                    $children = array_merge($children, get_term_children($cat_id, \'category\'));
                }
            }
            $included_cats = implode(",", array_merge($post_category, $children));
        }
        if ($invert) {
            $inc_exc = \'exclude\';
        } else {
            $inc_exc = \'include\';
        }
        $cat_args = array($inc_exc => $included_cats);
          $before_title . $title . $after_title;
// before and after widget arguments are defined by themes
    echo $before_widget;
        //echo $this->core->comment;
        //echo $before_title . $title . $after_title;
        echo $args[\'before_title\'] . $title . $args[\'after_title\'];

    // This is where you run the code and display the output(browser)
    //echo __( \'Widget done by Plugin!\', \'wpb_widget_domain\' );
?>
 <?php


    echo $args[\'after_widget\'];
    }

    // Widget Backend

    public function form( $instance )
    {
        //print_r($instance);
        if ( isset( $instance[ \'title\' ] ) )
            {
                $title = $instance[ \'title\' ];
            }
        else
            {
                $title = __( \'New title\', \'wpb_widget_domain\' );
            }
            if(isset($instance[\'exclude_main\']))
            {
                $chkbox=$instance[\'exclude_main\'];
            }
            else
            {
                echo "ssssssssssss";
            }

            ?>
    <p>
    <label ><?php _e( \'Title:\' ); ?></label>
    <input name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />

    </p>
    <?php

          // displays the widget admin form
      $instance = wp_parse_args((array) $instance, array(\'title\' => \'\', \'rssimage\' => \'\', \'depth\' => 0));
        // Prepare data for display

        $selected_cats = (avhGetArrayValue($instance, \'post_category\') !== \'\') ? unserialize($instance[\'post_category\']) : false;

        ob_start();
        ?>
<table class="widefat">
                <thead>
                    <tr>
                        <th scope="col"><?php _e(\'Category\', \'UCE\'); ?></th>
                        <th scope="col"><?php _e(\'content shown\', \'UCE\'); ?></th>

                    </tr>
                </thead>
                <tbody id="the-list">
            <?php
            $args=array(
                \'hide_empty\' => 0,
                \'order\' => \'ASC\'
            );
            $cats = get_categories($args);
            $alt = 0;
            foreach( $cats as $cat ) {
                ?>
                <tr<?php if ( $alt == 1 ) { echo \' class="alternate"\'; $alt = 0; } else { $alt = 1; } ?>>
                    <th scope="row"><?php echo $cat->cat_name; //. \' (\' . $cat->cat_ID . \')\'; ?></th>
 <td><input type="checkbox" name="exclude_main[]" value="<?php echo $cat->cat_name ?>" <?php if ( in_array( \'-\' . $cat->cat_ID, $options[\'exclude_main\'] ) ) { echo \'checked="true" \'; } ?>/></td>
<!-- <input value="\' . $object->term_id . \'" type="checkbox" name="\' . $this->input_name . \'[\' . $object->term_id . \']" id="\' . $input_id . \'"\' .  ? \' checked="checked"\' : "") . \'/> </label>

 -->    </tr>

            <?php } ?>
            </table>
            <tr>
                <td><input type="submit" name="save" value="save"></td>
                </tr>
            <input type="hidden" name="ksuce" value="true" />

            <?php
       //$this->avh_wp_category_checklist($selected_cats, $this->number);
        ob_end_flush();
        echo \'</ul>\';
        echo \'</p>\';

        echo \'<p>\';
        avh_doWidgetFormCheckbox($this->get_field_id(\'invert_included\'), $this->get_field_name(\'invert_included\'), __(\'Exclude the selected categories\', \'avh-ec\'), (bool) avhGetArrayValue($instance, \'invert_included\'));
        echo \'</p>\';

       echo \'<input type="hidden" id="\' . $this->get_field_id(\'submit\') . \'" name="\' . $this->get_field_name(\'submit\') . \'" value="1" />\';
    }

    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance )
    {
         // update the instance\'s settings
        if (!isset($new_instance[\'submit\'])) {
            return false;
        }

        $instance = $old_instance;

        $instance[\'title\'] = strip_tags(stripslashes($new_instance[\'title\']));
        $instance[\'selectedonly\'] = isset($new_instance[\'selectedonly\']);
        $instance[\'count\'] = isset($new_instance[\'count\']);
        $instance[\'hierarchical\'] = isset($new_instance[\'hierarchical\']);
        $instance[\'hide_empty\'] = isset($new_instance[\'hide_empty\']);
        $instance[\'use_desc_for_title\'] = isset($new_instance[\'use_desc_for_title\']);
        $instance[\'sort_column\'] = strip_tags(stripslashes($new_instance[\'sort_column\']));
        $instance[\'sort_order\'] = strip_tags(stripslashes($new_instance[\'sort_order\']));
        $instance[\'style\'] = strip_tags(stripslashes($new_instance[\'style\']));
        $instance[\'rssfeed\'] = isset($new_instance[\'rssfeed\']);
        $instance[\'rssimage\'] = strip_tags(stripslashes($new_instance[\'rssimage\']));
        if (array_key_exists(\'all\', $new_instance[\'post_category\'])) {
            $instance[\'post_category\'] = false;
        } else {
            $instance[\'post_category\'] = serialize($new_instance[\'post_category\']);
        }
        $instance[\'depth\'] = (int) $new_instance[\'depth\'];
        if ($instance[\'depth\'] < 0 || 11 < $instance[\'depth\']) {
            $instance[\'depth\'] = 0;
        }
        $instance[\'invert_included\'] = isset($new_instance[\'invert_included\']);

        return $instance;
    }

  public function avh_wp_category_checklist($selected_cats, $number)
    {
        $walker = new AVH_Walker_Category_Checklist();
        $walker->number = $number;
        $walker->input_id = $this->get_field_id(\'post_category\');
        $walker->input_name = $this->get_field_name(\'post_category\');
        $walker->li_id = $this->get_field_id(\'category--1\');

        $args = array(\'taxonomy\' => \'category\', \'descendants_and_self\' => 0, \'selected_cats\' => $selected_cats, \'popular_cats\' => array(), \'walker\' => $walker, \'checked_ontop\' => true, \'popular_cats\' => array());

        if (is_array($selected_cats))
        {
            $args[\'selected_cats\'] = $selected_cats;
        } else
         {
            $args[\'selected_cats\'] = array();
        }

        $categories = $this->core->getCategories();
        $_categories_id = $this->core->getCategoriesId($categories);

        // Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
        $checked_categories = array();
        foreach ($args[\'selected_cats\'] as $key => $value)
        {
            if (isset($_categories_id[$key]))
             {
                $category_key = $_categories_id[$key];
                $checked_categories[] = $categories[$category_key];
                unset($categories[$category_key]);
            }
        }

        // Put checked cats on top
        echo $walker->walk($checked_categories, 0, $args);
        // Then the rest of them
        echo $walker->walk($categories, 0, $args);
    }


}

// Class wpt_widget ends here

    // Register and load the widget
    function wpt_load_widget() {
        register_widget( \'wpt_widget\' );
    }
add_action( \'widgets_init\', \'wpt_load_widget\' );

1 个回复
SO网友:flomei

哇,我们不需要所有这些代码(至少我没有读过)。让我们从概念的角度来看你的问题。

如果我理解正确,您希望在后端选择要在前端显示的类别。

后端的步骤如下:

在后端构建一些东西来选择类别将刚才选择的内容(逗号分隔的列表可能是一种方式)保存到数据库(wp_options 将是保存它的位置)在前端:

检索保存的信息(从wp_options)explode() 逗号分隔的字符串)将此数组作为参数传递给所选查询(get_posts()WP_Query() 例如)仅从类别中检索帖子并使用它们

相关推荐

针对在widgets.php页面上的WP指针可拖动的小工具的提示

我正在更新我编写的名为Feature Me的小部件插件,并为第一次安装插件的用户添加WordPress指针,以帮助他们在安装插件后找到小部件。然而,我遇到了一个问题。指针必须有一个目标,该目标是一个ID# 或者一个班级. (正常情况下可以)。但是在widget页面上,所有widget都以widget-[somenumber]. 如果我知道[某个数字]是什么,我可以毫无问题地瞄准它,但99%的时候,我不知道数字是什么,因为它实际上取决于插件的安装顺序。在其他网站上,这是小部件的ID:希望你能看到我的问题。在