コード例 #1
0
ファイル: attached.php プロジェクト: surreal8/wptheme
 public static function attached_shortcode($atts, $content, $tag)
 {
     $args = array('orderby' => '', 'order' => '', 'category' => '', 'count' => '', 'offset' => '', 'trim' => '', 'id' => '', 'field' => '', 'columns' => '', 'pad' => '', 'between' => '');
     extract(shortcode_atts($args, $atts, true));
     /*---------------------------------------------
      *
      * Get attachments
      *
      */
     $attachment_ids = array();
     if (!empty($id)) {
         $parent_id = 0;
         // Any post
     } elseif (!empty($field)) {
         // Specific attachment ID from field
         $id = do_shortcode('[field ' . $field . ']');
         if (empty($id)) {
             return;
         }
         $parent_id = 0;
         // Any post
     } else {
         $parent_id = do_shortcode('[field id]');
         // Attachments of current post
         if (empty($parent_id)) {
             return;
         }
     }
     if (isset($atts[0]) && $atts[0] == 'gallery') {
         // Get attachment IDs from gallery field
         $attachment_ids = CCS_Gallery_Field::get_image_ids($parent_id);
         // Support for orderby title
         if ($orderby == 'title') {
             usort($attachment_ids, array($this, 'sort_gallery_by_title'));
         }
     } else {
         $attach_args = array('post_parent' => $parent_id, 'post_type' => 'attachment', 'post_status' => 'any', 'posts_per_page' => '-1');
         // default orderby
         $attach_args['orderby'] = empty($orderby) ? 'date' : $orderby;
         // default for titles
         if ($orderby == 'title') {
             $order = empty($order) ? 'ASC' : $order;
         }
         if (!empty($order)) {
             $attach_args['order'] = $order;
         }
         if (!empty($category)) {
             $attach_args['category'] = $category;
         }
         if (!empty($count)) {
             $attach_args['posts_per_page'] = $count;
         }
         if (!empty($offset)) {
             $attach_args['offset'] = $offset;
         }
         if (!empty($id)) {
             $attach_args['post__in'] = CCS_Loop::explode_list($id);
             $attach_args['orderby'] = empty($orderby) ? 'post__in' : $orderby;
             unset($attach_args['post_parent']);
         }
         // Get attachments for current post
         $posts = get_posts($attach_args);
         $index = 0;
         foreach ($posts as $post) {
             $attachment_ids[$index] = $post->ID;
             // Keep it in order
             $index++;
         }
     }
     // If no images in gallery field
     if (count($attachment_ids) == 0) {
         return null;
     }
     /*---------------------------------------------
      *
      * Compile template
      *
      */
     $out = array();
     // if nested, save previous state
     if ($tag[0] == '-') {
         $prev_state = self::$state;
     }
     self::$state['is_attachment_loop'] = true;
     foreach ($attachment_ids as $index => $attachment_id) {
         self::$state['current_attachment_id'] = $attachment_id;
         $out[] = do_ccs_shortcode($content);
     }
     self::$state['is_attachment_loop'] = false;
     // if nested, restore previous state
     if ($tag[0] == '-') {
         self::$state = $prev_state;
     }
     /*---------------------------------------------
      *
      * Post-process
      *
      * TODO: Combine this with loop and others
      *
      */
     if (!empty($columns)) {
         $out = CCS_Loop::render_columns($out, $columns, $pad, $between);
     } else {
         $out = implode('', $out);
         if ($trim == 'true') {
             $out = trim($out, " \t\n\r\v,");
         }
     }
     return $out;
 }
コード例 #2
0
ファイル: acf.php プロジェクト: supahseppe/path-of-gaming
 public static function loop_through_acf_gallery_field($atts, $content)
 {
     extract(shortcode_atts(array('field' => '', 'count' => '', 'start' => '', 'subfield' => '', 'sub' => '', 'columns' => '', 'pad' => '', 'between' => '', 'option' => ''), $atts));
     if (empty($field) && isset($atts[0])) {
         $field = $atts[0];
     }
     // If in repeater or flexible content, get subfield by default
     if (self::$state['is_repeater_or_flex_loop']) {
         $sub = 'true';
     }
     // Backward compatibility
     if (!empty($subfield)) {
         $field = $subfield;
         $sub = 'true';
     }
     // Support getting field from option page
     $option = $option == 'true' ? 'option' : false;
     if (empty($sub)) {
         $images = get_field($field, $option);
     } else {
         $images = get_sub_field($field);
         // Gets option from the_row()
     }
     $outputs = array();
     if ($images) {
         $index_now = 0;
         if ($start == '') {
             $start = '1';
         }
         self::$state['is_gallery_loop'] = true;
         self::$state['gallery_index'] = 0;
         foreach ($images as $image) {
             self::$state['current_image'] = $image;
             $index_now++;
             self::$state['gallery_index'] = $index_now;
             if ($index_now >= $start) {
                 if ($count != '' && $index_now >= $start + $count) {
                     break;
                 }
                 $outputs[] = str_replace('{COUNT}', $index_now, do_ccs_shortcode($content));
             }
         }
         self::$state['is_gallery_loop'] = false;
     }
     if (is_array($outputs)) {
         if (!empty($columns)) {
             $output = CCS_Loop::render_columns($outputs, $columns, $pad, $between);
         } else {
             $output = implode('', $outputs);
         }
     } else {
         $output = $outputs;
     }
     self::$state['current_image'] = '';
     return $output;
 }