简体   繁体   English

使用ajax或jquery实时搜索到wordpress自定义post_type标题列表

[英]Live search to wordpress custom post_type title list using ajax or jquery

I am trying to adding a AJAX live search to filter posts title in my wordpress theme to sort the output post list. 我正在尝试添加AJAX实时搜索来过滤我的wordpress主题中的帖子标题,以对输出帖子列表进行排序。 I have added a image the current output. 我在当前输出中添加了图像。 please

I added a input field for filter the list view 我添加了一个用于过滤列表视图的输入字段

  <input placeholder="Search here........." type="text" />

Bellow is my custom page type to output the list of custom post here my custom post-type is circular 贝娄是我的自定义页面类型,用于在此处输出自定义帖子列表,我的自定义帖子类型是circular

<?php /*Template Name: Circular search*/
 function TrimTitle($s) {
    $maxLength = 80;
    if (strlen($s) > $maxLength) {
        echo substr($s, 0, $maxLength - 5) . ' ...';
    } else {
        echo $s;
    }
}
?>

<?php get_header(); ?>
<input placeholder="Search here........." type="text" />
<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$circular_query= new WP_Query(array(
    'post_type'=>'circular',
    'posts_per_page' => 7,
    'order' => 'DESC',
    'paged' => $paged,
));

if($circular_query->have_posts()) :
    while($circular_query->have_posts())  : $circular_query->the_post();
     $file = get_post_meta(get_the_ID(), 'circular_attachment', true); 
     //get the url
     $url = $file['url'];
    ?>
    <ul>
        <li>
            <a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute() ?>">
             <i class="fa fa-file-pdf-o circular-list-icon" aria-hidden="true"></i>  <?php the_time( 'd-m-Y' ) ?>  <?php TrimTitle(get_the_title()); ?> 
            </a>
            <a class="download-btn read-more btn btn-primary" href="<?php echo $file['url']; ?>"> Download</a>
        </li>
        <hr class="sep-circular"/>
    </ul>           
    <?php endwhile;
    $total_pages = $circular_query->max_num_pages;
    if ($total_pages > 1){
        $current_page = max(1, get_query_var('paged'));
        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }
    ?>    
<?php else :?>
<h3><?php _e('No Circular found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
<div class="sep-circular clearfix"></div>
<?php get_footer(); ?>

在此处输入图片说明

In your query you aren't telling WordPress which field to order by. 在您的查询中,您不会告诉WordPress订购哪个字段。 Try this instead: 尝试以下方法:

$circular_query= new WP_Query(array(
    'post_type'=>'circular',
    'posts_per_page' => 7,
    'order' => 'ASC',
    'orderby' => 'post_title',
    'paged' => $paged,
));

Also, I set the result order to ASC rather than DESC as I think that you want the posts in ASC order, don't you? 另外,我将结果顺序设置为ASC而不是DESC,因为我认为您希望按ASC顺序排列帖子,不是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM