使用PHP代码在Wordpress中显示图片 [英] Show Picture in Wordpress with PHP Code

查看:146
本文介绍了使用PHP代码在Wordpress中显示图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用wordpress进入页面内容我使用PHP代码向数据库发送查询并将结果打印到页面,现在从数据库中我有很多列,其中一列是URL照片。
PHP代码如何在页面上显示图片?
这是我的以下代码并且它不干净:D我也使用INSERT PHP插件为wordpress在页面内容中插入PHP代码:

I'm using wordpress and into the content of pages i use a PHP code to send query to database and print result of that to the page, and now from in database i have many columns that one of them is a URL of a picture. in PHP code how can i show the picture on a page ? this is my following code and it's not clean :D and also i use INSERT PHP plugin for wordpress to insert the PHP code in content of a page:

[insert_php]
global $wpdb;
$wpdb->show_errors();
$pr = get_the_title();
$results = $wpdb->get_results("SELECT * from print_drug where Product_Name = '".$pr."'");
foreach ($results as $obj) {
  echo "<em><strong>Product Name:</strong></em> "; print_r($obj->Product_Name); echo "<br>";
  echo "<em><strong>Active Substance:</strong></em> "; print_r($obj->Active_Substance); echo "<br>";
  echo "<em><strong>Substance Sorting Identification Number:</strong></em> "; print_r($obj->Substance_Sorting_Identification_Number); echo "<br>";
  echo "<em><strong>Production Form of Use:</strong></em> "; print_r($obj->Production_Form_of_Use); echo "<br>";
  echo "<em><strong>Chemical Formation:</strong></em> "; print_r($obj->Chemical_Formation); echo "<br>";
  echo "<em><strong>Dosages of Use:</strong></em> "; print_r($obj->Dosages_of_Use); echo "<br>";
  echo "<em><strong>Approval Agency:</strong></em> "; print_r($obj->Approval_Agency); echo "<br>";
  echo "<em><strong>Year of Approval:</strong></em> "; print_r($obj->Year_of_Approval); echo "<br>";
  echo "<em><strong>Approval Assigned Company:</strong></em> "; print_r($obj->Approval_Assigned_Company); echo "<br>";
  echo "<em><strong>Approval Country:</strong></em> "; print_r($obj->Approval_Country); echo "<br>";
  echo "<em><strong>Prohibited Cases:</strong></em> "; print_r($obj->Prohibited_Cases); echo "<br>";
}
[/insert_php]

然后我有办法清理这段代码?
我是PHP新手:)

and then i there any way to clean this code ? i'm new in PHP :)

推荐答案

我更喜欢以下代码结构。顺便说一下,在你的代码中,你没有指出图像URL的列是什么,因此,我将在下面的代码中假设它是:Image_URL

I really prefer the following structure of code. By the way, in your code you didn't indicate what is the column for the image's URL, so, I'll assume in my following piece of code that it is: Image_URL

请注意使用方法$ wpdb->准备清理查询参数。并注意图像标记中的方法esc_attr。

Please, pay attention to the use of the method $wpdb->prepare for sanitizing the query parameters. And pay attention to the method esc_attr in the image tag.

[insert_php]
global $wpdb;
$pr = get_the_title();
$results = $wpdb->get_results($wpdb->prepare("SELECT * from print_drug where Product_Name = %s", $pr));
foreach ($results as $obj) {
    echo "<em><strong>Product Name:</strong></em> {$obj->Product_Name}<br>
        <em><strong>Active Substance:</strong></em> {$obj->Active_Substance}<br>
        <em><strong>Substance Sorting Identification Number:</strong></em> {$obj->Substance_Sorting_Identification_Number}<br>
        <em><strong>Production Form of Use:</strong></em> {$obj->Production_Form_of_Use}<br>
        <em><strong>Chemical Formation:</strong></em> {$obj->Chemical_Formation}<br>
        <em><strong>Dosages of Use:</strong></em> {$obj->Dosages_of_Use}<br>
        <em><strong>Approval Agency:</strong></em> {$obj->Approval_Agency}<br>
        <em><strong>Year of Approval:</strong></em> {$obj->Year_of_Approval}<br>
        <em><strong>Approval Assigned Company:</strong></em> {$obj->Approval_Assigned_Company}<br>
        <em><strong>Approval Country:</strong></em> {$obj->Approval_Country}<br>
        <em><strong>Prohibited Cases:</strong></em> {$obj->Prohibited_Cases}<br>
        <image src='".esc_attr($obj->Image_URL)."'>";
}
[/insert_php]

这篇关于使用PHP代码在Wordpress中显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆