gpt4 book ai didi

javascript - 如何使用 WordPress 上的搜索创建 AJAX 自定义帖子类型存档页面

转载 作者:行者123 更新时间:2023-11-28 04:04:22 25 4
gpt4 key购买 nike

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

我这几天一直在努力尝试在 WordPress 中的自定义帖子类型上运行一些功能...

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

这是我在 HTML/CSS 中创建的内容,我正在追求:

The basics are here, but the functionality is not... The search doesn't work at all and clicking on the subject headers take me to "single.php"

我在许多论坛中进行了搜索并密切关注它们,但没有设法让任何东西按照我需要的方式工作。

这是我到目前为止所得到的(抱歉,如果它很困惑,我花了几天时间复制了许多不同的教程,所以可能弗兰肯斯坦已经修改了代码)。

/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);

?>

/wp-content/themes/child/archives-guidelines.php:

<?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(); ?>

我从 Divi 复制并粘贴了“Archive.php”文件,并将 Divi 元素隐藏在我试图组合在一起的页面上。

我尝试使用分类法“主题”创建搜索,但即使如此,它也不会从正确的分类法中提取任何结果,因此在我使其正常工作之前,我需要 AJAX 才能工作...

我知道我需要一个 .js 文件,但即便如此我也没有任何运气。

我知道这对我来说可能比应有的更加令人困惑,我只是真的希望有人能给我一些指导或告诉我重新开始......

我还需要确保我正确地发布了主题和类别标题以及内容,因为我似乎也无法确定这一点......

最佳答案

这是一个开始:

  1. 您的存档模板应该是 archive-<post_type>.php遵循 WordPress 的模板层次结构。
  2. 您的帖子类型应该是单数,因此您应该注册guideline
  3. 这将使您的存档 archive-guideline.php
  4. 每当您运行 WP 的大部分the_函数,例如the_title() , the_content() ,或the_permalink() ,默认行为是 echo ,所以无论何时 echo the_ID() ,这是多余的:它已经在回响了。您需要运行 the_ID()echo get_the_ID()

关于javascript - 如何使用 WordPress 上的搜索创建 AJAX 自定义帖子类型存档页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46829722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com