[英]How to get attachment url if match with tag (slug/name) (wordpress)
如果圖像文件名的文件名中包含該標記(單詞),我想在標記下顯示所有附件(圖像)。
我的意思是,如果我有標簽http://example.com/tag/john
我想顯示名稱中帶有john的所有圖像。
像/wp-content/uploads/2018/08/my_name-john.jpg, /wp-content/uploads/2017/06/john_meet-us.jpg,
我是php和wordpress的新手,我不知道是否有可能從瀏覽器中打開的url中獲取標簽,然后為該標簽查找圖像。 我知道它的幫助形式,我還必須提供我嘗試使用的代碼,然后人們可以幫助我們。 但是正如我說的是新的,我用谷歌搜索,嘗試使用此代碼,但有很多限制,也不知道如何修改以供使用。
<?php
function get_attachment_url_by_title( $title ) {
$attachment = get_page_by_title($title, OBJECT, 'attachment');
//print_r($attachment);
if ( $attachment ){
$attachment_url = $attachment->guid;
}else{
return 'image-not-found';
}
return $attachment_url;
}
echo get_attachment_url_by_title('title of image');
?>
上面的代碼顯示圖像,如果我們手動提供圖像的標題,其名稱必須與文件名相同,則上面的代碼起作用,否則不起作用。 所以希望你能回答我的問題。 提前致謝。 ( 對不起,我的英語不好)
您可以通過以下Sql Query按標簽獲取附件的網址:
將以下代碼粘貼到當前活動主題的functions.php文件中 。
function get_url_from_tag_name($tag = 'old'){
global $wpdb;
$query = 'SELECT guid FROM `'.$wpdb->prefix.'posts` WHERE guid like "%'.$tag.'%" AND post_type="attachment"';
$results = $wpdb->get_results( $query, OBJECT );
return $results;
}
希望對您有幫助。 謝謝 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.