1. (re) Enabling the past

    May 26, 2008 by jerome

    sangbleu.png

    A few days ago, I had to modify the SangBleu WordPress theme for Maxime. Maxime finally wanted to give access to the recent archives pages. The SangBleu theme has been built on top of Naked Wordpress Theme; Naked is a drastically simplified theme with just the strict minimum DOM architecture. As such it is very easy to develop your own CSS - which is not the case with the standard Kubricks theme: too many intricated elements in the DOM structure, producing a super huge and complex stylesheet.

    You can download the Naked theme from http://bealers.com/wordpress-naked/; it is definitely a must-have if you’re going to develop some small project and need flexibility as well as simplicity.

    The problem I faced was a bit silly: the posts_nav_link was not behaving correctly - I changed at some point the MySql bits form the origianl theme: the Loop was not a standard one, but a simple MySql request on the database.

    $querystr = "	SELECT wposts.*
        FROM $wpdb->posts AS wposts
        WHERE wposts.post_status = 'publish'
        AND wposts.post_type='post'
        ORDER BY wposts.post_date DESC LIMIT 0,20
     ";
    
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    
    if ($pageposts): ?>
    <ol id="posts">
    				setup_postdata($post);
    [...]
    			endforeach;
    [...]
    endif;</ol>

    This MySql bit does not query any paged items. To manage to get the paging back, I had to re-modify the way the theme is requesting the posts from the database. In order to have the pagination for past posts enabled, I simply did a copy/paste from the Kubricks theme. So, I replaced the above by the following:


    if (have_posts()) :
    while (have_posts()) : the_post();
    [...]
    endwhile;
    endif;

    In Wordpress you can control the pagination on a page with multiple posts (like index.php) using the template tag: posts_nav_link. This tag takes 3 parameters, the first one being the string inserted at the beginning, the last two parameters being quite obvious…
    posts_nav_link(’ · ‘, ‘previous page’, ‘next page’)
    In order for it to work, you need to have a Loop on the page which is going to query the posts using a paged information.

    On a single post page, you can also use the next_post_link() and previous_post_link() to display links to the individual next/previous post.

    * http://codex.wordpress.org/Template_Tags/posts_nav_link


  2. Mime Type of Attachments

    April 1, 2008 by jerome

    Today, during one of the soft redesign of Assembling I found myself in need to know whether an attachment was an image or not; the wordpress documentation did not give me any clue about how to do this - so I started to create myself the function I needed: get_the_attachment_type

    It sits in my theme functions.php - and might be of any use to someone else:
    <?php
    function get_the_attachment_type($id = 0) {
    $id = (int) $id;
    $_post = & get_post($id);
    $mime = $_post->post_mime_type;
    return $mime;
    }
    ?>

    I use the function in the attachment.php file in the following manner:


    <?php switch (get_the_attachment_type($post->ID)) {
    case "image/jpeg":
    case "image/gif":
    case "image/png":
    break;
    default:?>
    <p><b>Download</b>:
    <?php } ?>
    <?php echo $attachment_link; ?> <?php the_content(); ?>

    This will then output a bold Download prompt in front of the file’s name in case, the attachment file is not an image (pdf, zip, etc.); if the attachment is an image, then nothing will be displayed.