【问题标题】:How to add &post_type=product on woocommerce url search results如何在 woocommerce url 搜索结果中添加 &post_type=product
【发布时间】:2019-03-05 13:18:24
【问题描述】:

我有一个主题表单 yootheme pro,我在 searchform.php 上有以下代码:

<?php
/**
 * The template for displaying a search form.
 */

$result = get_view('search', [

    'position' => get_current_sidebar(),
    'attrs' => [

        'id' => 'search-'.rand(100, 999),
        'action' => esc_url(home_url('/')),
        'method' => 'get',
        'role' => 'search',
        'class' => '',

    ],
    'fields' => [

        ['tag' => 'input', 'name' => 's', 'placeholder' => esc_attr_x('Search &hellip;', 'placeholder'), 'value' => get_search_query()], 'name' => get_post_type()]

    ]

]);

if ($echo) {
    echo $result;
} else {
    return $result;
}

我跪下来修改此代码并将 &post_type=product 添加到结果 url

谢谢

【问题讨论】:

  • 您只想要搜索中的产品吗?还是页面和帖子以及所有东西?
  • 是的,只有产品!搜索后的网址是:example.com/?s=test(此代码如下)我跪为:example.com/?s=test&post_type=product(此代码修改)谢谢
  • 如果你只想要产品,你只需要URL中的post_type GET参数

标签: woocommerce woocommerce-theming


【解决方案1】:

试试这个。不确定这是否适用于您的模板,但在我的模板中可以。所以只是为了澄清:WordPress中有许多不同的帖子类型。例如页面、产品使用...

我们所知道的是告诉函数它应该采用哪种搜索值。从逻辑上讲,它应该可以工作!

<?php
/**
 * The template for displaying a search form.
 */

$result = get_view('search', [

    'position' => get_current_sidebar(),
    'attrs' => [

        'id' => 'search-'.rand(100, 999),
        'action' => esc_url(home_url('/')),
        'method' => 'get',
        'role' => 'search',
        'class' => '',

    ],
    'fields' => [

        ['tag' => 'input', 'name' => 's', 'placeholder' => esc_attr_x('Search &hellip;', 'placeholder'), 'value' => 'product'], 'name' => 'post_type']

    ]

]);

if ($echo) {
    echo $result;
} else {
    return $result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2019-11-17
    • 2017-10-30
    • 2015-07-17
    • 1970-01-01
    • 2014-06-22
    相关资源
    最近更新 更多