文件准备在Timber中的哪里查询?

时间:2019-05-03 17:08:08

标签: php twig timber

在搜索表单中,搜索参数由GET发送

Cannot assign "1": "Post.group" must be a "Trip" instance.

如何准备sql查询或文件在哪里准备查询?

Twig中的搜索表单:

http://site_url/?s=searching_text

1 个答案:

答案 0 :(得分:1)

该文件在逻辑上名为search.php,看起来像这样:

$templates = array( 'search.twig', 'archive.twig', 'index.twig' );
$context          = Timber::get_context();
$context['title'] = get_search_query();
$text_search = get_search_query();


$args = array(
    'post_type'        => array('post', 'product'),
    'posts_per_page'   => 8,
    's'                => $text_search,
    'orderby'          => 'date',
    'order'            => 'DESC',
    'post_status'      => 'publish',
    'paged'            => $paged
);

$context['pagination'] = Timber::get_pagination();
$context['posts'] = new Timber\PostQuery($args);
$context['count'] = new WP_Query($args);

Timber::render('templates/search.twig', $context);

以防您在搜索结果中使用分页。 这里的结果既来自“帖子”,也来自自定义的帖子类型,称为“产品”,但是您可以随意自定义该查询。