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

Displaying specific category WordPress

  • SOLVED

Hello,

I'm using a theme that has two sections on the home page - 1. Hot Selling Items - 2. Recently Added Items...

Currently it grabs the title and image of each product/post from all the categories and randomly displays them in both boxes / section...

I wish to have the top section "Hot Selling Items" Display products from only ONE category - and a separate category to be displayed in the "Recently Added Items" box / section.

I've tried using the category_name=category-name method but that doesn't work.

Here is the code from the theme:


<?php if(get_option("display_home_hotsell") !="no"){ ?>

<h1 class="ItemBoxTop" style="margin-top:0px;">
<b class="ItemLineHeight"><?php echo SPEC($GLOBALS['_LANG']['_home2']) ?></b></h1>
<div class="ItemBox"><div class="inner"><div id="gallerycontent">



<?php $data = shopperPress_featured($TOTALFEATURED,"rand");

$i=1; while($i < $TOTALFEATURED){ ?>

<div class="thumb" style="height:200px;">

<div style="height:130px; overflow:hidden;"><center>
<a href="<?php echo $data[$i]['link']; ?>" rel="bookmark">
<img src="<?php echo $PPT->ImageCheck($data[$i]['image']); ?>" alt="<?php echo str_replace("-","",$data[$i]['title']); ?>"/>
</a>
</center></div>

<p class="themelabel">
<a href="<?php echo $data[$i]['link']; ?>" style="text-decoration:none;">
<strong><?php echo $data[$i]['title']; ?></strong>
</a>
</p>

</div>

<?php $i++; } ?>

</div><div style="clear:both;"></div></div></div>


<?php } ?>


********* Hi Moar - thanks for fast reply ********

Here is the function detailing shoppPress_featured below


function shopperPress_featured($NumberOfPosts = 1, $orderby="rand") {

global $wpdb; $i=1; $gotsofar=0;

if($NumberOfPosts ==""){ $NumberOfPosts=5; }
$searchDouble = $NumberOfPosts*2;
$posts = query_posts('&posts_per_page='.$searchDouble.'&orderby='.$orderby.'&meta_key=price');

foreach($posts as $thepost) { if(get_post_meta($thepost->ID, "type", true) != "article"){

$customData[$i]['price'] = get_post_meta($thepost->ID, "price", true);
$customData[$i]['id'] = $thepost->ID;
$customData[$i]['title'] = $thepost->post_title;
$customData[$i]['description'] = $thepost->post_content;
$customData[$i]['price'] = GetPrice(get_post_meta($thepost->ID, "price", true));
$customData[$i]['old_price'] = GetPrice(get_post_meta($thepost->ID, "old_price", true));
$customData[$i]['price'] = GetPrice($customData[$i]['price']);
$customData[$i]['image'] = get_post_meta($thepost->ID, "thumbnail", true);

if($customData[$i]['image'] ==""){
$customData[$i]['image'] = get_post_meta($thepost->ID, "image", true);
}
if($customData[$i]['image'] ==""){
$customData[$i]['image'] = "wp-content/themes/shopperpress/images/noimg.gif";
}

$customData[$i]['link'] = get_permalink($thepost->ID);
$i++;
$gotsofar++;

if($gotsofar == $NumberOfPosts){ wp_reset_query(); return $customData; }

} }



******* It's also defined again here under the first section I'm wanting to display a specific category *******


function shopperpress_homepage_hotselling($items){

global $wpdb;

$DisplayNum = get_option("display_homepage_num");

$countRemains = $DisplayNum+1;

$data = shopperPress_featured($countRemains);

$i=1; while($i < $countRemains){

$content .= '<div class="Row460" style="border-bottom:1px solid #eee; margin-bottom:10px;">
<div class="Rowa">
<a href="'.$data[$i]['link'].'">
<img src="'.get_bloginfo('template_url').'/themes/'.get_option('theme').'/images/badge.gif" alt="hot item" />
</a>
</div>
<div class="Rowb">
<a href="'.$data[$i]['link'].'">
'.substr($data[$i]['title'],0,55).'
</a>
<br />
'. substr(strip_tags($data[$i]['description']),0,130).'...
</div>

<div class="Rowc">
<div style="height:80px; overflow:hidden;">
<a href="'.$data[$i]['link'].'">
<center><img src="'.SPImage($data[$i]['image']).'" alt="'.$data[$i]['title'].'" style="max-width:80px; max-height:80px;" /></center>
</a>
</div>
</div>
<div style="clear:both;"></div>
</div>';


$i++;


}

return $content;

}

Answers (2)

2010-10-13

Maor Barazany answers:

Please check where in the template there is defined the function <strong>function shopperPress_featured()</strong> and paste it here, since this code doesn't actually has the loop inside.

The function may be declared in the functions.php file, or maye in another file, just do a wide inner file search using the editor and locate where this function is.
After you paster it here, there will be more possibility to help you with this issue.


Maor Barazany comments:

You should eliminate the first function to grab only posts from categories you want,

First option, is to tell the function which categories to display, let's say the id of the category you want to display is <strong>4</strong>, so change -

$posts = query_posts('&posts_per_page='.$searchDouble.'&orderby='.$orderby.'&meta_key=price');

to this:

$posts = query_posts('&posts_per_page='.$searchDouble.'&orderby='.$orderby.'&meta_key=price&cat=4');

But it will grab only posts from this category.
So, if you want, you can add a parameter to the function, and pass it anywhere there is a call to the <strong>shopperPress_featured</strong> fuctions.

Change this

function shopperPress_featured($NumberOfPosts = 1, $orderby="rand") {



global $wpdb; $i=1; $gotsofar=0;



if($NumberOfPosts ==""){ $NumberOfPosts=5; }

$searchDouble = $NumberOfPosts*2;

$posts = query_posts('&posts_per_page='.$searchDouble.'&orderby='.$orderby.'&meta_key=price');



to this -

function shopperPress_featured($NumberOfPosts = 1, $orderby="rand", $cat="all") {

global $wpdb; $i=1; $gotsofar=0;


if($NumberOfPosts ==""){ $NumberOfPosts=5; }

$searchDouble = $NumberOfPosts*2;

if ($cat == "all") {
$posts = query_posts('&posts_per_page='.$searchDouble.'&orderby='.$orderby.'&meta_key=price');
}
elseif (is_numeric($cat) {
$posts = query_posts('&posts_per_page='.$searchDouble.'&orderby='.$orderby.'&meta_key=price&cat='.$cat);
}
else {
$cat = str_replace('|', ',', $cat)
$posts = query_posts('&posts_per_page='.$searchDouble.'&orderby='.$orderby.'&meta_key=price&cat='.$cat);
}

{


<strong>Then </strong>, you change the second function to eliminate the cat id , to a call like, instead


$data = shopperPress_featured($countRemains);


put this -


$data = shopperPress_featured($countRemains, 'rand', '4');

And it will grab only posts from category ID number 4.
If you want only to <strong>exclude</strong> let's say categry number 5, so have this:


$data = shopperPress_featured($countRemains, 'rand', '-5');



And if you want to grab posts from several category IDs, let's say from 3,6,8, so use this:


$data = shopperPress_featured($countRemains, 'rand', '3|6|8');


If you won't give a number, then it will take all categories, like it's working now.

Hope it helps..

2010-10-13

enodekciw answers:

function shopperPress_featured($NumberOfPosts = 1, $orderby="rand",$category) {
global $wpdb; $i=1; $gotsofar=0;
if($NumberOfPosts ==""){ $NumberOfPosts=5; }
$searchDouble = $NumberOfPosts*2;
$posts = query_posts('category_name='.$category.'&posts_per_page='.$searchDouble.'&orderby='.$orderby.'&meta_key=price');

foreach($posts as $thepost) { if(get_post_meta($thepost->ID, "type", true) != "article"){
$customData[$i]['price'] = get_post_meta($thepost->ID, "price", true);
$customData[$i]['id'] = $thepost->ID;
$customData[$i]['title'] = $thepost->post_title;
$customData[$i]['description'] = $thepost->post_content;
$customData[$i]['price'] = GetPrice(get_post_meta($thepost->ID, "price", true));
$customData[$i]['old_price'] = GetPrice(get_post_meta($thepost->ID, "old_price", true));
$customData[$i]['price'] = GetPrice($customData[$i]['price']);
$customData[$i]['image'] = get_post_meta($thepost->ID, "thumbnail", true);
if($customData[$i]['image'] ==""){
$customData[$i]['image'] = get_post_meta($thepost->ID, "image", true);
}
if($customData[$i]['image'] ==""){
$customData[$i]['image'] = "wp-content/themes/shopperpress/images/noimg.gif";
}
$customData[$i]['link'] = get_permalink($thepost->ID);
$i++;
$gotsofar++;
if($gotsofar == $NumberOfPosts){ wp_reset_query(); return $customData; }
} }


replace your given function with mine and then use it like this:

$data = shopperPress_featured($TOTALFEATURED,"rand", 'category name');

for example:

$data = shopperPress_featured($TOTALFEATURED,"rand", 'hot selling');

or whatever your hot selling item category is called ;)


enodekciw comments:

oh, cool, Mario..


enodekciw comments:

Maor, i meant, sorry :(