Javascript 如何使用Wordpress上的搜索创建AJAX自定义文章类型存档页面

Javascript 如何使用Wordpress上的搜索创建AJAX自定义文章类型存档页面,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,在我开始之前,我必须说我对PHP的了解很少,但在过去的几周里我一直在努力学习它 几天来,我一直在努力在Wordpress中的自定义帖子类型上实现一些功能 我已经创建了一个MU插件,并注册了一个类别“指南”和分类“主题”,它们在后端运行得非常好,我能够添加信息,但我无法在前端获得任何搜索功能或点击 这是我在HTML/CSS中创建的内容,我的目标是: 我在许多论坛上搜索过,并密切关注,但没有找到任何我需要的方式 这是我到目前为止所做的(抱歉,如果它很混乱,我花了几天的时间复制了许多不同的教程,所以

在我开始之前,我必须说我对PHP的了解很少,但在过去的几周里我一直在努力学习它

几天来,我一直在努力在Wordpress中的自定义帖子类型上实现一些功能

我已经创建了一个MU插件,并注册了一个类别“指南”和分类“主题”,它们在后端运行得非常好,我能够添加信息,但我无法在前端获得任何搜索功能或点击

这是我在HTML/CSS中创建的内容,我的目标是:

我在许多论坛上搜索过,并密切关注,但没有找到任何我需要的方式

这是我到目前为止所做的(抱歉,如果它很混乱,我花了几天的时间复制了许多不同的教程,所以可能有弗兰肯斯坦的代码)

/wp content/mu plugins/member zone.php

<?php

/**
* Plugin Name: BAKS9's plugin
* Description: Creates the Member-Zone
* Author: BAKS9
* Version: 1.0
*/
add_action( 'init', 'add_guidelines' );

function add_guidelines() {
    register_post_type( 'guidelines',
        array(
            'labels' => array(
                'name' => 'Member Zone',
                'singular_name' => 'Guideline',
                'add_new' => 'Add New',
                'add_new_item' => 'Add New Guideline',
                'edit' => 'Edit',
                'edit_item' => 'Edit Guideline',
                'new_item' => 'New Guideline',
                'view' => 'View',
                'view_item' => 'View Guidelines',
                'search_items' => 'Search Guidelines',
                'not_found' => 'No Guidelines found',
                'not_found_in_trash' => 'No Guidelines found in Trash',
                'parent' => 'Parent Guidelines',
                'exclude_from_search' => false,
                'pre_get_posts' => false,
                'hierarchical' => true
            ),
            'public' => true,
            'menu_position' => 15,
            'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
            'taxonomies' => array( 'subject' ),
            'menu_icon' => plugins_url( 'images/Members.png', __FILE__ ),
            'has_archive' => true
        )
    );
}

add_action( 'init', 'subject_taxonomy', 0 );

function subject_taxonomy() {
    register_taxonomy(
        'subject',
        'guidelines',
        array(
            'labels' => array(
                'name' => 'Subject',
                'add_new_item' => 'Add new subject',
                'new_item_name' => "New subject",
                'rewrite' => array( 'slug' => 'subject' )
            ),
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true,
            'show_admin_column' => true
        )
    );
}

/*
 * Template
 */

function include_template_function( $template_path ) {
    if ( get_post_type() == 'guidelines' ) {
        if ( is_archive() ) {
            if ( $theme_file = locate_template( array ( 'archive-guidelines.php' ) ) ) {
                $template_path = $theme_file;
            } else { $template_path = plugin_dir_path( __FILE__ ) . '/archive-guidelines.php';

            }
        }
    }
    return $template_path;
}

// Add the custom columns to the subject post type:
add_filter( 'manage_guidelines_posts_columns', 'set_custom_edit_guidelines_columns' );
function set_custom_edit_guidelines_columns($columns) {
    $columns['subject'] = __( 'Subject', 'http://www.MYWEBSITE.com' );

    return $columns;
}

// Add the data to the custom columns for the book post type:
add_action( 'manage_guidelines_posts_custom_column' , 'custom_guidelines_column', 10, 2 );
function custom_guidelines_column( $column, $post_id ) {
    switch ( $column ) {

        case 'subject' :
            $terms = get_the_term_list( $post_id , 'subject' , '' , ', ' , '' );
            if ( is_string( $terms ) )
                echo $terms;
            else
                _e( 'Unable to get subject(s)', 'http://www.MYWEBSITE.com' );
            break;

    }
}

function filter_subject_by_taxonomies( $post_type, $which ) {

    // Apply this only on a specific post type
    if ( 'guidelines' !== $post_type )
        return;

    // A list of taxonomy slugs to filter by
    $taxonomies = array( 'subject' );

    foreach ( $taxonomies as $taxonomy_slug ) {

        // Retrieve taxonomy data
        $taxonomy_obj = get_taxonomy( $taxonomy_slug );
        $taxonomy_name = $taxonomy_obj->labels->name;

        // Retrieve taxonomy terms
        $terms = get_terms( $taxonomy_slug );

        // Display filter HTML
        echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
        echo '<option value="">' . sprintf( esc_html__( 'Show All %s', 'text_domain' ), $taxonomy_name ) . '</option>';
        foreach ( $terms as $term ) {
            printf(
                '<option value="%1$s" %2$s>%3$s (%4$s)</option>',
                $term->slug,
                ( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
                $term->name,
                $term->count
            );
        }
        echo '</select>';
    }

}
add_action( 'restrict_manage_posts', 'filter_subject_by_taxonomies' , 10, 2);

?> 
<?php get_header(); ?>

<div id="main-content">
    <div class="container">
        <div id="content-area" class="clearfix">
            <div id="left-area">

<table style="overflow: hidden;" height="300px">
    <tbody>
<tr>
    <td class="Members_Left_Bar"><input role="search" id="search-container form" class="Members_Left_Search" type="search" placeholder="Search"><button type="submit" id="search-container form" class="Members_Left_Search_Icon"><i class="fa fa-search"></i></button></td>
    <th style="background-color: grey; color: white;"><a class="TitleSeperator" style="color: white;" href="#<?php echo the_ID ?>"><?php echo get_the_title(); ?></a></th>
    </tr>
    <tr>
        <td style="vertical-align: top;">
        <?php echo listsubjects() ?>
        </td>
        <td><br /><a name="<?php echo the_ID?>"></a><h2><?php echo get_the_title(); ?></h2><br /><?php echo the_content(); ?></td>
</tr>
    </tbody>
</table>

<?php
function listsubjects() {
    $taxonomy = 'subject';
    $args=array(
        'orderby' => 'menu_order',
              'show_count' => 0,
              'pad_counts' => 0,
              'hierarchical' => 1,
              'taxonomy' => $tax,
              'title_li' => '',
              'include'=> array()
        );

    $terms = get_terms('subject',$args );
    echo '<ul class="subject_list">';

    foreach ($terms as $term) {
        $term_link = get_term_link( $term, 'subject' );
        if( is_wp_error( $term_link ) )
            continue;

        echo '<li class="subject_list_item"><a href="' . $term_link . '">' . $term->name . '</a></li>';
    }
    echo '</ul>';
}

            if ( have_posts() ) :
                while ( have_posts() ) : the_post();
                    $post_format = et_pb_post_format(); ?>

                    <article style="display: none;" id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' ); ?>>

                <?php

                    $thumb = '';

                    $width = (int) apply_filters( 'et_pb_index_blog_image_width', 1080 );

                    $height = (int) apply_filters( 'et_pb_index_blog_image_height', 675 );
                    $classtext = 'et_pb_post_main_image';
                    $titletext = get_the_title();
                    $thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
                    $thumb = $thumbnail["thumb"];

                    et_divi_post_format_content();

                    if ( ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) {
                        if ( 'video' === $post_format && false !== ( $first_video = et_get_first_video() ) ) :
                            printf(
                                '<div class="et_main_video_container">
                                    %1$s
                                </div>',
                                $first_video
                            );
                        elseif ( ! in_array( $post_format, array( 'gallery' ) ) && 'on' === et_get_option( 'divi_thumbnails_index', 'on' ) && '' !== $thumb ) : ?>
                            <a href="<?php the_permalink(); ?>">
                                <?php print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height ); ?>
                            </a>
                    <?php
                        elseif ( 'gallery' === $post_format ) :
                            et_pb_gallery_images();
                        endif;
                    } ?>

                <?php if ( ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) : ?>
                    <?php if ( ! in_array( $post_format, array( 'link', 'audio' ) ) ) : ?>
                        <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php relevanssi_the_title(); ?></a></h2>
                    <?php endif; ?>

                    <?php
                        et_divi_post_meta();

                        if ( 'on' !== et_get_option( 'divi_blog_style', 'false' ) || ( is_search() && ( 'on' === get_post_meta( get_the_ID(), '_et_pb_use_builder', true ) ) ) ) {
                            truncate_post( 270 );
                        } else {
                            the_content();
                        }
                    ?>
                <?php endif; ?>

                    </article> <!-- .et_pb_post -->
            <?php
                    endwhile;

                    if ( function_exists( 'wp_pagenavi' ) )
                        wp_pagenavi();
                    else
                        get_template_part( 'includes/navigation', 'index' );
                else :
                    get_template_part( 'includes/no-results', 'index' );
                endif;
            ?>
            </div> <!-- #left-area -->
        </div> <!-- #content-area -->
    </div> <!-- .container -->
</div> <!-- #main-content -->

<?php get_footer(); ?>
这是一个开始:

  • 您的归档模板应该是
    archive-.php
    ,以遵循WordPress的模板层次结构
  • 您的帖子类型应该是单数,因此您应该注册
    guideline
  • 这将使您的归档
    归档指南.php
  • 无论何时运行大多数WP
    函数,例如
    标题()
    内容()
    ,或
    永久链接()
    ,默认行为都是
    回显
    ,因此无论何时执行
    回显
    ,它都是多余的:它已经回显了。您需要运行
    the_ID()
    echo-get_ID()
  • 这是一个开始:

  • 您的归档模板应该是
    archive-.php
    ,以遵循WordPress的模板层次结构
  • 您的帖子类型应该是单数,因此您应该注册
    guideline
  • 这将使您的归档
    归档指南.php
  • 无论何时运行大多数WP
    函数,例如
    标题()
    内容()
    ,或
    永久链接()
    ,默认行为都是
    回显
    ,因此无论何时执行
    回显
    ,它都是多余的:它已经回显了。您需要运行
    the_ID()
    echo-get_ID()

  • 别担心,WordPress有史以来最古老的编码标准。你应该去看看——这是一个“将WordPress拖到2017年”的好社区。我正在浏览你的代码,试图提供更多帮助。WP中的AJAX一开始有点混乱,但一旦你掌握了窍门,它就相当不错了。这是一个很好的教程——希望对你有所帮助!抓住你了!我会把我的头绕在AJAX上,这样可能会有所帮助,谢谢!别担心,WordPress有史以来最古老的编码标准。你应该去看看——这是一个“将WordPress拖到2017年”的好社区。我正在浏览你的代码,试图提供更多帮助。WP中的AJAX一开始有点混乱,但一旦你掌握了窍门,它就相当不错了。这是一个很好的教程——希望对你有所帮助!抓住你了!我会把我的头绕在AJAX上,这样可能会有所帮助,谢谢!