我如何让我的wordpress插件显示我想要的位置?

问题描述 投票:0回答:2

我制作了一个PHP代码,通过从JSON文件中获取信息来跟踪软件包,并将其作为Wordpress插件。但是每次搜索时,搜索结果都会显示在页面顶部。我可以将其显示在需要的位置吗?

“

<?php

    $url = "a url where i get my json fille";
    $code = $_POST[code];
    $data = file_get_contents("$url$code");
    $json = json_decode($data);

    echo "<table class=\"table\">";
    foreach ($json as $key => $row) {
        echo "<tr>";
        foreach ($row as $key0 => $row0) {
            echo "<th>" . $key0 . " : " . $row0 . "</th>";
        }
        echo "</tr>";
    }
    echo "</table>";
?>
php html wordpress
2个回答
0
投票

最简单的方法是使用简码。

  1. 首先,将代码包装在函数中
// NB: you can name your function anything
function trackingplugin_print_table(){
    // Not part of your code, but add it just in case
    // This simply checks if user submitted anything
    if( ! $_POST['code'] ) {
       return;
    }
    $url = "a url where i get my json fille";
    $code = $_POST[code];
    $data = file_get_contents("$url$code");
    $json = json_decode($data);

    echo "<table class=\"table\">";
    foreach ($json as $key => $row) {
        echo "<tr>";
        foreach ($row as $key0 => $row0) {
            echo "<th>" . $key0 . " : " . $row0 . "</th>";
        }
        echo "</tr>";
    }
    echo "</table>";
}
  1. 然后将简码添加到WordPress More Info Here
// NB: you can also name your shortcode whatever you want
add_shortcode( 'trackingplugin', 'trackingplugin_print_table' );
  1. 然后在任何需要的地方使用简码[trackingplugin]。在这种情况下,它将在表格之后。

0
投票

尝试进行Ajax调用并将结果加载到响应div。

在此处查看一些想法How to call ajax in wordpress

您可以将console.log(msg);替换为jQuery('#response_div').html(msg);

© www.soinside.com 2019 - 2024. All rights reserved.