• home
  • forum
  • my
  • kt
  • download
  • Working with Context Includes

    Author: 2009-03-06 10:58:03 From:

    Preface

    This tutorial assumes that you have a WordPress engine running on a server that you have access to upload files, download files and browse to. If you want to run a local server on your computer with a wordpress installation, there is a tutorial on that here for Windows, and here for OS X.

    Let me describe a little further in depth what we'll be doing. Many WordPress sites are more than just a blog these days. For those that integrate a Blog, a News System, and a Portfolio, there are many ways to distinguish between each of these posts. For a start, the amount of meta information WordPress attaches to each post is monumental. Dates, Categories, Custom Fields, Options, Tags, amount of tags, etc. It goes on. By picking on one of these, you can manipulate WordPress to display certain things even when the context changes (I.e. different category name, different tag, etc). The method I'm about to teach figures out when a certain category is attached to a certain post, and then bring in a different file accordingly. Contextually! If the context is of a blog, display it like a blog post! If the context is of a portfolio item, display it like a portfolio item! So on, so forth. You get it. Let's go!

    Step 1 - Basic Theme Necessities

    I've prepared a couple of files; style.css, index.php and some images (thanks TUTs sites!). We'll be building off these to create our final product. Download them, and place the folder in the wp-content/themes folder. Now go to the WordPress Dashboard, click 'design' or 'presentation' if you're still living in the stone age, and select the 'Context Files' theme. Great! Now it's activated we can dive into editing the files and getting on with the tutorial.

    Step 2 - the WordPress Posts

    For the method to work, a group of posts need to have a certain category. For this, I gave a few of them the category 'Blog', some 'theirNews', and left the rest to be the portfolio group, without any specific category. Make sure to do this, or your results wont be too varied. So make sure your posts (for this tutorial at least) are grouped in some way. It's vital!

    Step 3 - WordPress Code

    The functional way to describe what happens, is depending on the category, a specific file is included for the loop's code. The heirarchy looks like this:

    A Blog post will end up looking like this:

    A News item will display like this:

    And all other posts, or portfolio items will resemble this:

    Notice the little watermarks in the top right of each post? That's proof our system will work!

    Anyway. Between the body tags, we need a header and a loop. Add this:

    view plaincopy to clipboardprint?
    1. <div id="header">   
    2.     <h1><a href="<?php bloginfo('url'); ?>" title="Home"><?php bloginfo('name'); ?></a></h1>   
    3.     <p class="description"><?php bloginfo('description'); ?></p>   
    4. </div>   
    5. <div id="content">   
    6.     <?php if(have_posts()) : while(have_posts()) : the_post();   
    7.            
    8.         //Getting the category and checking it against certain   
    9.         //values to include the correct file goes here...   
    10.            
    11.     endwhile;   
    12.     endif; ?>   
    13. </div>  

    Next up, we need to get the category for each post. This uses some Custom WordPress/PHP syntax. Usually you could use get_the_category for the value, but you can get ANY category information via this method (goes under or replaces the comment in PHP):

    view plaincopy to clipboardprint?
    1. foreach((get_the_category()) as $category){   
    2.     $categoryName = $category->cat_name.'';   
    3. }  

    Now with that, we need to check this value against a value. If you named your categories 'Blog' or 'theirNews', we can now check the $categoryName variable against exactly those names/values. Remember this is in the loop, so this check is made for each individual post.

    view plaincopy to clipboardprint?
    1. if($categoryName == 'Blog'){ include(TEMPLATEPATH.'/blogTemplate.php'); }   
    2. elseif($categoryName == 'theirNews'){ include(TEMPLATEPATH.'/newsTemplate.php'); }   
    3. else(include(TEMPLATEPATH.'/portfolioTemplate.php'));  

    Each line of the 3 above is relatively the same. The PHP '==' means 'is equal to'. It's two ='s because when using only one = defines a variable. Not what we want. If, elseif, and else are a few conditional PHP tags. You can use multiple elseif statements if you wish to have more than three files!

    With the PHP done, we can now create the actual files that are included above. What's interesting about this template is that you can keep the entire loop, category, and includes all within one set of PHP tags. Efficiency!

    blogTemplate.php

    These files are basically what goes in the loop, consisting of loop template tags. So we can assign each category with the top right watermark, a different class is applied to each different category file. Otherwise, this is all basic WordPress stuff. Create a new file called 'blogTemplate.php' in the theme folder, and fill it up with some PHP goodness!

    view plaincopy to clipboardprint?
    1. <div class="post blog">   
    2. <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>   
    3. <p class="postInfo">Posted by <?php the_author(); ?> | Filed under <?php the_category(' & '); ?></p>   
    4. <?php the_content(''); ?>   
    5. <p class="postMetaData">   
    6.     <a href="<?php the_permalink(); ?>">Read More</a> |   
    7.     <?php comments_popup_link('Comments(0)','Comments(1)','Comments(%)'); ?> |    
    8.     <?php the_time('F d, Y'); ?>   
    9. </p>   
    10. </div>  

    Just one thing to note. I know it's very bad practice for SEO reasons, but the_content('') has the 2 's so that no 'read more' is displayed. The 'Read More' link in the postMetaData makes up for this!

    newsTemplate.php

    This is pretty minimalistic. The header is smaller too!

    view plaincopy to clipboardprint?
    1. <div class="post news">   
    2. <h3><?php the_title(); ?></h3>   
    3. <?php the_content(''); ?>   
    4. <p class="postMetaData"><a href="#">Visit Source</a> | <?php the_time('F d, Y'); ?></p>   
    5. </div>  

    portFolio.php

    Finally, the last sub template file.

    view plaincopy to clipboardprint?
    1. <div class="post portfolio">   
    2. <img src="<?php bloginfo('template_directory'); ?>/images/portfolio.png" alt="" />   
    3. <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>   
    4. <?php the_content(''); ?>   
    5. <p class="postMetaData"><a href="<?php the_permalink(); ?>">View Project</a></p>   
    6. </div>  

    And that's all the PHP/HTML you'll be needing! If you load your WordPress page, it'll now resemble something like below:

    Note you can already see the differences in each post!

    Step 4 - CSS

    Now to make it pretty! We'll start off with some defaulting code. This is to set it to a base look, get rid of stuff we don't want and style general tags (i.e. headers, etc).

    view plaincopy to clipboardprint?
    1. a{   
    2.     text-decorationnone;   
    3.     color#b93a00;   
    4. }   
    5.   
    6. *{   
    7.     margin: 0;   
    8.     padding: 0;   
    9. }   
    10.   
    11. body{   
    12.     background#000;   
    13.     color#5b5b5b;   
    14.     font-family"Lucida Grande", Lucida, Verdanasans-serif;   
    15.     font-size: 75%;   
    16. }   
    17.   
    18. h1, h2, h3, h1 a, h2 a, h3 a{   
    19.     font-family:"Trebuchet MS"ArialHelvetica;   
    20.     color#fff;   
    21.     letter-spacing: -2px;   
    22.     text-decorationnone;   
    23. }   
    24.   
    25. h1 a:hover, h2 a:hover, h3 a:hover{   
    26.     color#8b8b8b;   
    27. }   
    28.   
    29. h2{   
    30.     font-size35px;   
    31.     margin-bottom10px;   
    32. }   
    33.   
    34. h3{   
    35.     font-size20px;   
    36.     color#a8a8a8;   
    37.     letter-spacing: -1px;   
    38.     padding-bottom20px;   
    39. }  

    Now we need some structure to our page.

    view plaincopy to clipboardprint?
    1. #wrapper{   
    2.     margin: 0 auto;   
    3.     width500px;   
    4.     font-size11px;   
    5. }   
    6.   
    7. #header{   
    8.     height150px;   
    9.     font-family: Georgia, 'Times New Roman', Times, serif;   
    10.     border-bottom1px solid;   
    11.     border-color#222;   
    12. }   
    13.   
    14. #content{   
    15.     padding-top20px;   
    16. }   
    17.   
    18. .post{   
    19.     margin-bottom40px;   
    20.     min-height150px;   
    21. }  

    Next we'll add the little blackground watermarks in:

    view plaincopy to clipboardprint?
    1. .blog{   
    2.     backgroundurl(images/blogbg.png) no-repeat top rightright;   
    3. }   
    4.   
    5. .portfolio{   
    6.     backgroundurl(images/portfoliobg.png) no-repeat top rightright;   
    7.     min-height150px;   
    8. }   
    9.   
    10. .news{   
    11.     backgroundurl(images/newsbg.png) no-repeat top rightright;   
    12.     padding-right100px;   
    13. }  

    Our theme is taking shape! All that's left is some image styling and the styling of the postMetaData!

    view plaincopy to clipboardprint?
    1. #header h1{   
    2.     font-size50px;   
    3.     padding-top30px;   
    4. }   
    5.   
    6. #header p.description{   
    7.     font-styleitalic;   
    8.     font-size16px;   
    9. }   
    10.   
    11. .post img{   
    12.     floatleft;   
    13.     padding-right10px;   
    14.     padding-bottom20px;   
    15.        
    16. }   
    17.   
    18. .post p{   
    19.     padding-bottom15px;   
    20. }   
    21.   
    22. .postInfo{   
    23.     padding10px;   
    24. }   
    25.   
    26. .postMetaData{   
    27.     padding10px;   
    28.     background#141414;   
    29.     margin10px 0;   
    30.     width480px;   
    31.     displayblock;   
    32.     clearboth;   
    33. }   
    34.   
    35. .postMetaData a{   
    36.     color#06f;   
    37. }  

    The last section of code needed, your theme should now look complete! This tutorial teaches you a couple of things. A versitile way to get category info, how to use this in conjunction with conditional tags, and grouping PHP!

    Final

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Ad Management (6)
      Calendars (3)
      Chat Systems (8)
      Content Management (39)
      Cookies and Sessions (11)
      Counters (15)
      Database Related (20)
      Date and Time (13)
      Development (19)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (13)
      Error Handling (7)
      File Manipulation (24)
      Flash and PHP (6)
      Form Processing (19)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot