SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    WordPress Category Page PHP

    Hello!

    I am running Wordpress 2.7.0.

    I have a question about the speed of certain php functions.

    Currently I am using this code on a category page to show descriptions based on the category. This is just a small sample of the code and it repeats for over 500 categories.

    PHP Code:
    <?php if( in_category(204) ) : ?>
    <p>Message 1</p>
    <?php else : ?>

    <?php endif; ?>
    <?php 
    if( in_category(205) ) : ?>
    <p>Message 2</p>
    <?php else : ?>

    <?php endif; ?>
    <?php 
    if( in_category(206) ) : ?>
    <p>Message 3</p>
    <?php else : ?>

    <?php endif; ?>

    Would there be any benefit to creating a separate category page for each category in terms of server load and/or page load time?

    If the answer is no, would it help to combine my code into one statement with the use of many elseifs?

    Thanks in advance.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's better to keep it in one file. More chance of it staying in the file cache in memory than if you have 500 separate files.

    Why not throw these descriptions into an array, keyed by the category ID, instead of all this code?

    PHP Code:
    $descriptions = array(
      
    204 => 'Message 1',
      
    205 => 'Message 2',
      
    206 => 'Message 3',
      ...
    )

    $categories get_the_category();
    foreach(
    $categories as $category) { 
      echo 
    $descriptions[$category->cat_ID];


  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    Thanks for the fast reply!

    I have two questions about your method:

    1.) My sample text was just a dumbed down version of what I have. Can I call php and or html in each array?

    2.) Is there a way to add an else to the array?


    Thanks in advance [=

  4. #4
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about

    Code:
    $descriptions = array(
      204 => '204.php',
      205 => '205.php',
      206 => '206.php',
      ...
    )
    
    $categories = get_the_category();
    foreach($categories as $category) { 
      include ($descriptions[$category->cat_ID]);
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •