WordPress Theme Tutorial Pt 3

Create WordPress ThemesI continue showing you how to create WordPress Themes. I’ll show you the numerous ways you can access the information stored in the WordPress database.

If you missed the previous tutorials check out WordPress Theme Tutorial.

I’m not approaching this topic in the normal way. I’m presenting a bottom up complete coverage on exactly how WordPress Themes work and not a quick fix.

I’ll specifically go over how to  do the following in this video:

  • Easily Display Lists of Pages, Posts, Categories, Links, Etc.
  • Send Queries to the Database
  • Scrub Queries to Avoid Potentially Dangerous Code
  • Access Information on People Logged onto your Site
  • Using the Global Variables: $current_user, $post and $authordata

And, much much more…

The code follows the video. Leave questions and comments below.

Code From the Video

index.php

<?php

/**

* @package WordPress

* @subpackage Default_Theme

*/

 

get_header(); ?>

 

 

<div id=”content” class=”narrowcolumn” role=”main”>

 

<h3>Pages</h3>

<!– Creates a bulleted list of all pages in your blog –>

<h3>Pages</h3>

<?php wp_page_menu(‘show_home=1&sort_column=menu_order’); ?>

 

<!– Creates a bulleted list of all pages in your blog –>

<h3>Same Pages</h3>

<ul>

<?php wp_list_pages(‘title_li=’); ?>

</ul>

 

<!– Dropdown list for all the pages –>

<?php wp_dropdown_pages(); ?>

 

<!– List of all categories on the blog –>

<h3>Categories</h3>

<ul>

<?php wp_list_categories(‘title_li=’); ?>

</ul>

 

<!– Dropdown list for all the categories –>

<?php wp_dropdown_categories(); ?>

 

<!– List of all the links in the blogroll –>

<ul>

<?php wp_list_bookmarks(‘title_li=’); ?>

</ul>

 

<!– Prints the tag cloud to the screen –>

<h3>Tag Cloud</h3>

<ul>

<?php wp_tag_cloud(”); ?>

</ul>

 

<!– You use $wpdb to access all the functions available to access the database. You must first declare it as a global variable to use it. –>

<?php global $wpdb; ?>

 

<!– get_results() will return is used to retrieve multiple rows of data that is stored in an array –>

<!– prepare() scrubs potentially dangerous code out of the query –>

<!– You point at the posts by referring to $wpdb->posts –>

<?php $allPostTitles = $wpdb->get_results( $wpdb->prepare( “SELECT DISTINCT post_title FROM $wpdb->posts” ) ); ?>

 

<?php foreach ($allPostTitles as $postTitle) {

echo $postTitle->post_title . ‘<br />’; } ?>

 

<br />

 

<!– You point at the comments by referring to $wpdb->comments –>

<?php $allComments = $wpdb->get_results( $wpdb->prepare( “SELECT comment_author, comment_content FROM $wpdb->comments” ) ); ?>

 

<?php foreach ($allComments as $commentBlock) {

echo $commentBlock->comment_author . ‘ said: <br />’;

echo $commentBlock->comment_content . ‘<br /><br />’; } ?>

 

<?php $temp = $wp_query; ?>

<?php $wp_query = null; ?>

 

<!– Create a new $wp_query object –>

<?php $wp_query = new WP_QUERY;?>

 

<?php echo “Hi ” . $current_user->first_name . ” ” . $current_user->last_name; ?><br /><br />

 

<!– Calls the custom query using the query() function. You must append the variable $paged so that the current page is passed. This allows you to use the newer and older entries links. –>

<?php $wp_query->query(‘posts_per_page=8&orderby=title&order=DSC&paged=’.$paged); ?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

 

<h3><a href='<?php echo $post->guid; ?>’><?php echo “Title: ” . $post->post_title; ?></a></h3>

 

<?php echo “Author: ” . $authordata->first_name . ” ” . $authordata->last_name; ?><br />

 

<?php echo “Date: ” . $post->post_date; ?><br />

<?php echo “Content: ” . $post->post_content; ?><br />

<?php echo “Excerpt: ” . $post->post_excerpt; ?><br />

<?php echo “Comments: ” . $post->comment_count; ?><br />

<?php echo “ID: ” . $post->ID; ?><br />

<?php echo “Userid: ” . $post->post_author ?><br />

 

 

 

 

<?php endwhile; ?>

 

<div class=”navigation”>

<div class=”alignleft”><?php next_posts_link(__(‘&laquo; Older Entries’, ‘kubrick’)) ?></div>

<div class=”alignright”><?php previous_posts_link(__(‘Newer Entries &raquo;’, ‘kubrick’)) ?></div>

</div>

 

 

<!– Reset the value for $wp_query –>

<?php $wp_query = null; ?>

<?php $wp_query = $temp; ?>

 

 

</div>

 

<?php get_sidebar(); ?>

 

<?php get_footer(); ?>

 

12 Responses to “WordPress Theme Tutorial Pt 3”

  1. Hi Derek-Great tutorials. Was wondering if you might be able to help me with a specific problem. I would like to be able to bring scripts into daily posts in WP. One of the plugins that I have built an interesting rendition of is Clouds Carousel:http://www.professorcloud.com/mainsite/carousel.htm.

    I have FTP’d the scripts into wp-admin/js folder and have linked all images and the scripts with html. Now that I have this all done I have NO IDEA where to put the html page! In addition, I have no idea if it will work!

    I have utilized javascript for my header and it works alright except in opera and MIE…(it works but object have moved in position in these browsers). From what I’ve read it seems js will work alright in headers but is difficult for pages.

    My thought is that since I have the scripts uploaded to wp-admin I would be able to use them whenever if I could figure out how to import the html into specific posts.

    Is this something you could help me with?

    Thank you!!

    jaxon
    jaxoons.com

    • admin says:

      Beautiful! I love it. I’ll show you how to create plugins, widgets and everything else in this tutorial. I’ll see if I can turn your nice code into a plugin / widget also

  2. sreekanth says:

    its really very nice & useful data

  3. Bobby says:

    Hi Sir Derek I just want to ask how to limit . I’m a complete beginner, I just want to shorten the_content(). Just a few characters to be display, is there a way like that? Thanks.

    • admin says:

      Sure the substr function in php will do that.

      echo substr(‘abcdef’, 1, 3); // returns bcd

      The first value is the string to shorten
      The second is the starting index for the string 0 is the first character
      The third is how many characters you want returned

      • Bobby says:

        Yes I can use that in the future Sir Derek but I just want, I think excerpt is the correct term. e.g Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

        -That is the_content(). I want something an output like (Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s) just an extracted from the content but not the whole content. I’m sorry I can’t explain it more clearly. I hope you get it Sir. Thanks.

        • admin says:

          Here is example code that grabs titles, shortened excerpts, link to the article, etc.


          query(‘posts_per_page=5&category_name=Featured’);

          // Cycle through all of the posts
          while ($wp_query->have_posts()):
          $wp_query->the_post();
          ?>

          <a href="”>post_title; echo substr($ntt_the_title, 0, 20); ?>

          post_excerpt; echo substr($ntt_the_excerpt, 0, 50); ?>
          <a href="” class=”clickForMore”>More

          <img src="” alt=”” width=”60px” height=”60px” class=”featuredImage” />

          • Jason says:

            Hi Derek,

            Your videos have been so helpful and without them I doubt I would have gotten as far as I have. I’m actually almost finished with my first wordpress powered website.

            The last obstacle is related to the question by Bobby and your answer. I’ve been trying to get an excerpt for a title and a post from my blog section to appear on the main page of the website.

            I’ve used your code above, tweaked it and while I get the permalink title to come thru it is not shortened at all. I’ve played with the attributes but no change.

            Also, the excerpts for the Posts does not even appear at all. It is blank. Can you take a look at my code?


            <a href="">post_title; substr (0, 10); ?>

            post_excerpt; echo substr (0, 60); ?>

            This really is the last thing I need to get thru in order to finish. Any help would be greatly appreciated.

            • admin says:

              This is what I did. Do you have an excerpt for the post? You could also use < ?php the_excerpt(); ?>, but the code below is tested and works for me. There is probably some stray error. Try copying and pasting the code directly from my tutorial WordPress News Theme


              < ?php $tempWPQuery = $wp_query; $wp_query = null; // Create a new WP_Query Object $wp_query = new WP_Query(); // Issue a query that returns 4 posts $wp_query->query(‘posts_per_page=5&category_name=Featured’);

              // Cycle through all of the posts

              while ($wp_query->have_posts()):

              $wp_query->the_post();

              ?>

            • ”>< ?php $ntt_the_title = $post->post_title; echo substr($ntt_the_title, 0, 28); ?>

              < ?php $ntt_the_excerpt = $post->post_excerpt; echo substr($ntt_the_excerpt, 0, 50); ?>

              ” class=”clickForMore”>More

              ” alt=”< ?php the_title(); ?>” width=”60px” height=”60px” class=”featuredImage” />

            • < ?php endwhile; // Reset wp_query to the default $wp_query = null; $wp_query = $temp; ?>

              < !– End of featuredPosts div –>

  • Jason says:

    The code was cut off. I hope you can see this….


    <a href="">post_title; substr (0, 10); ?>

    post_excerpt; echo substr (0, 60); ?>

  • Jason says:

    Sorry, how do I paste in code correctly? I has stripped the PHP and HTML preceding it.

  • Leave a Reply

    Your email address will not be published.

    Google+