Dynamically Get the Slug of the Current Page

February 15th, 2011

I needed to get the post slug for a wordpress client because I conditionally needed to show posts and a custom post type that was tagged a certain way. By naming the page and the tag I needed the same, their slug was the same. Therefore being able to “relate posts” dynamically.

All you need is to add this to your functions.php

  1. function the_slug() {
  2. $post_data = get_post($post->ID, ARRAY_A);
  3. $slug = $post_data['post_name'];
  4. return $slug;
  5. }

Then anywhere in your template you can get posts dynamically by the tag slug.

  1. <?php $relatedposts = get_posts(array('post_type' => 'post', 'numberposts' => 5, 'tag' => the_slug()));
  2.  foreach($relatedposts as $post) : setup_postdata($post); ?>
  3.   <h2><a href="<?php the_permalink(); ?>" class="ctaurls"><?php the_title(); ?></a></h2>
  4. <?php endforeach; ?>

Add Custom Taxonomy to Archive Page

February 14th, 2011

So I know that this is going to come out in 3.1 but for anyone who needs this currently this might be helpful. I was building a site that had a custom taxonomy of say “portfolio types” and I made an archive for that page (taxonomy-portfolio-types.php). You’d think that the body class would automatically be but alas it was just “archive”. Not very useful if you want to style some part of the header or footer slightly differently than your other archive pages without writing conditionals into them. Well here’s how to add “portfolio-types” or your own taxonomy into your body class.

Note: Add this code into your functions.php

  1. add_filter( 'body_class', 'add_tax_bodyclass' );
  2. function add_tax_bodyclass( $classes ){
  3.    if( is_archive() && is_tax() == 'portfolio-types')
  4.    {
  5.         $classes[] =  'portfolio-types';
  6.    }
  7.    return $classes;
  8. }

New Hosting!!

December 14th, 2010

I’ve got new hosting, yay!

Sorry it’s been awhile

December 11th, 2010

I realized the other day it’s been a long time since I’ve updated my blog. I’ve learned lots of new things about wordpress and will be updating on them during the holiday. In the meantime enjoy some lovely photos I took while in Chicago 2 months ago.




Worpdress: Building a JQuery Slideshow without a plugin

August 19th, 2010

Recently, I had to add a slideshow to a wordpress site. It was a pretty simple request: it had to utilize the automatic wordpress gallery functionality in each page/post, needed to pull the image, image title, and image description using a fade out/fade in, and the client did not want it to be in flash.

I searched endlessly through plugins only to be disappointed. Some don’t work well or AT ALL with Wordpress 3.0; some won’t even install. Some don’t use the gallery function, therefore can’t have different slideshows on different pages. Some of the best ones were flash, so that wouldn’t work. And others just only called the image, while the functionality of one just didn’t make sense. Now I don’t want to call anyone out and call their hard work, bad work, so I won’t link any of the offending plugins here.

I began thinking; it cannot be that hard to build a slideshow in wordpress using the functionality that already exists. Besides if I didn’t use a plugin it wouldn’t break/would be easier to fix after a wordpress update. So using the jQuery cycle plugin I utilized wordpress’s built in calls and made my own 100% dynamic, married to gallery functionality slideshow.

Now onto the good part, how it’s done:

Go to the jQuery cycle page and download the necessary files/get links for necessary js. You’ll need to put the jQuery source and the jquery cycle scripts into your header and then call it in the document ready function as shown below:

  1. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  2. <script type="text/javascript" src="path/to/cycle/jquery.cycle.all.min.js"></script>
  3. <script type="text/javascript">
  4. $(document).ready(function() {
  5.     $('.slideshow').cycle({
  6.   fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc…
  7.  });
  8. });
  9. </script>

So here’s the hard part, figuring out exactly how to call the image, title and description. I’m sure this would have been a lot easier if i actually knew PHP (don’t worry I’m learning), but each time I figure something out, PHP makes a little more sense.

In your template file after if (have_posts)… add the following code:

  1. <?php
  2. $args = array(
  3.  'post_type' => 'attachment',
  4.  'numberposts' => -1,
  5.  'post_status' => null,
  6.  'title' => $attachment->post_title,
  7.  'post_parent' => $post->ID,
  8.  'description' => $attachment->post_content
  9. );
  10. $attachments = get_posts($args);
  11.  if ($attachments) {
  12.  foreach ($attachments as $attachment) {
  13.  echo
  14.               '<div>',
  15.               '' .the_attachment_link($attachment->ID, $size = 'full', $attr = '', false).  '',
  16.               '<h2>' .$attachment->post_title . '</h2>',
  17.               '<p>' . $attachment->post_content . '</p>',
  18.               '</div> ' ;
  19.  }        
  20. }
  21. ?>

Now let’s break down the code:

post_type => attachment : tells wordpress database to call the attachment instead of post content
numberposts => -1 : call all attachments
post_status => null : can be changed to draft, published, etc
title => $attachment->post_title : defines the title of the attachment
post_parent => $post->ID : defines the current post
description => $attachment->post_content : calls the images description

  1.  echo
  2.               '<div>',
  3.               '' .the_attachment_link($attachment->ID, $size = 'full', $attr = '', false).  '',
  4.               '<h2>' .$attachment->post_title . '</h2>',
  5.               '<p>' . $attachment->post_content . '</p>',
  6.               '</div> ' ;

This part says find all of the above and then display as written: find the image, make the size full (can be changed the thumb etc), find the title and wrap it in an h2, find the description and wrap that in the p tags, and finally wrap the entire thing in a div. Now I’m not sure why, but the 2 single quotes before and after the_attachment_link were necessary to make it not break. If you wanted to wrap just the image in say a div class then you could do this:

  1.  echo
  2.               '<div>',
  3.               '<div class="image-class">' .the_attachment_link($attachment->ID, $size = 'full', $attr = '', false).  '</div>',
  4.               '<h2>' .$attachment->post_title . '</h2>',
  5.               '<p>' . $attachment->post_content . '</p>',
  6.               '</div> ' ;

So that’s my sans plugin wordpress slideshow. Questions?

Page 1 of 3123