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!

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

报告相同问题?

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数