Joomla, how to display category name in module latest - php

I've been trying to display the category name after the contents of the 'Latest' module in Joomla.
I've made the query in phpMyAdmin and it works. But when I try to use this in the php module template page the page stops at the point the php should start.
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$db->setQuery("SELECT `title` FROM `#__categories` WHERE `id` = " .$item->catid);
$category = $db->loadResult();
echo $category;
When I replace $item->catid with a fixed number, it works like it does in phpMyAdmin. Can anyone tell me where I go wrong?
Thanks

$item is already having the category title so no need to get it through a db query. You can simply do this in your tmpl file. You can get the category using $item->category_title
<ul class="latestnews<?php echo $moduleclass_sfx; ?>">
<?php foreach ($list as $item) : ?>
<li itemscope itemtype="http://schema.org/Article">
<a href="<?php echo $item->link; ?>" itemprop="url">
<span itemprop="name">
<?php echo $item->title; ?>-
<b><?php echo $item->category_title; ?></b>
</span>
</a>
</li>
<?php endforeach; ?>
</ul>
UPDATE:
if you want to display as you asked in comments then you need to do this
<ul class="latestnews<?php echo $moduleclass_sfx; ?>">
<?php foreach ($list as $item) : ?>
<li itemscope itemtype="http://schema.org/Article">
<a href="<?php echo $item->link; ?>" itemprop="url">
<span itemprop="name">
<?php echo $item->title; ?>
</span>
</a>
</li>
<?php endforeach; ?>
<b>Click here for more news on ("<?php echo $item->category_title; ?>")</b>
</ul>

Related

Echo ACF color picker value inside foreach loop for WP tags

I have a foreach loop to display all the tags of my project. Each Tag must have a colour associated with it, and I am using ACF to create this field on the tags dashboard.
However, when I try to display the value from color picker inside my foreach loop it just doesn't work.
I just can't see what I am doing wrong.
Here is my php code:
<?php
$tagslist = get_tags();
foreach($tagslist as $tag) {
?>
<li>
<p><?php echo get_field('tag_color'); ?></p>
<a class="tag-list_item theme-<?php echo $tag->slug; ?>" data-tag="<?php echo $tag->term_id; ?>" href="<?php echo get_tag_link($tag->term_id); ?>" stye>
<?php echo $tag->name; ?>
</a>
</li>
<?php }
?>
The name of the field that I made is "tag_color".
You can use below code to get field from tags:
<?php
$tagslist = get_tags();
foreach($tagslist as $tag) {
?>
<li>
<p><?php echo get_field('tag_color', 'post_tag_'.$tag->term_id); ?></p>
<a class="tag-list_item theme-<?php echo $tag->slug; ?>" data-tag="<?php echo $tag->term_id; ?>" href="<?php echo get_tag_link($tag->term_id); ?>" stye>
<?php echo $tag->name; ?>
</a>
</li>
<?php } ?>
Below is the reference link:
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

Amateur trying to fix dropdown menu from SQL

I'm trying to help a friend with a website. I usually don't work with PHP, jquery.
The dropdown has 4 levels. The first level has 4 points. The 4 points have their own sub-levels, different for each other.
I'm trying to find them and then make them display in a dropdown, directly from the database.
And I'm stuck at the second level, with this error
Notice: Trying to get property 'subcategorie' of non-object in .....
What I've managed to do, until now:
<?php $categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategorie = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach($categorii as $categorie) {
?>
<ul>
<li>
<a <?php if(isset($_GET['categorie']) && $categorie->categorie==$_GET['categorie']){ echo "btn-success";}else{ echo "btn-info";};?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode($categorie->categorie); ?>"> <i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie ; ?> </a>
<ul class="">
<li> <?php echo $subcategorie->subcategorie; ?>
</li>
</ul>
</li>
</ul>
</a>
Hi You need to do a foreach loop for $subcategorieas you did for $categorii. Because you are getting an array and not a object as you got for $categorii. Here I set it around the li because it seems to be the best.
<?php
$categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategories = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach( $categorii as $categorie ) {
?>
<ul>
<li>
<a
<?php
#### What are these? Are they classnames? ####
if( isset( $_GET['categorie'] ) && $categorie->categorie==$_GET['categorie'] ){
echo "btn-success";
}else{
echo "btn-info";
};
?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode( $categorie->categorie ); ?>">
<i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie; ?>
</a>
<ul class="">
<?php foreach( $subcategories as $subcategorie ) { ?>
<li>
<a href="<?php echo site_url(); ?>/catalog-rural/?categorie=<?php echo urlencode( $_GET['categorie'] ); ?>&subcategorie=<?php echo $subcategorie->subcategorie; ?>" class="list-group-item <?php if($subcategorie->subcategorie==$_GET['subcategorie']){ echo "active";};?> ">
<?php echo $subcategorie->subcategorie; ?>
</a>
</li>
<?php } ?>
</ul>
</li>
</ul>
Not intended as a solution to your error " Trying to get property 'subcategorie' of non-object" ~ just to illustrate the invalid markup. If you use proper code indentation you would probably have been able to spot this!
<?php
$categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategorie = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach( $categorii as $categorie ) {
?>
<ul>
<li>
<a
<?php
#### What are these? Are they classnames? ####
if( isset( $_GET['categorie'] ) && $categorie->categorie==$_GET['categorie'] ){
echo "btn-success";
}else{
echo "btn-info";
};
?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode( $categorie->categorie ); ?>">
<i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie; ?>
</a>
<ul class="">
<!-- ### Where is the opening LI? ### -->
<a href="<?php echo site_url(); ?>/catalog-rural/?categorie=<?php echo urlencode( $_GET['categorie'] ); ?>&subcategorie=<?php echo $subcategorie->subcategorie; ?>" class="list-group-item <?php if($subcategorie->subcategorie==$_GET['subcategorie']){ echo "active";};?> ">
<?php echo $subcategorie->subcategorie; ?>
</a>
</li>
</ul>
</li>
</ul>
</a><!-- ### what is this closing? ### -->

Can't Link <a href="#"> W/ PHP code

I tried to link $child['id'] but can't do it. Link is removed on line 13. Can anyone tell me correct way to generate category link on click?
<!-- fetch parent categories -->
<?php
while ($parent = mysqli_fetch_assoc($parentquery)) : ?>
<?php
$parent_id=$parent['cat_id'];?>
<!--fetch sub-categories-->
<?php
$sql2 = "SELECT * FROM categories WHERE cat_parent = '$parent_id'";
$child_query = $db->query($sql2); // database object
?>
<div class="col-menu col-md-3">
<h6 class="title"><?php echo $parent['cat_name'] ?></h6>
<div class="content">
<ul class="menu-col">
<?php while($child = mysqli_fetch_assoc($child_query)) : ?>
Now I want to link each category on the below. Loops works fine. I am seeing category names but no link. Please help
<li> <a href='#'>
<?php echo $child['cat_name']; ?></a></li>
<?php endwhile; ?>
</ul>
</div>
</div>
<?php endwhile; ?>
You need to pass it in href
<li>
<a href='<?php echo $child['link'];?'>
<?php echo $child['cat_name']; ?>
</a>
</li>
Try this if ur href is empty then you should add
<a href='"<?php echo $child['link'];?>"'> // double quotes
you should pass the id to the href,
<a href='<?=$child['id'];?>'>

Page goes blank after adding to PHP foreach

Hello after I add some code to this PHP foreach the page goes blank and all I can see is a round black dot at the top left of screen.
My website is in Joomla3 and I am trying to customize a module. my site is http://get2gethersports.com
I have a recent post module that only shows the articles title.
that code is posted below
<?php if ($items) { ?>
<ul class="rsblog-recent-module unstyled<?php echo $params->get('moduleclass_sfx',''); ?>">
<?php foreach ($items as $item) { ?>
<li>
<a <?php echo $opener; ?> href="<?php echo JRoute::_('index.php?option=com_rsblog&view=post&id='.RSBlogHelper::sef($item->id,$item->alias).$Itemid,false); ?>">
<?php echo $item->title; ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
I would like to add an image abocve like the blog feed on http://vape-co.com
So I navigated to the component and saw the call for the image. which is posted below:
<div class="rsblog-entry-content">
<?php if ($this->item->image) { ?>
<div class="rsblog-entry-image">
<img class="rsblog-entry-thumb img-polaroid" src="<?php echo JURI::root().'components/com_rsblog/assets/images/blog/'.$this->item->image; ?>?nocache=<?php echo uniqid(''); ?>" alt="<?php echo $this->escape($this->item->title); ?>">
</div>
<?php } ?>
but whenever i add it or a snippet of it to the previous code it breaks....
Any ideas why it is breaking the page and how to fix it?
I tried adding in new li tags. Just adding the PHP part above the a link etc...
CODE UPDATE----
<?php if ($items) { ?>
<ul class="rsblog-recent-module unstyled<?php echo $params->get('moduleclass_sfx',''); ?>">
<?php foreach ($items as $item) { var_dump($item);?>
<li>
<div class="rsblog-entry-content">
<?php if ($this->item->image) { ?>
<div class="rsblog-entry-image">
<img class="rsblog-entry-thumb img-polaroid" src="<?php echo JURI::root().'components/com_rsblog/assets/images/blog/'.$this->item->image; ?>?nocache=<?php echo uniqid(''); ?>" alt="<?php echo $this->escape($this->item->title); ?>">
</div>
</div>
<?php } ?>
<a <?php echo $opener; ?> href="<?php echo JRoute::_('index.php?option=com_rsblog&view=post&id='.RSBlogHelper::sef($item->id,$item->alias).$Itemid,false); ?>">
<?php echo $item->title; ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
Try $item->image instead of $this->item->image
Correct code. Looks like it just needed some reduction.
<?php if ($items) { ?>
<ul class="rsblog-recent-module unstyled<?php echo $params -> get('moduleclass_sfx',''); ?>">
<?php foreach ($items as $item) { ?>
<li>
<?php if($item->image != '') ?>
<img src="components/com_rsblog/assets/images/blog/<?php echo $item->image;?>" alt="<?php echo $item->title. "logo";?>" width="100px"/>
<br/>
<a <?php echo $opener; ?> href="<?php echo JRoute::_('index.php?option=com_rsblog&view=post&id='.RSBlogHelper::sef($item->id,$item->alias).$Itemid,false); ?>">
<?php echo $item->title; ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>

PHP/Magento: Show, sort , and limit by products based on subcategory on category page

How do I sort products on a category page by subcategory as well as limit the number of products from each subcategory:
For example if the category was Food I would want to display the following:
Drinks Coke 12oz, Orange Juice 8oz, Milk Gallon,
Pasta, Spaghetti 1lb, Pesto 12 pc, Tortellini 1 PC.
And so on, displaying each subcategory name followed 3 products (images etc.)
I currently have a custom template that displays the subcategories but can't figure out the products,
<?php
$_category = $this->getCurrentCategory();
$collection = $_category->getCollection()
->addAttributeToSelect(
array('url_key','name','all_children','is_anchor','description','image')
)
->addAttributeToFilter('is_active', 1)
->addIdFilter($_category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite();
$helper = Mage::helper('catalog/category');
?>
<ul>
<?php foreach ($collection as $cat): ?>
<li>
<div class="level1descript">
<a href="<?php echo $helper->getCategoryUrl($cat); ?>">
<img src="<?php echo $cat->getImageUrl(); ?>" class="catlevel1image" />
<h2><?php echo $cat->getName(); ?></h2>
</a>
<p class="level1descript">
<?php
$catdesc = '';
$catdesc = strip_tags($cat->getDescription());
if (strlen($catdesc) > 300) {
$catdesc = substr($catdesc, 0, 300) . ' ...';
}
echo $catdesc;
?>
</p>
</div>
<?php
$childLevel2Category = $cat->getCollection()
->addAttributeToSelect(
array('url_key','name','all_children','is_anchor','description','image')
)
->addAttributeToFilter('is_active', 1)
->addIdFilter($cat->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite();
?>
<ul>
<?php foreach ($childLevel2Category as $catLevel2) { ?>
<li class="level2cats">
<a href="<?php echo $helper->getCategoryUrl($catLevel2); ?>">
<img src="<?php echo $catLevel2->getImageUrl(); ?>" class="catlevel2image" />
<h4><?php echo $catLevel2->getName(); ?></h4>
</a>
<p class="level2descript">
<?php
$catdesc = '';
$catdesc = strip_tags($catLevel2->getDescription());
if (strlen($catdesc) > 60) {
$catdesc = substr($catdesc, 0, 60) . ' ...';
}
echo $catdesc;
?>
</li>
<?php } ?>
</ul>
</li>
<?php endforeach;?>
</ul>
Below I documented an Idea, I think this is an idea.. Please excuse my rough throw together style wise. As I just used a project I was working on to throw this together. Any questions please ask. The setPageSize method will pull the first 3 products that display by default in the subcategories.
<!-- Finding Current Category and Finding it's children -->
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<div class="subcategories">
<p>Select a category to view products:</p>
<ul class="clearfix">
<!-- Display Each Subcategory Image and Name -->
<?php foreach ($categories as $category): ?>
<li class="grid12-3">
<a href="<?php echo $category->getUrl() ?>" class="clearfix">
<?php if($thumbFile = $category->getThumbnail()): ?>
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $thumbFile;?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
<?php endif;?>
<span><?php echo $category->getName() ?></span></a>
</li>
<!-- Load (3) Products from within each subcategory -->
<?php
$_helper = $this->helper('catalog/output');
$products = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->setPageSize(3)
->addAttributeToSelect(array('name', 'product_url', 'small_image'))
->load();
?>
<!-- Display Each product's detailed info -->
<?php foreach ($products as $product): ?>
<li>
<?php // Product Image ?>
<img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" />
<?php // Product description ?>
<?php $_productNameStripped = $this->stripTags($product->getName(), null, true); ?>
<h2 class="product-name"><?php echo $_helper->productAttribute($product, $product->getName() , 'name'); ?></h2>
</li>
<?php endforeach ; ?>
<?php endforeach; ?>
</ul>
</div>

Categories