Mobile WordPress Theming: Day 4

Mobile WordPress Theming: Day 4

Tutorial Details
  • Technology: jQTouch + WordPress
  • Difficulty: Beginner
  • Completion Time: 30 Minutes
This entry is part 4 of 4 in the series Mobile WordPress Theming

Welcome to the final installment of our Mobile WordPress Theming series! In this tutorial, we will be loading posts and pages dynamically into our mobile web application. By the end, we will have a usable mobile WordPress theme! Let’s jump right in!

Changing The Theme Thumbnail

First off, as we package this theme we will need to change the theme thumbnail eventually to replace the thumbnail that came with the original naked theme. For this I just fit a screenshot of the theme into the 300 X 225 canvas. I also finally gave the theme an official name: MyTouch. It might not be the most creative, but, hey, it works. The file name is screenshot.png and is located in the root directory of any theme.

Thumbnail

 

Changing Permalink Stucture

This is the one sloppy part of this series. In order to get jQTouch’s AJAX capabilities to load individual blog posts we need to modify the permalink structure of the blog. This is because out of the box, jQTouch can only load static files. However, WordPress is anything but static-it’s dynamic to the extreme. So we’re going to trick jQTouch, by creating "virtual static" pages. This is actually quite simple with WordPress. Just go to Settings > Permalinks and choose Custom Structure. Then use the following:

/%category%/%postname%.html

The important part here is the .html that we added. Everything before that is up to you.

Permalinks

Changing Page Permalinks

Unfortunately, pages work a little differently with permalinks. In order to get a .html extension for pages, we need to install a plugin called .html on Pages. This will append .html after all pages, and make jQTouch work again for pages.

Plugin

Load Posts

Remove <section> tags

First, we need to remove the section tags that wrap the blog posts. This allows jQTouch to be able to understand the structure of the page, which will make loading the blog posts a lot easier and quicker. Now, the blog section of index.php should look like this:

<div id="blog">
    <div class="toolbar">
        <h1>Blog</h1>
        <a class="back" href="#">Home</a>
    </div>
 	<div class="info">
        This is a short description of blog. Here you describe the purpose.
    </div>
<?php
  if (have_posts()):  ?>
	<?php
	    while (have_posts()) : the_post(); ?>
		<ul class="rounded">
	      <li><a class="blog-title" href="<?php the_permalink() ?>"><?php the_title(); ?></a><br />
		<div class="month"><?php the_time('M') ?></div>
		<div class="date"><?php the_time('j') ?></div>
		<p class="post-author">By: <i><?php the_author(); ?></i></p>
		<p class="post-description"><?php the_content(__('<span class="readmore">Read More</span>')); ?></p></li>
		</ul>
	    <?php endwhile; ?>
<?php else: ?>
  <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php
  endif;
  ?>
  <?php if (will_paginate()): ?>
<nav>
    <ul id="pagination">
      <li class="previous"><?php posts_nav_link('','','« Previous Entries') ?></li>
      <li class="future"><?php posts_nav_link('','Next Entries »','') ?></li>
    </ul>
</nav>
  <?php endif; ?>
</div><!-- End blog-->

Single.php

Single.php is the file that is used to view an individual post. We need to modify this, so that it is only a div section that we can easily target and load using AJAX. First, we need to check that there are posts to display. Then, we will start a while loop to display the post’s content. We have already covered almost all of the WordPress template functions below, so they should all be pretty self-explanatory:

<?php
  /**
  *@desc A single blog post See page.php is for a page layout.
  */
  if (have_posts()) : while (have_posts()) : the_post();
  ?>
<div class="post" id="post-<?php the_ID(); ?>">
	<div class="toolbar">
        <h1>Blog Post</h1>
        <a class="back" href="#">Blog</a>
    </div>
	<ul class="rounded">
      <li>
		<div class="month-small"><?php the_time('M') ?></div>
		<div class="date-small"><?php the_time('j') ?></div>
		<a class="blog-title" href="<?php the_permalink() ?>"><?php the_title(); ?></a><br />
	<p class="post-author-text">By: <i><?php the_author(); ?></i></p>
	<p class="post-text"><?php the_content_rss(); ?></p></li>
	</ul>
	<?php
  comments_template();
  endwhile; else: ?>
		<p>Sorry, no posts matched your criteria.</p>
<?php
  endif;
?>
</div>

The only thing that really is new is comments_template(). This basically inserts the comment template, which we will talk about in a bit.

Styling

We will also need to add a little bit more styling to style.css to make this look good:

.month-small{
			position: absolute;
			width: 35px;
			text-align: center;
			background: #aa3939;
			color: #f5f2f2;
			line-height: 14px;
			padding-top: 2px;
			padding-bottom: 3px;
			border-top-left-radius: 4px;
			border-top-right-radius: 4px;
			font-size: 12px;
			margin-top: -8px;
			right: 15px;
	}
	.date-small{
		position: absolute;
		width: 35px;
		margin-top: 11px;
		font-size: 18px;
		text-align: center;
		background: #f5f2f2;
		border-bottom-left-radius: 4px;
		border-bottom-right-radius: 4px;
		right: 15px;
	}
	.post-author-text, .post-text{
		font-size: 13px;
		color: #fff;
		margin-bottom: 5px;
		text-align: justify;
		line-height: 18px;
	}
	.avatar-32{
		float: right;
		margin-right: 35px;
		margin-top: -4px;
	}
	h3{
		margin-left: 5px;
	}
Blog Post

Comments

I mentioned the comments template in the last step. Now we need to define that. To do so, open up comments.php.

Security and Title

We need to keep in mind that this is a template. And as such, we need to be flexible. Not all blogs will want comments enabled. Some will only want private comments. You get the idea. This first part is pretty standard to many templates, but we need to check if comments are open, what type of post this is, and then insert the comment area title. We do so with the following code:

<?php
  /**
  *@desc Included at the bottom of post.php and single.php, deals with all comment layout
  */
  if ( !empty($post->post_password) && $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) :
?>
<p><?php _e('Enter your password to view comments.'); ?></p>
<?php return; endif; ?>
<h2 id="comments"><?php comments_number(__('No Comments'), __('1 Comment'), __('% Comments')); ?>
<?php if ( comments_open() ) : ?>
<?php endif; ?>
</h2>

Display Comments

Now we need to display comments. First, we need to check if we have comments on the post. Then we will start a foreach loop and print out each comment:

<?php if ( $comments ) : ?>
	<?php foreach ($comments as $comment) : ?>
		<ul class="rounded" id="comment-<?php comment_ID() ?>">
	      <li>
			<?php echo get_avatar( $comment, 32 ); ?>
			<div class="month-small"><?php comment_time('M') ?></div>
			<div class="date-small"><?php comment_time('j') ?></div>
			<a class="blog-title" target="_blank" href="<?php comment_author_url(); ?>"><?php comment_author(); ?></a>
			<br />
		<p class="comment-text"><?php comment_text() ?><?php edit_comment_link(__("<br /><hr />Edit This"), ''); ?></p></li>
		</ul>
	<?php endforeach; ?>
<?php else : // If there are no comments yet ?>
	<p><?php _e('No comments yet.'); ?></p>
<?php endif; ?>
Blog Comments

Comment RSS Feed/Trackback URL

Another pretty standard theme feature is including an RSS feed for every post’s comments as well as a trackback URL. We need to specifically target a new window for jQTouch to work properly. This complicates the Comments RSS a bit, because there isn’t an out of the box WordPress theme function that we can use that we can specify a blank target. Luckily, WordPress’ convention is basically just post url/feed. Therefore, we can create our own:

<ul class="rounded">
      <li class="forward"><a href="<?php the_permalink() ?>/feed" target="_blank">Post Comments RSS</a></li>
<?php if ( pings_open() ) : ?>
	<li class="forward"><a href="<?php trackback_url() ?>" target="_blank">Trackback URL</a></li>
	<?php endif; ?>
</ul>
Blog Links

Leave A Comment

Now we’re going to add the capability to add a comment. First, you need to check if comments are open (There’s a function for that). Then add a title letting the user know they can. Then we need to check if anyone can leave a comment or if you need to be registered. Then we open the form. Then we create a form in jQTouch’s style. Then we close everything up. It should make more sense below:

<?php if ( comments_open() ) : ?>
<h2 id="postcomment"><?php _e('Leave a comment'); ?></h2>
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p><?php printf(__('You must be <a href="%s">logged in</a> to post a comment.'), get_option('siteurl')."/wp-login.php?redirect_to=".urlencode(get_permalink()));?></p>
<?php else : ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform" class="form">
<ul class="edit rounded">
<?php if ( $user_ID ) : ?>
<p><?php printf(__('Logged in as %s.'), '<a href="'.get_option('siteurl').'/wp-admin/profile.php">'.$user_identity.'</a>'); ?> <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="<?php _e('Log out of this account') ?>"><?php _e('Logout »'); ?></a></p>
<?php else : ?>
<li><input type="text" name="author" id="author" placeholder="Name" value="<?php echo $comment_author; ?>" size="22" tabindex="1" /></li>
<li><input type="text" name="email" id="email" placeholder="Email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" /></li>
<li><input type="text" name="url" id="url" placeholder="URL" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" /></li>
<?php endif; ?>
<!--<p><small><strong>XHTML:</strong> <?php printf(__('You can use these tags: %s'), allowed_tags()); ?></small></p>-->
<li><textarea name="comment" id="comment" placeholder="Comment Here" ></textarea></li>
</ul>
<h3>Your comment will appear after approved.</h3>
<input name="submit" type="submit" id="submit" tabindex="5" value="<?php echo attribute_escape(__('Submit Comment')); ?>" style="margin:0 10px;color:rgba(0,0,0,.9)" class="submit whiteButton" />
<input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />
<?php do_action('comment_form', $post->ID); ?>
</form>
<?php endif; // If registration required and not logged in ?>
<?php else : // Comments are closed ?>
<p><?php _e('Sorry, the comment form is closed at this time.'); ?></p>
<?php endif; ?>
Blog Comment Write

Pages

We also need to make sure that we cover pages in this theme. Within index.php, we need to list out all pages in a menu. Then we can link to the actual pages content like we did with blog posts. It is very simple to list pages in WordPress. We will add one parameter though. By default the template function will add a title list item. We will fix this below:

<div id="pages">
  <div class="toolbar">
      <h1>Site Pages</h1>
      <a class="back" href="#">Home</a>
  </div>
  <h2>Pages</h2>
  <ul class="rounded">
		<?php wp_list_pages('title_li='); ?>
  </ul>
</div><!-- End #pages -->
Pages

Page.php

Page.php is the page’s version of the post’s single.php. That being said, it will be a very similar file too.

<?php
  /**
  *@desc A single blog post See page.php is for a page layout.
  */
  if (have_posts()) : while (have_posts()) : the_post();
  ?>
<div class="post" id="post-<?php the_ID(); ?>">
	<div class="toolbar">
        <h1>Blog Post</h1>
        <a class="back" href="#">Pages</a>
    </div>
	<ul class="rounded">
      <li>
		<div class="month-small"><?php the_time('M') ?></div>
		<div class="date-small"><?php the_time('j') ?></div>
		<a class="blog-title" href="<?php the_permalink() ?>"><?php the_title(); ?></a><br />
	<p class="post-author-text">By: <i><?php the_author(); ?></i></p>
	<p class="post-text"><?php the_content_rss(); ?></p></li>
	</ul>
	<?php
  comments_template();
  endwhile; else: ?>
		<p>Sorry, no pages matched your criteria.</p>
<?php
  endif;
?>
</div>
Page

What Now?

wp-mobile

 

Now we have a usable WordPress theme that anyone can use to reach mobile audiences from their desktop WordPress blog. But what now? There’s still a lot of room for improvement on this theme (There might be a tutorial on adding search functionality, or there might not be, who knows ;] -leave a comment if you’d like to see one), and that is why I am releasing it as open source to the community to use and improve upon. You can download and access the project over at http://code.google.com/p/wp-mobile/. I encourage everyone to use it and make it their own. I’d love to see some people using this, so make sure to email me a link! Let me know your thoughts!

Series Navigation«Mobile WordPress Theming: Day 3

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://reciprocity.be/ Keith S.

    Great series Connor! One thing I did was to make my own list function for the Pages section, so that the list items had the “arrow” class. It’s a simple little function that goes in the theme’s functions.php file. You can see the function here:

    http://pastie.textmate.org/1095688

    Like I said, it’s a simple little thing. To make this work, you’d just replace your call to wp_list_pages() in the Pages section with pageList(). You can see the results here:

    http://www.flickr.com/photos/zarath0s/4897633067/

    • http://connorzwick.com Connor Zwick
      Author

      Keith, That’s great! Why don’t you submit those changes to the google code page listed above?

      • http://reciprocity.be/ Keith S.

        Done…wasn’t sure how to work on Google Project Hosting (never used it for more than downloading :) ), so I opened an issue with details about the new function and how to implement it.

  • Poul H. Hanen

    It felt good reading all this to the end, still knowing i learned something, without it being too confusing.
    Having just entered the world of toying around with wordpress in general. This seemed like a major step, but then again didn´t seem too bad.

    On the subject of continuing the tutorial including search functionality. It would defintely be awsome. But why not let some people toy with it at least. I know i will be, before thinking about reading on anyways.

    Good work (:

  • http://christophkock.com chris

    Thank you! hope get more tutorials like this soon!!!!!!!

  • Dieter

    Hi Connor! Really nice. But i have small question. “Keep content in one place, but have a separate theme for mobile and desktop users”! how does this work? how can i differentiate between the mobile and desktop theme?
    can i give the mobile theme less content? because the most of the time, the desktop sites have to much content for a mobile version.

    thank you very much!

    best regards

  • Anders

    How do you distinguish your themes, I mean default vs mobile version? So WordPress loads the ‘wp-mobile’ theme if you’re using a mobile browser.

    It also feels like you can’t separate this functionality into a theme of it own, since you probably has custom post types and other stuff in your default theme that you’ll want to use.

  • http://www.connorzwick.com Connor Zwick
    Author

    @Dieter & @Anders:

    That is actually the very topic I am writing my next tutorial on. How to detect a mobile browser in several different situations.

  • http://christophkock.com Chris

    Hi Connar,

    that would be really great! i hade the same question like Dieter and Anders.

    thank you very much and best regards,

    Chris

    • Anders

      I’m working on a solution that overrides the active theme with the filters ‘stylesheet’ and ‘template’ and set my theme_root to a separate directory if visitor is using a mobile browser.

      Feels kinda hacky. And can someone please tell me how I can get around the “.html”-extension on jqtouch? I reaaaaally don’t want to change my sites permalink-structure… :(

      • http://www.connorzwick.com Connor Zwick
        Author

        Anders, the quickest way would probably be manually writing the AJAX code yourself instead of relying on jQTouch.

    • http://www.connorzwick.com Connor Zwick
      Author

      Good to hear, you should see an in-depth tutorial next week!

  • http://www.iasava.com iasava

    Thank you for all articles about Mobile WordPress Theming. I will try to apply to my blog. ^_^

  • http://digitalart.splesh.net Steo

    these tutorials are just great. I can’t wait to work with it and try it on my blog. I’ll let you know about it :)

  • http://www.connorzwick.com Connor Zwick
    Author

    For those that did not see it, the mobile browser detection tutorial is online:

    http://mobile.tutsplus.com/tutorials/html5/mobile-browser-detection/

  • Paul

    Can you please post a .zip the end results files?

    • Lee

      It would only make sense if you coded them yourself, this is a very nice clean easy to use tutorial, you will learn nothing just copying the files here.

      • Paul

        Not true, I attempted coding it myself and ran into some stumbling blocks so the final results would be helpful. All of the best tutorials/books include the code. I’m currently reading the Big Nerd Ranch book and it’s been very helpful to compare my code side by side with their code to check for differences.

        I would still like to download the sample code to finish this tutorial series please.

  • http://connorzwick.com Connor Zwick
    Author

    Paul,

    If you read the last step, you’ll see the link that leads to where you can download it:

    http://code.google.com/p/wp-mobile/

    • Paul

      Sorry I overlooked that, guess I was looking for it at the top. Thanks a lot for reminding me where the link was!

    • Rodrigo

      Great article! Or rather articles! Just what I needed to dive into this! I got the file and was playing around with it and noticed two things…

      1. When I make a chance to the code and I refresh the page, nothing happens? Well the front page seems fine when I change the text, but the other pages stay the same after many tries.

      2. Without doing the .html and html for pages thing I get all the animation from the app just fine… or so it seems like it? Or am I missing something?

  • Nis

    Hi Connor

    I’m so glad that you have JQtouch theme! But have some questions:

    1) I have installed wordpress 3.03 and installed .html on pages and changed my permalink – then I choose the theme (mytouch) and expected it will work. But it don’t work, don’t show any nice graphic ?

    2) Are you considering to make it into a add-on instead? Could be really nice :)

    Thx again for your great work!

    Nis

  • http://abraham.mx Abraham Velazquez

    FYI,

    From the WP Docs. “Starting Permalinks with %category% is strongly not recommended for performance reasons.”

    http://wordpress.org/support/topic/using-category-to-start-permalinks

    http://codex.wordpress.org/Using_Permalinks#Structure_Tags

  • cydia

    older and recent posts links don’t work.

    every time i click on one of this links, homepage of the theme is loading, so only post number limit can be viewed.

    could you tell me how to fix this problem?

    thx

    cydia

  • http://themes.lonchbox.com/mytouch Pancho

    Hi Connor,

    Thx to take your time and make this tutorial :)
    Recently I donwload the source from Google code but the Theme doesn´t work :( it not have any style on it and can´t find why? Can you help me?

    Gracias,
    Pancho

  • http://www.eaglelight.com/ LED

    Thanks for this site – stunningly useful Tutorial.

  • http://www.beautifulicon.com OC Wedding Photographer

    Nice tip on the .html on Pages plugin … I was just wondering how I can make pages .html … Anyways, thanks for giving the code for FREE … :) Great job!!!

  • shae

    I am listing Taxonomies that will display Custom Post Types when clicked. The problem I have is the toolbar gets duplicated between every post. Does anyone know how to have it show only once? I’ve tried putting the toolbar above the loop, but then none of the posts show up. Any ideas?
    Here is my code:

    <div class="post" id="post-”>

    Movies
    Back

    <a class="blog-title" href="”>

    By:

  • shae

    Lets try that again.

    <div class="post" id="post-”>

    Movies
    Back

    <a class="blog-title" href="”>

    By:

  • shae

    Here is the code:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <div class=”post” id=”post-<?php the_ID(); ?>”>
    <div class=”toolbar”>
    <h1>Movies</h1>
    <a class=”back” href=”#”>Back</a> </div>
    <ul class=”rounded”>
    <li>
    <div class=”month-small”>
    <?php the_time(‘M’) ?>
    </div>
    <div class=”date-small”>
    <?php the_time(‘j’) ?>
    </div>
    <a class=”blog-title” href=”<?php the_permalink() ?>”>
    <?php the_title(); ?>
    </a><br />
    <p class=”post-author-text”>By: <i>
    <?php the_author(); ?>
    </i></p>
    <p class=”post-text”>
    <?php the_content_rss(); ?>
    </p>
    </li>
    </ul>
    <?php endwhile; else: ?>
    <?php endif; ?>
    </div>

  • Shae

    I have everything working properly so far. However, I have added an image that I want to show as a thumbnail in a post that when tapped I want to use the flip transition of JQtouch to show a larger version of the image.

    I cant get it to work properly. The flip transition starts, but then the image will not show up. Is there another template file that needs to go with it?

    This is the code:

    <ul class=”rounded”>
    <li><a class=”blog-title” href=”<?php the_permalink() ?>”><?php the_title(); ?></a><br />
    <div class=”month”><?php the_time(‘M’) ?></div>
    <div class=”date”><?php the_time(‘j’) ?></div>
    <p class=”post-author”>By: <i><?php the_author(); ?></i></p>
    <p><a href= “http://localhost/M3/wp-content/uploads/2011/04/test.jpg” class=”photo flip” > <img src= “http://localhost/M3/wp-content/uploads/2011/04/test.jpg” </a></p>
    <p class=”post-description”><?php the_content(__(‘<span class=”readmore”>Read More</span>’)); ?></p></li>
    </ul>

  • Laurent

    Hello, how can i insert a search in this wordpress theme ?
    I try many things but with no results :(

    Thanks

  • Sam

    What about logging users into wordpress, or better yet, creating new users from the app? I have been searching and am not coming up with any solution for that. If i had a more in-depth coding background it would probably be simple but im just not there. any suggestions would be awesome. Thanks

  • http://forgetdbigwords.com Okeowo Aderemi

    Superb Article,know i can use either this (jQTouch) or just use the article as a guide with Dojo Mobile.

  • shae

    I am having trouble getting image.php to work properly. It seems like it just wont load. Is there something I’m missing?

    How does it handle loading the various templates like archive.php, single.php, and in may case image.php

  • DSLR-A900

    Helló, én szeretem a blog. Van valami, amit tehetünk, hogy kapni, mint egy előfizetést vagy valamilyen dolog? Sajnálom, nem vagyok ismeri RSS?

  • http://www.solo2clic.com jose

    its very good this article. But only post comment fro pc the smartphone is difficult tipping text.
    Thanks

  • http://www.softplug.com Gino

    Great tuts, but i wanted to verify if your mobile site worked on jQtouch, on my iPod your website is not mobile at all. its the desktop browser version. and it take hours to load, okay, minutes

  • Nate

    I am also having trouble getting the other template files to work, such as search and archive. Does anyone have a solution set up for that?

  • Mark

    Very excited to find this tutorial. Thanks for a great job. Sadly when I got to Day 4, I hit a roadblock with the permalinks issue. Many sites have linked to articles on my site and adding the .html breaks the links. Could you please suggest a work around? I’ve found a couple of plugins such as WordPress Mobile, which have a similar UI and don’t require fudging with the permalinks settings but I’d really like to customize from this tutorial.

    Thanks.