I have pages with the URL of
<strong>http://www.allentullett.co.uk/wards/...</strong>
I also have sub post categories for each ward that sit under the main category of 'Wards'
<strong>'Wards'
'Ward1'
'Ward2'
'Ward3'</strong>
Using only one template for all wards, how can I list the posts for that sub category on my page template by pulling the title '/Ward1' from the page slug and using that to identify the category?
My template is:
<?php
/*
Template Name: Ward Page
*/
function getUserList($type)
{
global $wpdb, $post;
$req = $_SERVER['REQUEST_URI'];
$pos = strpos($req, 'wards/')+strlen('wards/');
if($pos!==FALSE)
{
$ward = trim(substr($req, $pos), '/');
$ward = $wpdb->escape($ward);
//echo " $ward $type <br/>";
$q = "SELECT ID FROM wp_users u, wp_usermeta m1, wp_usermeta m2 WHERE u.ID = m1.user_id AND u.ID = m2.user_id AND ( m1.meta_key = 'ward' OR m1.meta_key = 'ward2' ) AND m1.meta_value = '$ward' AND m2.meta_key = 'electedmember' AND m2.meta_value = '$type' LIMIT 3 ";
$res = $wpdb->get_results($q);
if($res)
{
foreach($res as $uid)
{
$userid = $uid->ID;
$user_info = get_userdata($userid);
$current_link = get_author_posts_url($userid,$user_info->display_name);
$current_link = str_replace(' ', '-', strtolower($current_link));
echo '<a href="'.$current_link.'">'.$user_info->display_name.'</a><br>';
}
}
}
}
?>
<?php get_header(); ?>
<div id="leather">
<div id="content-container" class="clearfloat">
<div class="wrapper clearfloat">
<?php
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<?php
if ($children) { ?>
<div class="submenu">
<h4 class="pagetitle">In this section:</h4>
<ul class="subnav">
<?php echo $children; ?>
</ul>
</div>
<?php } ?>
<?php wp_link_pages('before=<p>&after=</p>&next_or_number=number&pagelink=page %'); ?>
<div id="small-blue-title">
<?php
$parent_title = get_the_title($post->post_parent);?>
<?php echo $parent_title;?>
<?php echo $children;?>
</div>
<div id="title">
<?php the_title(); ?>
</div>
<div id="page_content">
<div id="ward-info-container">
<div id="sub-small-blue-title">
Councillors & Surgery Information
</div>
<div id="ward-info-container-blue">
<div id="ward-info-block-left">
<div id="ward-info-block-title">
City Councillors
</div>
<div id="ward-info-block-links">
<?php
// insert list of 'citycouncillor' + 'northgate' users here
getUserList('city-councillors');
?>
</div>
</div><!--END INFO BLOCK LEFT-->
<div id="ward-info-block-left">
<div id="ward-info-block-title">
County Councillor
</div>
<div id="ward-info-block-links">
<?php
// insert list of 'countycouncillor' + 'northgate' users here
getUserList('county-councillors');
?>
</div>
</div><!--END INFO BLOCK LEFT-->
<div id="ward-info-block-right">
<div id="ward-info-block-title">
Surgery Information
</div>
<div id="ward-info-block-links">
<?php echo get_post_meta($post->ID, 'Surgery-Info', TRUE); ?><br>
</div>
</div><!--END INFO BLOCK RIGHT-->
</div><!--END WARD INFO CONTAINER BLUE-->
</div><!--END WARD INFO CONTAINER-->
<?php the_content(); ?>
// category listing here
<div id="edit">
<?php edit_post_link(__("+ Edit this page"), ''); ?>
</div>
<div id="page-parent-link">
<?php $parent_title = get_the_title($post->post_parent);?><a href="<?php echo get_permalink($post->post_parent) ?>">+ Go to <?php echo $parent_title;?></a> <?php echo $children;?>
</div>
</div>
</div><!--END POST-->
<?php endwhile; endif; ?>
</div><!--END COLUMN-->
</div><!--END CONTAINER-->
</div><!--END FLOATS-->
<?php get_footer(); ?>
Baki Goxhaj answers:
Here is the solution:
<?php
// When page name is the same as category name, the query is succesfull - Baki @ wplancer.com
$page_slug = $post->post_name;
query_posts( 'category_name=' . $page_slug );
if (have_posts()) : while (have_posts()) : the_post(); ?>
// Do your stuff here
<?php endwhile; endif; ?>
<strong>How it works:</strong> First get page name, which is the same as a category or subcategory name, than use the same name to query posts of that category.
Allen Tullett comments:
I used this in the end it worked perfectly. But thanks.
<?php
$pageslug = $post->post_name; // name of the page (slug)
$catslug = get_category_by_slug($pageslug); // get category of the same slug
$catid = $catslug->term_id; // get id of this category
$query= 'cat=' . $catid. '';
$queryObject = new WP_Query($query);
// The Loop...
$query = 'posts_per_page=10';
if ($queryObject->have_posts()) {
while ($queryObject->have_posts()) {
$queryObject->the_post();
include (TEMPLATEPATH . "/loop-news.php");
}
}
?>
:-)
Ashfame answers:
You need to use <blockquote>is_category()</blockquote> function
Check here http://codex.wordpress.org/Conditional_Tags#A_Category_Page
Allen Tullett comments:
After looking into is_category() I came across this...
<?php
$pageslug = $post->post_name; // name of the page (slug)
$catslug = get_category_by_slug($pageslug); // get category of the same slug
$catid = $catslug->term_id; // get id of this category
$query= 'cat=' . $catid. '';
query_posts($query); // run the query
?>
of which does display the title of the latest post, but in a hideous mess. It replicates my page content non stop.
How can this be extended/altered to display just a list with the <em>title</em> and <em>excerpt</em> without affecting the remainder of the pages content?
:-)
Ashfame comments:
Debug the code a little and check the value of category ID before it is queried. The loop is I think because of the parent child category thing here.
Check what you have in your code and then work around. Getting me?
If I were you, I would first try to create a template for wards category by having a file category-wards.php and then catching the child categories in it.
You may probably need get_category_parents() function at some point of time.
Hack around.
P.S - Your quote for the question is too much for someone to give you a tested code and the reason why you didn't get a good response.