###前提・実現したいこと
Wordpressでコンテンツスライダーに表示する画像を指定カテゴリから新しいものから指定数、
画像のURL一覧を取得したいのですが良い方法はないでしょうか?
###該当のソースコード
function get_photo_links_plus($category = '', $yearmonth = ''){ if($category=='' || $yearmonth=='' ){ return; } global $post; $count=0; $nowyear = substr($yearmonth, 0, 4); $nowmonth = substr($yearmonth, 4, 2); $prev_yearmonth = date_i18n("Ym", mktime(0, 0, 0, $nowmonth - 1, 1, $nowyear)); $args = array( 'category' => $category, 'posts_per_page' => -1 ); $myposts = get_posts( $args ); foreach( $myposts as $post ){ setup_postdata($post); if($yearmonth!=date_i18n('Ym',get_post_time())){ continue; } $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => $post->ID, 'post_mime_type' => 'image' ); $attachments = get_posts($args); if($attachments){ foreach ($attachments as $attachment){ $image = wp_get_attachment_image_src($attachment->ID, 'none'); print '<img src="' . $image[0] . '" alt="" />'."\n"; $count=$count+1; } } } if($count<1){ // get images from old entrys. foreach( $myposts as $post ){ setup_postdata($post); if($prev_yearmonth!=date_i18n('Ym',get_post_time())){ continue; } $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => $post->ID, 'post_mime_type' => 'image' ); $attachments = get_posts($args); if($attachments){ foreach ($attachments as $attachment){ $image = wp_get_attachment_image_src($attachment->ID, 'none'); print '<img src="' . $image[0] . '" alt="" />'."\n"; $count=$count+1; } } } } wp_reset_postdata(); }
あなたの回答
tips
プレビュー