0

Background:

I'm using a wordpress theme called skt full width. I am modifying their front page slideshow, so that it displays six random images instead of six pre-selected images. I have a custom field in media to select which images could be selected in this lottery to be on the front page. Then there is a query to select those images.

The code they use to display the images need certain image properties to be set in order to work. They are, image url, image title, image description, and image href.

Question:

How do I get that information needed from the media/attachment once it is in the query? Do I need to use their insertion code or is their a better way to accomplish what I want?

Code for displaying the image:

echo '{image : \''.$imgUrl.'\', title : \'<div class="slide-title"><span>'.( ($imgHref!='' && $imgTitle!='') ? '<a href="'.$imgHref.'">' : '').$imgTitle.( ($imgHref!='' && $imgTitle!='') ? '</a>' : '').'</span></div><div class="slide-description"><span>'.$imgDesc.'</span></div>'.( ($imgHref != '') ? '<div class="slide-description"><span><a href="'.$imgHref.'">Read More &rsaquo;</a></span></div>' : '').'\', thumb : \''.$imgUrl.'\', url : \'\'},'."\n";

My code:

$args = array(
   'post_type'  => 'attachment',
   'meta_key'   => 'on_front_page',
   'meta_value' => '1',
   'orderby'    => 'rand',
   'posts_per_page' => 6 ,
   'max_num_pages'  => 1,
);

$slides = new WP_Query($args);
$total_images = (int) $slides->found_posts;
while( $slides->have_posts() ) {
    echo '{image : \''.$imgUrl.'\', title : \'<div class="slide-title"><span>'.( ($imgHref!='' && $imgTitle!='') ? '<a href="'.$imgHref.'">' : '').$imgTitle.( ($imgHref!='' && $imgTitle!='') ? '</a>' : '').'</span></div><div class="slide-description"><span>'.$imgDesc.'</span></div>'.( ($imgHref != '') ? '<div class="slide-description"><span><a href="'.$imgHref.'">Read More &rsaquo;</a></span></div>' : '').'\', thumb : \''.$imgUrl.'\', url : \'\'},'."\n";   
}
1

The below is starting after you do your query:

$slide_list = array();

if ( $slides->have_posts() ) {
    while ( $slides->have_posts() ) {

Grab the next slide using next_post to grab it without stomping globals

        $slide = $slides->next_post();
        $slide_id = $slide->ID;

Your image title is your post title:

        $slide_title = $slide->post_title;

Your href is your permalink

        $slide_href = get_permalink( $slide );

Your description is your post content or excerpt

        $slide_content = $slide->post_content;
        $slide_excerpt = $slide->post_excerpt;

If you want the width and height, you can grab that as follows:

        $image_data = wp_get_attachment_image_src( $slide_id, 'thumbnail');
        $image_url = $image_data[0];
        $width = $image_data[1];
        $height = $image_data[2];

        # do something here ...
        $slide_list[] = array(
            'id' => $slide_id,
            'title' => $slide_title,
            'href' => $slide_href,
            'content' => $slide_content,
            'image_w' => $width,
            'image_h' => $height
        );
    }
}

Hopefully that helps!

The main thing is to just cycle through them.

If you want to see all of the available data, use the following in the look:

echo "<pre>" . print_r($slide, true) . "</pre>";

That should show you all of the returned fields for each slide.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.