本文介绍了如何在wordpress中获取现有媒体的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在wordpress库中添加了一些图片。现在我需要通过名称检索其中一个并获取它的URL。请注意,我没有在任何帖子中附上他们。 感谢您的关注。 解决方案一个简单的方法 - 在WordPress数据库抽象API中使用直接的SQL SELECT 语句: $ wpdb-> get_var( $ wpdb-> prepare( SELECT ID FROM $ wpdb-> posts WHERE post_title =%s AND post_type ='%s',$ title,$ type)); 你可以将它合并到一个函数中(你可以放在你的functions.php文件中) ($ title,$ type ='post'){ global $ wpdb; function get_post_by_title $ b $ post_id = $ wpdb-> get_var( $ wpdb-> prepare( SELECT ID FROM $ wpdb-> posts WHERE post_title =%s AND post_type ='%s',$ title,$ type)); if(!empty($ post_id)){ return(get_post($ post_id)); $ b $ p $ b 在模板中你可以调用你的函数所以: $ attachment = get_post_by_title('文件名','attachment'); echo $ attachment-> guid; //这是原始URL echo get_attachment_link($ attachment-> ID); //这是漂亮的URL i added some image to my wordpress library. now i need to retrieve one of them by name and get it's URL. note that i didn't attach them in any post.thanks for your attention. 解决方案 A straightforward approach - using a direct SQL SELECT statement with the WordPress database abstraction API:$wpdb->get_var( $wpdb->prepare(" SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = '%s' ", $title, $type));You can incorporate this into a function (you can place in your functions.php file):function get_post_by_title($title, $type = 'post') { global $wpdb; $post_id = $wpdb->get_var( $wpdb->prepare(" SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = '%s' ", $title, $type) ); if(!empty($post_id)) { return(get_post($post_id)); }}And the in your templates you can call you functions like so:$attachment = get_post_by_title('Filename', 'attachment');echo $attachment->guid; // this is the "raw" URLecho get_attachment_link($attachment->ID); // this is the "pretty" URL 这篇关于如何在wordpress中获取现有媒体的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-22 21:16