您可以创建一个短代码函数用于您的内容,例如。[data-display]
您可以将其放置在自定义插件中,即子主题的函数中。php或/wp内容/mu插件/文件夹:
add_shortcode(\'data-display\', \'custom_api_data_display\');
function custom_api_data_display($atts) {
// for whatever you API call function is
// you may want check $_REQUEST or shortcode $atts to modify
$args = array(\'some_key\' => \'some_value\', \'some_key2\' => \'some_value2\');
$data = custom_api_data_request($args);
if (count($data) > 0) {
$html = \'<div class="result-container">\';
foreach ($data as $result) {
$html .= \'<div class="single-result">\';
// output whatever data result keys you like here
// this is just a simple example with image, title and description
$html .= \'<img class="result-image" src="\'.$result[\'image_url\'].\'">\';
$html .= \'<div class="result-title">\'.$result[\'title\'].\'</div>\';
$html .= \'<div class="result-description">\'.$result[\'description\'].\'</div>\';
$html .= \'</div>\';
}
$html .= \'</div>\';
} else {
$html = \'<p>No results found.</p>\';
}
return $html;
}
注意到
$atts
是在内容中传递的短代码属性,例如。
[data-display category="cat1"]
将给予
$atts
作为的数组数据
array(\'category\' => \'cat1\');
当然,您需要添加更多的层来启用搜索过滤功能,这些参数可以通过$_REQUEST