How to split category lists into 4 columns?
-
I read a few topics regarding this and tried what they suggested without any luck. I basically have 50 pages and categories(one for each state) and would like to display the state’s cities (sub categories) for each state on its respective page. I know how to do this but its one long column and I would like 4. Has anyone done this before? If so, any help or guidance is appreciated!
-
display this code in your category pages
<?php wp_list_categories(‘title_li=&child_of=’.$cat);?>
it will show sub -categories of the viewing category ..
i think this is wht u r looking for
Hi sribharath,
I know how to display the sub categories, I just need them to show in 4 columns not 1 long one.EX.
Sub-Cat1 Sub-Cat2 Sub-Cat3 Sub-Cat4
Sub-Cat5 Sub-Cat6 Sub-Cat7 Sub-Cat8
Sub-Cat9 Sub-Cat10 Sub-Cat11 Sub-Cat12
Sub-Cat1 Sub-Cat2 Sub-Cat3 Sub-Cat4I looked at that earlier but from what I understand it wouldn’t work. For example, on my New York state page I need to just display New York cities (the sub categories of New York). That plugin does what i am looking for but is there a way to display categories depending on which page you’re on?
See this thread for showing a category on a page.
Here is some sample code that shows how to create a 4 column list. However, the list runs down column 1, then to the top of column 2, etc, not across like your example.
I think you can adapt this to your needs. If not, let me know how you would like it changed.
<?php /* Template Name: testcolumns Author: Mac McDonald Contact: Use the 'Contact Us' page under 'About' at BluegrassMiataClub.com */ ?> <?php include(TEMPLATEPATH.'/header.php') ?> <style type='text/css'> <!-- .col-1 { width: 20%; float: left; } .col-2 { width: 20%; float: left; } .col-3 { width: 20%; float: left; } .col-4 { width: 20%; float: left; } .col-clear { clear: both; } --> </style> <div id="content"> <?php // Create some dummy entries to test the table $numlines = 45; // The number of entries to generate for ($i=1;$i<=$numlines;++$i) { $cities[] = "City $i"; } $cols = 4; $linespercol = ceil($numlines/$cols); $linesthiscol = 0; $thiscol = 0; echo "<div class='table'>"; for ($i=0;$i < sizeof($cities);++$i) { ++$linesthiscol; if ($linesthiscol > $linespercol) { $linesthiscol = 1; echo '</div>'; } if ($linesthiscol == 1) { ++$thiscol; echo "<div class='col-$thiscol'>"; } echo "<p>$cities[$i]</p>"; } echo "</div>"; // Column echo "<div class='col-clear'></div>"; echo "</div>"; // Table echo "</div>"; // Content ?> <?php include(TEMPLATEPATH.'/footer.php') ?>
Can I just put a similar code on my page.php so that anytime a category is displayed it will show with 4 columns (I have 50 pages/not posts + categories for each state, I need to display that states sub category cities on each)? For example, on New York states page, I need to show the sub categories of New York (which I know how to do). Or would I need to make a custom template? Thanks for the advice.
I think that page.php is not the correct template, but rather category.php, or archive.php if your theme does not have a category.php. Take a look at the Template Hierarchy in the Codex.
You could just put similar code in category.php, but I would strongly advise against it. In the future, you may want to add categories for other purposes, and then it would not work correctly. And, you will still be faced with telling category.php which category you want to select.
I would use category.php (or archive.php if no category.php) to make a custom template, say cities.php, and assign that as the template for each state page. Then use a Custom Field in each state page to tell the template which state category to post. Or, if the state page title is the name of the state, make your categories the state name and use the page title as the category name in the query.
I know this sounds confusing, but if you are interested in the Custom Field approach and you are using a free theme, post the theme name and I will reply with a template for you.
Thanks again, I actually follow you. I don’t have a category php so it would have to be on the archive php. I have thousands of categories (cities), hundreds for each state and right now i’m using <?php if (is_page(135)) wp_list_categories(‘child_of=10’); ?> in the page php. This is telling it to list California cities on the California page. I was assuming I’d have to enter that 50 times, once for each state. Am I on the right track? I guess I don’t understand how to use a custom field to tell the template which category/sub categories to post.
You know, its late – I was off track about not using page.php. You could use it as the start of a template or modify it with 50 if statements. If you do decide to modify it, you must still leave in the code for a normal page display for when it is called from a non-state page. So, I still strongly advise making a cities.php template and assigning it as the template to each state page.
Here is a sample UNTESTED of how a custom field would work. Assume the custom field is named ‘state-category’ with a value of a category id. The code would look something like this:
<?php $this_cat = get_post_meta($post->ID,'state-category',TRUE); if ($this_cat) { $args = array ( 'child_of' => $this_cat, 'echo' => 0, ); $cities = wp_list_categories($args); // Put the code here to do the columns } else { echo "This page is missing the state-category Custom Field"; }?>
Then you would save this as cities.php. Assign this as the template to each state page and add the state-category custom field.
If you use the 50 ‘if’ statements, you would do something like this:
<?php if (is_page(135)) {$cat = 10;} elseif (is_page(144)) {$cat = 14;} elseif .... if ($cat) { $args = { 'child_of' => $cat, 'echo' => 0, }; $cities = wp_list_categories($args); // Put the code here to do the columns } else { echo 'No state page found'; }?>
It occurred to me that there is a good chance that the titles of your state pages are the names of the states, and that the category names are also state names. If that is the case, you don’t need to use Custom Fields or 50 if statements. You can just use the page title, like this:
<?php $state = get_the_title(); $cat = get_cat_ID($state); if ($cat) { $args = { 'child_of' => $cat, 'echo' => 0, }; $cities = wp_list_categories($args); if ($cities) { // Put the code here to do the columns } else { echo "No cities found for state $title, category ID $cat"; } } else { echo "No category ID found for state $title"; }?>
- The topic ‘How to split category lists into 4 columns?’ is closed to new replies.