如何在没有短代码的wordpress中实现FAQ

时间:2017-02-28 08:36:07

标签: php wordpress

您好我有一个任务是将FAQ模块添加到worpress项目中。我是wordpress的新手。 我的客户说明如下:“从正确的位置获取常见问题解答,而不是使用短代码”。 。这条指令意味着什么,我该怎么做?

我尝试使用FAQ模块,但根据此代码生成短代码。如何在不使用短代码的情况下使用它。我的代码是

 <?php
    add_action('init', function() {

        $labels = array(
            'name' => _x('FAQ', 'post type general name'),
            'singular_name' => _x('Question', 'post type singular name'),
            'add_new' => _x('Add New Question', 'Question'),
            'add_new_item' => __('Add New Question'),
            'edit_item' => __('Edit Question'),
            'new_item' => __('New Question'),
            'all_items' => __('All FAQ Questions'),
            'view_item' => __('View Question'),
            'search_items' => __('Search FAQ'),
            'not_found' => __('No FAQ found'),
            'not_found_in_trash' => __('No FAQ found in Trash'),
            'parent_item_colon' => '',
            'menu_name' => 'FAQ'
        );

        $args = array(
            'labels' => $labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'query_var' => true,
            'rewrite' => true,
            'capability_type' => 'post',
            'has_archive' => true,
            'hierarchical' => false,
            'menu_position' => null,
            'supports' => array('title', 'editor', 'page-attributes')
        );
        register_post_type('FAQ', $args);
    });

    add_action( 'wp_enqueue_scripts', 'wptuts_enqueue' );

    function wptuts_enqueue() {
        wp_register_style('wptuts-jquery-ui-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/south-street/jquery-ui.css');
        wp_enqueue_style('wptuts-jquery-ui-style');

        wp_register_script('wptuts-custom-js', get_template_directory_uri() . '/faq/faq.js', array('jquery','jquery-ui-accordion'), '', true);
        wp_enqueue_script('wptuts-custom-js');
    }




    add_shortcode('faq', function() {

        $posts = get_posts(array(  //Get the FAQ Custom Post Type
            'numberposts' => 10,
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'post_type' => 'faq',
        ));

        $faq  = '<style type="text/css">
        .ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default{
            border-bottom : 1px solid #327e04;
            background: #F7F7F7;
            font-weight: bold;
            color: #ffffff;
            border-bottom: 4px solid red;
        }

        .ui-corner-all, .ui-corner-bottom, .ui-corner-right, .ui-corner-br {
            -moz-border-radius-bottomright: 0px;
            -webkit-border-bottom-right-radius: 0px;
            -khtml-border-bottom-right-radius: 0px;
            border-bottom-right-radius: 0px;
            border-radius: 0px;
            border-bottom: 4px solid red;
        }

        address, blockquote, dl, fieldset, figure, h1, h2, h3, h4, h5, h6, hgroup, hr, ol, p, pre, table, ul{
            margin-bottom: 0rem;
            /*border-bottom: 4px solid red;*/
        }

        .ui-corner-all, .ui-corner-top, .ui-corner-right, .ui-corner-tr {
            -moz-border-radius-topright: 0px;
            -webkit-border-top-right-radius: 0px;
            -khtml-border-top-right-radius: 0px;
            border-top-right-radius: 0px;
            /*border-bottom: 4px solid #F7F7F7;*/
        }

        .ui-corner-all, .ui-corner-top, .ui-corner-left, .ui-corner-tl {
            -moz-border-radius-topleft: 0px;
            -webkit-border-top-left-radius: 0px; 
            -khtml-border-top-left-radius: 0px;
            border-top-left-radius: 0px; 
        }

        .ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default {
            border: 0px solid #327e04;
            border-top: 2px solid #C0C0C0;
            border-bottom: 2px solid #C0C0C0;
        }

        .ui-widget-content {
            border: 0px solid #F7F7F7; 
            background: #F7F7F7; 
            color: #312e25;
            border-bottom: 2px solid #C0C0C0;
        }

        .ui-accordion .ui-accordion-header {
            cursor: pointer;
            position: relative;
            margin-top: -2px; 
            zoom: 1;
        }

        .ui-accordion .ui-accordion-content {
            padding: 1em 2.2em;
            border-top: 0;
            margin-top: -3px;
            position: relative;
            top: 1px;
            margin-bottom: -3px;
        }

        .ui-accordion .ui-accordion-header .ui-icon {
            position: absolute;
            left: initial;
            top: 50%;
            margin-top: -8px;
            width: 56px;
            height: 56px;
        }

        .ui-accordion .ui-accordion-header .ui-icon {
            position: absolute;
            right: .5em !important;
        }

        .ui-state-default .ui-icon {
            background-image: url(http://freevector.co/wp-content/uploads/2014/08/54785-down-arrow.png);
            right: .5em !important;
            background-position: 0px -20px;
            background-size: 100% 100%;
        }

        .ui-state-active .ui-icon {
            background-image: url(http://www.seeicons.com/images/iconstore/512/seeicons__57808be438471.png);
            right: .5em !important;
            background-position: 0px 0px;
            background-size: 100% 100%;
        }

        .ui-icon-triangle-1-e {
            background-position: 0px -20px;
        }

        .ui-icon-triangle-1-s {
            background-position: -0px -20px;
        }


    </style> <div id="wptuts-accordion" >'; //Open the container
        foreach ( $posts as $post ) { // Generate the markup for each Question
            $faq .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
                $post->post_title,
                wpautop($post->post_content)
            );
        }
        $faq .= '</div>'; //Close the container



        return $faq; //Return the markup.
    });

    ?>

1 个答案:

答案 0 :(得分:-1)

您只能将此代码放在主题的function.php

 add_action('init', function() {

    $labels = array(
        'name' => _x('FAQ', 'post type general name'),
        'singular_name' => _x('Question', 'post type singular name'),
        'add_new' => _x('Add New Question', 'Question'),
        'add_new_item' => __('Add New Question'),
        'edit_item' => __('Edit Question'),
        'new_item' => __('New Question'),
        'all_items' => __('All FAQ Questions'),
        'view_item' => __('View Question'),
        'search_items' => __('Search FAQ'),
        'not_found' => __('No FAQ found'),
        'not_found_in_trash' => __('No FAQ found in Trash'),
        'parent_item_colon' => '',
        'menu_name' => 'FAQ'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title', 'editor', 'page-attributes')
    );
    register_post_type('faq', $args);
});

它会为您创建自定义帖子类型,然后您可以在此处添加与帖子相同的常见问题解答,并使用WP_Query()函数从前端获取所有数据。