How to Show Accurate Category Post Count? - php

I've got some code in my blog sidebar that is designed to show the number of posts in a particular category. It works, kind of. It will only show up to the number 5 and then it stops updating the post count. Any ideas on how to fix?
Here's the code for one of the categories:
<?php
$count_posts = get_posts('post_type=post&category_name=Software')
?>
<li id="nav1">
<a class="<?php if ($current_cat == get_cat_ID('Software')) echo "software-active active"; ?>" href="<?php echo get_category_link(get_cat_ID('Software')) ?>" title="Software">Software (<?php echo count($count_posts) ?>)</a>
Here's a link to the blog. thanks for your help!

Change:
get_posts('post_type=post&category_name=Software')
To:
get_posts('post_type=post&category_name=Software&numberposts=-1');

Related

?php the_permalink() returns wrong post ID and 404 error

I'm having an issue with grabbing the correct post ID using the_permalink() -- see code below:
<?php
while( $wpr->have_posts() ) :
$wpr->the_post();
?>
<?php $wpcover = get_field('whitepaper_cover'); ?>
<li>
<img src="<?php echo $wpcover['url']; ?>" alt="<?php echo $wpcover['alt']; ?>" />
<?php the_title(); ?>
Get it Now
</li>
<?php
endwhile;
wp_reset_postdata();
?>`
For example, on one page the_permalink() is grabbing the post ID link as p=17005 (a post that doesn't exist) when it SHOULD be p=28649, leading to a 404 error. Any ideas?
I tried resetting permalinks and updating elementor plugins, neither of which worked. Also tried various suggetions from browsing stackexchange but none really matched this exact issue. The code looks good to me, but maybe I'm missing something?

Add hreflang to magento language switcher

I am trying to add hreflang="ca-fr" or "ca-en" my code is below
<?php if ($_lang->getId() != $this->getCurrentStoreId()): ?>
<li class="language-<?php echo $this->htmlEscape($_lang->getCode()); ?>">
<a href="#" onclick="changeLang('<?php echo $_lang->getCurrentUrl() ?>')"
hreflang="<?php echo $this->htmlEscape($_lang->getCode()); ?>"
id="link-<?php echo $_lang->getId() ?>"><?php echo $frontendNameAssign[$_lang->getId()]; ?></a>
</li>
<?php endif; ?>
I added the 4th line to the file but this displays as "ca_us_english", is there a way to amend this?
I have also tried amending Miscellaneous Scripts with no joy, any help would be grateful.
thanks
Mel
Well in this case you are "right" and Magento is "wrong".
What Magento is actually presenting you as $_lang is not really the lang, but one of your store.
So ca_us_english is the code of your english candian store in your backend (you can have a look at it in the admin under System > Manage Stores)
To get the actual local set for this store you have to use this line of code :
Mage::getStoreConfig('general/locale/code', $_lang->getId())

Magento - Display Category information in Sales Orders view

EDIT: So a guy in this thread says the following:
"All order data in Magento is product-specific. It doesn't store anything about what category the products are in. So, as you have found out, there are no reports available in Magento for that sort of thing."
Is this true? Is what I am trying to do a lost cause?
I am trying to display product categories on Magento's backend Sales > Order > specific order view > Information > Items Ordered grid. This is not the main "Orders grid" that you see when navigating to Sales > Order. I want to alter the more detailed one that you see after clicking on a specific order in that first grid.
Following advice from this thread, I've made a new column and given it the proper title (Categories).
Then, to try to populate that column, I added code from Joost de Valk's solution on this thread to app/design/adminhtml/default/default/template/sales/order/view/items/renderer/default.phtml
I feel like I should be close (or not) but currently that code is just returning the word "Array". Any thoughts?
My current code is below. The column in question is the second td - the one that echoes $cats:
<?php $_item = $this->getItem() ?>
// Here is the snippet I pasted in to define variables.
<?php $product = Mage::getModel('catalog/product')->load($productId);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
echo $_cat->getName();
} ?>
<?php $this->setPriceDataObject($_item) ?>
<tr<?php if (!$this->canDisplayGiftmessage()): ?> class="border"<?php endif; ?>>
<td>
<?php if ($this->canDisplayContainer()): ?>
<div id="<?php echo $this->getHtmlId() ?>" class="item-container">
<?php endif; ?>
<div class="item-text">
<?php echo $this->getColumnHtml($_item, 'name') ?>
</div>
<?php if ($this->canDisplayContainer()): ?>
</div>
<?php endif ?>
</td>
// Here is the column in question.
<td class="a-center"><?php echo $cats ?></td>
<td class="a-center"><?php echo $_item->getStatus() ?></td>
<td class="a-right"><?php echo $this->displayPriceAttribute('original_price') ?></td>
etc, etc, etc ...
$_cat is an object
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
<td class="a-center"><?php echo $cats ?></td>
You need to know which property you want to display
echo $_cat->getXyz();
eg.
echo $_cat->getName();
To see a list of some of it properties try
print_r($_cat->getData());
So it seems that categories are really complicated and certainly beyond my abilities. I figured out a work-around, however ugly. Since I am not using the manufacturer field and have only 4 relevant categories, I am creating "manufacturers" for each of those 4 categories, going through and assigning them to all products, and then calling for manufacturer in the relevant file. The code for manufacturer is as follows:
<?php
$manufacturer = Mage::getModel('catalog/product')->load($_item['product_id'])->getAttributeText('manufacturer');
echo $manufacturer;
?>
Thanks to this post for that snippet and thanks to everyone else for their help.

Using php to add classes to looped data in WP e-commerce

In WP e-commerce, I have the following in my products page template:
<ul id="product-categories">
<?php $counter = 1; ?>
<?php wpsc_start_category_query(array('category_group'=>get_option('wpsc_default_category'), 'show_thumbnails'=> get_option('show_category_thumbnails'))); ?>
<li class="four columns<?php if ($counter % 4 == 1){echo ' alpha';}else if ($counter % 4 == 0){echo ' omega';} ?>">
<?php $counter++ ; ?>
<a title="<?php wpsc_print_category_name(); ?>" href="<?php wpsc_print_category_url();?>">
<?php wpsc_print_category_image('auto', 'auto'); ?>
<span><?php wpsc_print_category_name(); ?></span>
</a>
</li>
<p><?php echo $counter ?></p>
<?php wpsc_end_category_query(); ?>
</ul>
As you can see, for every 4n-3th item, I am trying to add an alpha class, and to every 4nth item, I am trying to add an omega class to my list items, such that the display works with a grid framework.
My issue is, start_wpsc_category_query does not appear to be 'looping' like a standard WP loop, as my echoed counter value remains at 2 for every item, hence no class is added.
Does anyone know how to fix this code to make the counter value increase each time, or have a better (PHP) way of addressing the problem if you've dealt with WP e-commerce before?
Another way of approaching would be to write a foreach loop for WP e-commerce categories, if anyone knows how to do this.
Thanks a million in advance.

Different <li> class based off of category or tag in Wordpress

My goal is to have action icons in lists that correspond to how the list item is tagged or categorized.
For example, if I have a list item that is a webinar it should have a webinar icon next to it, the next item in the list might be a white paper, which should have a white paper icon next to it. The population of the icons should be controlled by how the list item is tagged or categorized.
I don't know where to start; any suggestions are appreciated. Thanks!
EDIT:
Thought it might be helpful if I show the list I'm wanting to modify - technically, the items that I want to modify are in the span class=meta" section, but I'm open to using whatever method worls:
<ul class="sub_nav">
<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active last">
<a href="#resource_center" title="Resources II">
Featured Resources
</a>
</li>
</ul>
<div id="resource_center">
<?php
$resources = get_posts("post_type=resource&posts_per_page=2&cat=31&tag=featured");
?>
<?php foreach ($resources as $key => $resource): setup_postdata($resource); ?>
<?php if ($key === 0): ?>
<?php endif ?>
<p><span class="meta"><?php echo apply_filters('get_the_date',mysql2date('m.d.Y', $resource->post_date), 'm.d.Y') ?></span>
<?php echo $resource->post_title ?> – <?php echo strip_tags(get_the_excerpt()) ?></p>
<?php endforeach; ?>
<span class="more">Read More</span>
</div>
Just name all your icons after the tags they correspond to and put them in the same folder on your server (let's say http://www.yoursite.com/tagicons)
In your loop, just iterate the meta tag inside an image tag
<img src="http://www.yoursite.com/tagicons/{$tag}.png" />
Paste the code you're using to iterate your list items if you need more help.
Cheers
-D
EDIT:
I se you're using wordpress.
Refer to http://codex.wordpress.org/Function_Reference/wp_get_post_tags
to see how to get the tags you're looking for.
If you are generating the list inside the WordPress loop you can add the category as a class to the list element. For example:
...loop started
$categories = get_the_category();
$ids = '';
foreach ($categories as $category) {
$ids .= ' ' . $category->slug;
}
echo '<li class="' . $ids '">This item</li>';
...more loop
Then utilize CSS to style the list block.
While I think both of these solutions would have worked, I decided to go with a third solution I discovered as I researched options to meet my use case. This one was ideal because I was able to seamlessly fit it into my existing code structure and because I have a relatively low number of resources that I need to add the featured image to.
I added the code below, which basically uses the post's featured image as a left-aligned thumbnail.
<?php if ( has_post_thumbnail()): ?>
<?php
$thumb_id = get_post_thumbnail_id($resource->id);
$args = array(
'p' => $thumb_id,
'post_type' => 'attachment'
);
$thumb_image = get_posts($args);
$thumb_caption = $thumb_image->post_excerpt;
?>
<?php if (!empty($thumb_caption)): ?>
<div class="caption"><?php echo $thumb_caption ?></div>
<?php endif ?>
<?php the_post_thumbnail('sidebar-thumb'); ?>
<?php endif; ?>
Followed by this code snipped to grab the image and put it by the list item:
<?php echo get_the_post_thumbnail($id, 'thumbnail', array()); ?>
Here's a screen shot of my test site list section after I added the code - it's exactly what I was looking for:
Thanks for the suggestions and help, it got me moving in the right direction!

Categories