dougaoxian8922 2017-06-01 20:46
浏览 103
已采纳

从自定义搜索表单中获取数据并在页面中显示结果(wordpress)

I am having an issue to display results from a search form in a page.

I have in my header.php a custom search form :

<form action="<?php echo site_url('/pagesearch/')?>" method="get" id="adminbarsearch">
    <input type="search" id="place"  name="mydata" />
    <input type="submit" class="adminbar-button" value="Rechercher"/>*/?>
</form>

When a user submits the form, I retrieve the data and modify it, through this code in my functions.php :

if ((isset($_GET['mydata'])) && (!empty($_GET['mydata']))){
    $q = sanitize_text_field($_GET['mydata']);      
    $search_posts_id = array();

    $search_args = array(
            's' => $q,
            'fields' => array('ids', 'post_type'),
            'post_type' => 'my_custom_posttype'
    );
    $search_query = new WP_Query($search_args);

    if ( $search_query->have_posts() ) :
            $search_posts_id = array_unique(wp_list_pluck( $search_query->posts, 'post_title', 'id' ));
            $search_posts_id = implode( ',', $search_posts_id );
    else :
            $search_posts_id = implode( ',', $search_posts_id );
    endif;
    wp_reset_postdata(); 

    echo $search_posts_id;

The problem is that I would like to display the result which is $search_posts_id in the "pagesearch" page of my website.

When I use the code above, it displays in the page but before the header and everything, and not in the content, between the header and the footer.

I don't know how to put this data correctly in the content.

Thank you very much for your help !

  • 写回答

1条回答 默认 最新

  • doujia4041 2017-06-02 02:40
    关注

    That's because you're echoing from your functions.php file -- this runs before the theme's header and content render, so it's going to just output before everything else.

    You'll need to take a different approach, like potentially placing this code inside page.php (or whatever template you want it in) possibly before the the_content() call, or maybe even creating a custom shortcode to display this content wherever you want.

    If you can provide some more info on where specifically you'd like these results to show up, we might be able to assist further. Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)