Scripting

Ramblings and links to interesting snippets


Gallery of all attached images in WordPress

For a site I’ve been working one I wanted to automatically include a gallery of all the images attached to a post, and I wanted it to display exactly like a standard WordPress gallery would look.  The HTML is modeled after the gallery code from Twenty Thirteen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
$args = array(
     'numberposts' => -1,
     'orderby' => 'menu_order',
     'order'=> 'ASC',
     'post_mime_type' => 'image',
     'post_parent' => $post->ID,
     'post_status' => null,
     'post_type' => 'attachment'
);
 
$images = get_children( $args );
 
if($images){ ?>
     <h2>Photos</h2>
 
     <div id='gallery-1' class='gallery galleryid-10 gallery-columns-4 gallery-size-thumbnail'>
          <?php foreach($images as $image){
               $image2 = wp_get_attachment_image_src( $image->ID, 'thumbnail'); ?>
               <dl class='gallery-item'><dt class='gallery-icon landscape'>
               <a href='<?php echo $image->guid; ?>'><img width="150" height="150" src="<?php echo $image2[0]; ?>" /></a>
               </dt></dl>
          <?php } ?>
          <br style='clear: both;' />
     </div>
<?php } ?>

Permalink » No comments

Exclude SlideDeck posts in WordPress

Tags: , , , ,

On a WordPress site I’ve been working on I’m using SlideDeck as a feature article slider on the homepage.  The only problem was I have additional articles populating underneath it and the articles from the SlideDeck were showing up down there as well.

We didn’t want that behavior so I researched how to filter out SlideDeck posts from the post loop.  At this time, it’s apparently easy to filter for content that has a custom meta-tag, but you can’t easily filter for content that doesn’t have a custom meta-tag applied to it.  It’s because when a post doesn’t have a custom met-tag attached to it that custom meta-tag doesn’t come up as “null” or “0”  it simply doesn’t come up at all.  So there’s no way to use query_posts to filter out SlideDeck posts, you have to make custom database query and use $wpdb->get_results.

This post from SlideDeck’s customer support website did most of the heavy lifting for me in putting together the exact query I needed to filter out only SlideDeck posts.  The only problem was that this gave me no insight as to how I could paginate the posts.  Thankfully google came to rescue once again and I found this post and this post which helped me piece together the script.  I posted my findings on SlideDeck’s customer service website, but I’m posted it here as well so I can easily find it again if I need it.

So for the code, inside loop.php before have_posts() I put…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(is_home()) {
    //query_posts(array( 'meta_key' => 'slidedeck_post_featured') );
    global $wpdb;
    $ppp = intval(get_query_var('posts_per_page'));
 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $offset = ($paged-1) * $ppp;
    $sql = "SELECT p.*
        FROM {$wpdb->posts} AS p
        LEFT JOIN {$wpdb->postmeta} AS pm
        ON pm.post_id = p.ID AND pm.meta_key = %s
        WHERE p.post_status = %s AND p.post_type = %s AND pm.meta_value IS NULL
	ORDER BY p.post_date desc
        LIMIT $offset, $ppp";
    $posts = $wpdb->get_results($wpdb->prepare( $sql, '_slidedeck_post_featured', 'publish', 'post' ));
}

if(is_home())  will make it so it will only filter out SlideDeck posts on the homepage, and apparently the have_posts() works exactly as it normally does even when I change the loop using $wpdb->get_results, which means I don’t have to do much to the rest of the page in order for this to work.  WordPress, you rock <3

This will paginate the posts according to the number of items I have set in the WordPress settings, but for some reason when I did this on the last page it printed out empty posts to fill out that page.

For instance, say I only have 7 posts left on the last page, but I have it set to show a maximum of 10 posts per page – it would print out 3 empty posts on that page.

To combat this I put the following code inside have_posts() so it wouldn’t print anything if the post ID was null.

1
2
3
if(get_the_ID() > 0) {
    //html for posts
}

Pretty straight forward and everything has been working great so far 🙂

 

References

http://support.slidedeck.com/slidedeck/topics/wp_query_a_list_of_posts_not_featured_in_the_slide_deck?from_gsfn=true

http://wordpress.org/support/topic/custom-loop-pagination

http://www.scriptygoddess.com/archives/2011/02/23/wordpress-pagination-woes-solved-i-hope/

Permalink » No comments

Custom Facebook RSS Widget

For a project we wanted to bring in outside links and display them in a box on the homepage.  Instead of building out a whole custom system to handle the link, I suggested posing them through Facebook.

So in order to be able to style the feed however we wanted I put together a simple Curl code in PHP to pull in the Facebook Page RSS feed (every page has a feed it’ll be formated like this => http://www.facebook.com/feeds/page.php?id=245091913103&format=rss20).

This will check if there’s a link inside the body of the status update and use that, but then default to the link to the status message in Facebook if that doesn’t exist.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "http://www.facebook.com/feeds/page.php?id=245091913103&format=rss20");
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
        $xmlTwitter = curl_exec($curl);
        curl_close($curl);
 
        $xmlObjTwitter = simplexml_load_string($xmlTwitter);
        $tempCounter = 0;
 
        foreach ($xmlObjTwitter->channel->item as $item) {                    
            if ( $tempCounter < 9 ){
                preg_match('/(http|https|ftp)(.*?) (id|target)/i', $item->description, $matches);
                $matches[0] = str_replace('" id','',$matches[0]);
                echo '<li><a href="'.($matches[0] ? $matches[0] : $item->link).'" target="_blank">'.$item->title.'</a>
                <div>'.date('F jS, Y',strtotime($item->pubDate)).'</div></li>';
            }
 
            $tempCounter += 1;
        }
 
        echo '</ul>';
?>

Permalink » No comments

Rails for Zombies

I recently completed the Ruby on Rails tutorial “Rails for Zombies.” I really like it’s “edutainment” zombie filled format, and on top of that it’s a very well written and easy to follow tutorial.

The tutorial consists of five “chapters.” Each chapter is broken up into an approximately 10 minute video and then a lab portion where you get to do exercises related to the video.  It’s really great to try out the code right after hearing about it, and I like that it doesn’t get drearily technical at the get go with needing to get Rails up and running.  A lot of RoR tutorials start with installing and runing your own local server, which a least for me is the least pleasant part.

A nice take away is the free PDF download it offers, which has all the slides from the video and is a nice reference back to everything you learned from the tutorial.  You also get a coupon for some paid rails tutorials after you finish.  They have “Rails for Zombies 2” in the works so I’m thinking of saving it for that :3

At their Code School website they also offer a “Rails best practices” class which sounds a little advanced for diving into right after Rails for Zombies, but if it’s anywhere as well put together as Rails for Zombies I’d like to come back to it later.

http://railsforzombies.org/

Permalink » No comments

Images Cut Off in the WordPress Image Editor

So, I’ve just spent the last half hour tracking down this annoying bug in WordPress 😛

I’m really glad I use an outdated theme (or at least one without custom header support) so I could track this puppy down.

I have been working on building a theme that is a child theme of good ‘ol 2010 (2011 wasn’t out when I started building :()

I uploaded a portrait image and noticed it got cut off in the image editor like so:


The editor’s will really need to be able to adjust that thumbnail image, so I went digging around and found that a theme called “Modfolio” had this exact same problem.  So, I figure, it’s gotta be something with the theme.  Especially since I didn’t get a whole lot of results…if it was a core issue and everyone was seeing this then I would assume there would be a lot more complaints.

I switched around from my child theme, to 2010, and even 2011, but the same error kept popping up :/  Then I figured I’d check it out in my blog here.  I’m currently using a slightly modified version of Uchilla theme – which hasn’t been updated in ages.  And lo-and-behold the image is coming up fine and dandy in the editor.  I could be wrong on this, but I believe the only file in a theme that can mess with the admin side of things is the functions.php file.  So I just kept hacking away at it until I found the bit of code that was causing me grief.

It’s lines 117 & 188 in the functions file for 2010:

define( 'HEADER_IMAGE_WIDTH', apply_filters( 'twentyten_header_image_width', 940 ) );
define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'twentyten_header_image_height', 198 ) );

That was somehow adding a constraint to the editor preview and for me simply commenting out those two lines did the trick 🙂  I did have to re-upload all the affected images though…

I don’t know what effect that might have on the custom headers…I’m not using them myself so it wasn’t an issue for me.

Permalink » No comments