Creating a Tabbed Widget

时间:2015-06-22 作者:James Gacuca

我已经设置了我的小部件来从自定义帖子类型中检索数据。我正在使用WP Query显示数据,但它仍然没有显示。

我还将文件设置为要点,以便于查找https://gist.github.com/609fc97c0352820eb762.git

足球俱乐部电影Xbox游戏布道阵列(“足球俱乐部”),);

                                            //Custom query
                                            $tabbedquery = new WP_Query($args);
                                            ?>

                                            <?php 
                                                if ($tabbedquery->have_posts()) {
                                                    while ($tabbedquery->have_posts()) {
                                                        $tabbedquery->the_post();

                                                        ?>

                                                        <p><?php the_excerpt(); ?></p>

                                                    <?php
                                                    }
                                                }

                                                /** Restore original postdata */
                                                wp_reset_postdata();
                                        }

                                        function f557_posts_widget_init(){
                                            register_sidebar_widget(\'Recent Soccer Clubs\', \'f557query_posts_widget\');
                                        }
                                        add_action(\'widgets_init\', \'f557_posts_widget_init\');

                                        ?>
                              </div>
                            </div>

                            <a href="#panel2" class="show-for-small-only"><i class="fa fa-calendar"></i> Movies</a>
                            <div id="panel2" class="content">
                                <div class="content-box section-box">
                                    <!--Insert PHP Query Here-->
                              </div>
                            </div>

                            <a href="#panel3" class="show-for-small-only"><i class="fa fa-bed"></i> Xbox Games</a>
                            <div id="panel3" class="content">
                                <div class="content-box section-box">
                                    <!--Insert PHP Query Here-->
                              </div>
                            </div>


                        </dd>
                    </dl>
                </div>
        </article>

1 个回复
最合适的回答,由SO网友:Mitul 整理而成

请找到代码,你只需要为你的设计添加js和css

它将添加三个自定义贴子,并将小部件添加到小部件区域,您可以将其拖放到任何侧栏中。

<?php
/**
 * Plugin Name:       Custom Tabbed Plugin
 * Description:       This is testing of the custom tabbed plugin
 * Version:           1.0.2
 * Author:            James Van Waza
 * Author URI:        http://jamesvanwaza.com
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 */

/**
 * Registers a new post type
 * @uses $wp_post_types Inserts new post type object into the list
 *
 * @param string  Post type key, must not exceed 20 characters
 * @param array|string  See optional args description above.
 * @return object|WP_Error the registered post type object, or an error object
 */

add_action(\'init\', \'aip_popup_init\');

function add_custom_post_type(){
    $labels = array(
        \'name\' => __(\'Soccer Clubs\', \'text-domain\') ,
        \'singular_name\' => __(\'Soccer Club\', \'text-domain\') ,
        \'add_new\' => _x(\'Add New Soccer Club\', \'text-domain\', \'text-domain\') ,
        \'add_new_item\' => __(\'Add New Soccer Club\', \'text-domain\') ,
        \'edit_item\' => __(\'Edit Soccer Club\', \'text-domain\') ,
        \'new_item\' => __(\'New Soccer Club\', \'text-domain\') ,
        \'view_item\' => __(\'View Soccer Club\', \'text-domain\') ,
        \'search_items\' => __(\'Search Soccer Clubs\', \'text-domain\') ,
        \'not_found\' => __(\'No Soccer Clubs found\', \'text-domain\') ,
        \'not_found_in_trash\' => __(\'No Soccer Clubs found in Trash\', \'text-domain\') ,
        \'parent_item_colon\' => __(\'Parent Soccer Club:\', \'text-domain\') ,
        \'menu_name\' => __(\'Soccer Clubs\', \'text-domain\')
    );
    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => false,
        \'description\' => \'description\',
        \'taxonomies\' => array() ,
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'show_in_admin_bar\' => true,
        \'menu_position\' => null,
        \'menu_icon\' => null,
        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => array(
            \'slug\' => \'soccer-club\'
        ) ,
        \'capability_type\' => \'post\',
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'author\',
            \'thumbnail\',
            \'excerpt\',
            \'custom-fields\',
            \'trackbacks\',
            \'comments\',
            \'revisions\',
            \'page-attributes\',
            \'post-formats\'
        )
    );
    register_post_type(\'soccer-club\', $args);
    $labels = array(
        \'name\' => __(\'Movies\', \'text-domain\') ,
        \'singular_name\' => __(\'Movie\', \'text-domain\') ,
        \'add_new\' => _x(\'Add New Movie\', \'text-domain\', \'text-domain\') ,
        \'add_new_item\' => __(\'Add New Movie\', \'text-domain\') ,
        \'edit_item\' => __(\'Edit Movie\', \'text-domain\') ,
        \'new_item\' => __(\'New Movie\', \'text-domain\') ,
        \'view_item\' => __(\'View Movie\', \'text-domain\') ,
        \'search_items\' => __(\'Search Movies\', \'text-domain\') ,
        \'not_found\' => __(\'No Movies found\', \'text-domain\') ,
        \'not_found_in_trash\' => __(\'No Movies found in Trash\', \'text-domain\') ,
        \'parent_item_colon\' => __(\'Parent Movie:\', \'text-domain\') ,
        \'menu_name\' => __(\'Movies\', \'text-domain\')
    );
    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => false,
        \'description\' => \'description\',
        \'taxonomies\' => array() ,
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'show_in_admin_bar\' => true,
        \'menu_position\' => null,
        \'menu_icon\' => null,
        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => array(
            \'slug\' => \'movies\'
        ) ,
        \'capability_type\' => \'post\',
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'author\',
            \'thumbnail\',
            \'excerpt\',
            \'custom-fields\',
            \'trackbacks\',
            \'comments\',
            \'revisions\',
            \'page-attributes\',
            \'post-formats\'
        )
    );
    register_post_type(\'movies\', $args);


    $labels = array(
        \'name\' => __(\'Xbox Games\', \'text-domain\') ,
        \'singular_name\' => __(\'Xbox Game\', \'text-domain\') ,
        \'add_new\' => _x(\'Add New Xbox Game\', \'text-domain\', \'text-domain\') ,
        \'add_new_item\' => __(\'Add New Xbox Game\', \'text-domain\') ,
        \'edit_item\' => __(\'Edit Xbox Game\', \'text-domain\') ,
        \'new_item\' => __(\'New Xbox Game\', \'text-domain\') ,
        \'view_item\' => __(\'View Xbox Game\', \'text-domain\') ,
        \'search_items\' => __(\'Search Xbox Games\', \'text-domain\') ,
        \'not_found\' => __(\'No Xbox Games found\', \'text-domain\') ,
        \'not_found_in_trash\' => __(\'No Xbox Games found in Trash\', \'text-domain\') ,
        \'parent_item_colon\' => __(\'Parent Xbox Game:\', \'text-domain\') ,
        \'menu_name\' => __(\'Xbox Games\', \'text-domain\')
    );
    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => false,
        \'description\' => \'description\',
        \'taxonomies\' => array() ,
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'show_in_admin_bar\' => true,
        \'menu_position\' => null,
        \'menu_icon\' => null,
        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => array(
            \'slug\' => \'xbox-games\'
        ) ,
        \'capability_type\' => \'post\',
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'author\',
            \'thumbnail\',
            \'excerpt\',
            \'custom-fields\',
            \'trackbacks\',
            \'comments\',
            \'revisions\',
            \'page-attributes\',
            \'post-formats\'
        )
    );
    register_post_type(\'xbox-games\', $args);
}

// creating a widget
    class f557TabberWidget extends WP_Widget {


        function __construct() {
            parent::__construct(
                \'f557TabberWidget\', // Base ID
                __( \'Taber Title\', \'text_domain\' ), // Name
                array( \'description\' => __( \'A Foo Widget\', \'text_domain\' ), ) // Args
            );
        }

        function f557TabberWidget() {
                $widget_ops = array(
                    \'classname\' => \'f557TabberWidget\',
                    \'description\' => \'Simple jQuery Tabber Widget\'
                );
                $this->WP_Widget(
                        \'f557TabberWidget\',
                        \'f557Beginner Tabber Widget\',
                        $widget_ops
                );
        }

        function widget($args, $instance) {
                ?>
                    <div class="small-12 columns">
                        <ul class="tabs show-for-medium-up" data-tab>
                                <li>Soccer Clib</li>
                                <li>Movie</li>
                                <li>Xbox Games</li>
                        </ul>

                        <div class="tab1">
                            <?php
                            $args = array(
                                \'post_type\' => \'soccer-club\',
                                \'posts_per_page\' => 2
                            );
                            $soccer = new WP_Query($args);
                            if( $soccer->have_posts() ) {
                                while ($soccer->have_posts()) : $soccer->the_post(); ?>
                                    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
                                <?php
                                endwhile;
                            }
                            ?>
                        </div>

                        <div class="tab2">
                            <?php
                            $args = array(
                                \'post_type\' => \'movies\',
                                \'posts_per_page\' => 2
                            );
                            $soccer = new WP_Query($args);
                            if( $soccer->have_posts() ) {
                                while ($soccer->have_posts()) : $soccer->the_post(); ?>
                                    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
                                <?php
                                endwhile;
                            }
                            ?>
                        </div>

                        <div class="tab3">
                            <?php
                            $args = array(
                                \'post_type\' => \'xbox-games\',
                                \'posts_per_page\' => 2
                            );
                            $soccer = new WP_Query($args);
                            if( $soccer->have_posts() ) {
                                while ($soccer->have_posts()) : $soccer->the_post(); ?>
                                    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
                                <?php
                                endwhile;
                            }
                            ?>
                        </div>
                    </div>

            <?php
        }
        public function form( $instance ) {

        }
        public function update( $new_instance, $old_instance ) {           
        }
    }

    add_action(\'widgets_init\', function(){
        register_widget( \'f557TabberWidget\' );
    });

?>

结束

相关推荐

What is calling jquery?

我只是不知道在我的主题中调用两个jquery脚本是什么。即使我turn off all plugins, 他们仍在被呼叫。我有not enqueued 在我的function.php 也不是在我的header.php 或footer.php.可能是WP core在呼叫他们吗?有什么办法可以找到答案吗?正在调用的两个jquery Javascript是:/wp包括/js/jquery/jquery。js公司/wp包括/js/jquery/jquery-migrate。最小js我想推迟他们,甚至把他们放在页脚