Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Make this series of queries more efficient WordPress

  • SOLVED

I'd like to reduce this bulky series of queries down to a single query.

<?php query_posts(array ('category_name' => 'bioengineering-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="http://bioengineering.missouri.edu/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>Biological Engineering</span>
</a>
<?php endwhile;?>


<?php query_posts(array ('category_name' => 'bioengineering-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="/chemical/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>Chemical Engineering</span>
</a>
<?php endwhile;?>

<?php query_posts(array ('category_name' => 'bioengineering-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="/civil/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>Civil &amp; Environmental</span><br />
<span>Engineering</span>
</a>
<?php endwhile;?>

<?php query_posts(array ('category_name' => 'cs-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="http://cs.missouri.edu/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>Computer Science</span>
</a>
<?php endwhile;?>

<?php query_posts(array ('category_name' => 'ece-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="/ece/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>Electrical &amp; Computer</span>
<span>Engineering</span>
</a>
<?php endwhile;?>

<?php query_posts(array ('category_name' => 'imse-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="/imse/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>Industrial &amp; Manufacturing</span><br />
<span>Systems Engineering</span>
</a>
<?php endwhile;?>

<?php query_posts(array ('category_name' => 'it-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="http://itprogram.missouri.edu/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>Information Technology</span>
</a>
<?php endwhile;?>

<?php query_posts(array ('category_name' => 'mae-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="/mae/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>Mechanical &amp; Aerospace</span><br />
<span>Engineering</span>
</a>
<?php endwhile;?>

<?php query_posts(array ('category_name' => 'muii-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="http://muii.missouri.edu/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>MU Informatics Institute</span>
</a>
<?php endwhile;?>

<?php query_posts(array ('category_name' => 'naval-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="http://nrotc.missouri.edu/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>Naval Sciences</span>
</a>
<?php endwhile;?>

<?php query_posts(array ('category_name' => 'bioengineering-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="http://nsei.missouri.edu/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>Nuclear Science &amp;</span><br />
<span>Engineering Institute</span>
</a>
<?php endwhile;?>

<?php query_posts(array ('category_name' => 'bioengineering-cat',
'posts_per_page' => 1)
); ?>
<?php while (have_posts()) : the_post(); ?>

<a href="/contact/"
class="home-department"
style="background: url(
<?php
//Get the Thumbnail URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), medium, false, '' );
echo $src[0];
?>
);background-position:center center">
<span>Other Engineering Offices</span>
</a>
<?php endwhile;?>


Answers (1)

2010-12-24

rilwis answers:

WP still doesn't support single query that takes 1 post per category. So, I suggest optimizing code as following:

Because all your loops have same structure and syntax, I suggest creating a function that display loops like this (paste in functions.php file):

// show loop by category name and array of link attributes
function rw_multi_loop_show($cat, $link_array) {
query_posts(array('category_name' => $cat, 'posts_per_page' => 1));
while (have_posts()) : the_post();
//Get the Thumbnail URL
$src = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'medium', false, '');
$src = $src[0];

foreach ($link_array as $a) {
echo "<a href='{$a[0]}' class='home-department' style='background:url($src) center center'>{$a[1]}</a>";
}
endwhile;
wp_reset_query();
}


And then, replace your loops above with this code:

<?php
rw_multi_loop_show('bioengineering-cat', array(
array('http://bioengineering.missouri.edu/', '<span>Biological Engineering</span>'),
array('/chemical/', '<span>Chemical Engineering</span>'),
array('/civil/', '<span>Civil &amp; Environmental</span><br /><span>Engineering</span>')
));

rw_multi_loop_show('cs-cat', array(
array('http://cs.missouri.edu/', '<span>Computer Science</span>')
));

rw_multi_loop_show('ece-cat', array(
array('/ece/', '<span>Electrical &amp; Computer</span><br /><span>Engineering</span>')
));

rw_multi_loop_show('imse-cat', array(
array('/imse/', '<span>Industrial &amp; Manufacturing</span><br /><span>Systems Engineering</span>')
));

rw_multi_loop_show('it-cat', array(
array('http://itprogram.missouri.edu/', '<span>Information Technology</span>')
));

rw_multi_loop_show('mae-cat', array(
array('/mae/', '<span>Mechanical &amp; Aerospace</span><br /><span>Engineering</span>')
));

rw_multi_loop_show('muii-cat', array(
array('http://muii.missouri.edu/', '<span>MU Informatics Institute</span>')
));

rw_multi_loop_show('naval-cat', array(
array('http://nrotc.missouri.edu/', '<span>Naval Sciences</span>')
));

rw_multi_loop_show('bioengineering-cat', array(
array('http://nsei.missouri.edu/', '<span>Nuclear Science &amp;</span><br /><span>Engineering Institute</span>'),
array('/contact/', '<span>Other Engineering Offices</span>')
));
?>