php - 如何使用 the_permalink();选择数据库查询中的wordpress?

标签 php mysql wordpress

我将我的 WordPress 文章链接存储在自定义表中(不是 guid)。

数据库:Wordpress

:wp_custom_link

:link_id,链接

现在我想从我的自定义表格中显示当前 WordPress 文章 URL 的 ID。为此,我尝试使用以下代码:


    global $wpdb;
    $result = $wpdb->get_results( "SELECT * FROM wp_custom_link WHERE link= '.the_permalink().'");

    foreach($result as $row)
       {   
          echo $row->link_id;
       }


它没有显示任何输出,但是当我尝试使用 WHERE link_id = 1 并尝试显示链接时,它起作用了。请帮助我!

谢谢你...

最佳答案

您的连接不正确。你应该像这样连接它:

$result = $wpdb->get_results( "SELECT * FROM wp_custom_link WHERE link= '".get_permalink()."'");

请注意,我正在使用 get_permalink(),使用 the_permalink() 回显结果,在您的情况下会导致另一个错误。

更好的是,使用带有占位符的 $wpdb->prepare 方法:

$result = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT * FROM wp_custom_link WHERE link= '%s'", 
        get_permalink()
    )
);

关于php - 如何使用 the_permalink();选择数据库查询中的wordpress?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28362691/

相关文章:

mysql - 使用相同的表和行偏移进行更新

php - MySql Query/PHP 获取具有相同客户名称的所有列值

php - 如何在 PHP 中显示数组中的项数

wordpress - 谷歌索引 https 而不是 http

php - 显示记录时如何检查Field是否为空

php - 将地址分解为数组

php - Symfony 4 和 Amazon S3 - AWS 凭证不起作用

php - PHP 中的正则表达式 : how to match any A and IMG HTML tag including content

mysql - Laravel Sail 重建默认数据库

php - Wordpress 中的内存泄漏