是否可以通过WordPress短代码从HTML输出中提取图像src属性?

时间:2016-10-14 21:24:41

标签: php html wordpress

我正在尝试从Instagram Wordpress插件中提取由以下短代码生成的HTML中包含的img src属性:

do_shortcode('[instagram-feed type=user id="'.get_company_field( 'instagram_id' ).'" num=1 cols=1 width=100 widthunit=% height=300 heightunit=px carousel=true carouselarrows=false carouselpag=false imagepadding=0 imagepaddingunit=px showfollow=false showheader=false showlikes=false showcaption=false hoverdisplay="date, location, likes" includewords="'.get_company_field( 'instagram_keyword' ).'"]');

我想将图像src属性的URL放入一个变量($ src)中,我可以使用PHP作为元素的CSS background-image URL输出。例如:

echo '<header style="background-image: url('.$src.');" class="job_listing-entry-header listing-cover has-image">'; 

我发现很难提取img src属性。不确定是否可以从短代码的输出中完成。

我尝试了以下操作,但它不起作用:

function sl_instagram_image() {
    $html = do_shortcode('[instagram-feed type=user id="'.get_company_field( 'instagram_id' ).'" num=1 cols=1 width=100 widthunit=% height=300 heightunit=px carousel=true carouselarrows=false carouselpag=false imagepadding=0 imagepaddingunit=px showfollow=false showheader=false showlikes=false showcaption=false hoverdisplay="date, location, likes" includewords="'.get_company_field( 'instagram_keyword' ).'"]');
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $xpath = new DOMXPath($doc);
    $src = $xpath->evaluate("string(//img/@src)");
    echo '<header style="background-image: url('.$src.');" class="job_listing-entry-header listing-cover has-image">';
} 

add_action( 'listify_content_job_listing_header_before', 'sl_instagram_image'); 

0 个答案:

没有答案