【问题标题】:how to connect to database from wordpress shortcode如何从 wordpress 简码连接到数据库
【发布时间】:2020-02-16 14:17:39
【问题描述】:

如何让这个数据库回显这些 a 标签。单独的代码字,但不适用于短代码

function get_repair_prices(){
$servername = "localhost";
$username = "localseo_prices";
$password = "******";
$dbname = "localseo_getitfixed";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM repair_prices";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
      echo "<a href=''>".$row['btn_name']."</a>";
    }
} else {
    echo "0 results";
}
$conn->close();

}
add_shortcode('price_shortcode', 'get_prices');

它作为一个单独的 php 文件工作,但不会将数据输出为简码

【问题讨论】:

  • add_shortcode('price_shortcode', 'get_prices'); 与方法名称不匹配 function get_repair_prices

标签: php html mysql wordpress


【解决方案1】:

试试这个:

function get_repair_prices($atts){
    $servername = "localhost";
    $username = "localseo_prices";
    $password = "******";
    $dbname = "localseo_getitfixed";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT * FROM repair_prices";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
          echo "<a href=''>".$row['btn_name']."</a>";
        }
    } else {
        echo "0 results";
    }
    $conn->close();

}
add_shortcode('price_shortcode', 'get_repair_prices');

【讨论】:

  • 我原来的add_shortcode与函数名不匹配,谢谢!
  • 嗨 Akhtarujjaman,请教我,function get_repair_prices($atts){ 中的 $atts 是什么?
  • 它是函数属性,您可以通过短代码传递。
猜你喜欢
  • 2014-01-15
  • 2016-03-02
  • 2014-11-21
  • 1970-01-01
  • 2020-12-16
  • 2019-08-20
  • 2017-12-10
  • 1970-01-01
  • 2020-10-18
相关资源
最近更新 更多