Custom header image for category in opencart - php

I have 4 main categories in an opencart site.
I want to have a different header image for the 4 separate categories.
How can I get the current category name and put an if statement to select the image for the header?
Current header image code:
<div id="headerWrapper">
<div id="header">
<div class="div1">
<div class="div2">
<?php if ($logo) { ?>
<img src="<?php echo $logo; ?>" title="<?php echo $store; ?>" alt="<?php echo $store; ?>" />
<?php } ?>
</div>
</div>
</div>

You will need to work out if the path is set and the route is product/category to begin with, to check if you are even on a category page. Then you will need to use that information in a switch really (rather than a large if else listing). Can you give more detail on what it is you are wanting to change in the code above? Is it the logo or are you wanting to add a class to the header that you can then use to style via your stylesheet
To find out if you are in the category, use this in the index() method in catalog/controller/common/header.php
$get = $this->request->get;
$this->data['cat_id'] = false;
$this->data['cat_name'] = '';
if(!empty($get['route']) && $get['route'] == 'product/category' && !empty($get['path'])) {
$cats = explode('_', $get['path']);
$cat_id = array_pop($cats);
$cat_id = (int) $cat_id;
if($cat_id) {
$this->load->model('catalog/category');
$result = $this->model_catalog_category->getCategory($cat_id);
$this->data['cat_id'] = $cat_id;
$this->data['cat_name'] = $result['name'];
}
}
This will give you two variables to use in your template's common/header.tpl file
$cat_id and $cat_name
$cat_id will be the current category id, or false if it's not a category page
$cat_name will have the current category name if one exists, or an empty string if it doesn't

Related

How to make my carousel active for each item?

I want to make one image active each when it is selected, but right now all items are active
<?php foreach($project_images as $image) {?>
<?php
if($image['project_image_name'] != '') {
$images = '<img src="'.base_url().'public/uploads/project_images/'.$image['project_category'].'/'.$image['project_id'].'/'.$image['project_image_name'].'" >';
} else {
$images = '<img src="'.base_url().'public/uploads/project_images/default/default-project-image.jpg'.'" ';
}
?>
<div class="carousel-item active">
<?php echo $images; ?>
</div>
<?php }?>
This sounds like a job for JavaScript, not PHP.
I'm assuming PHP doesn't know if the image is active or not, it just displays the image.
I'm assuming you want the image to be active when it's clicked. That's JavaScript.
Step 1: Don't make anything active.
Remove 'active' from the div.
<div class="carousel-item">
<?php echo $images; ?>
</div>
Step 2: Give each div a unique identifier so JavaScript knows what element you're referring to.
<?php
$id = 0;
foreach($project_images as $image) {
$id++;
?>
<?php
// .. code is the same
?>
<div id="carousel_<?php echo $id ?>" class="carousel-item">
<?php echo $images; ?>
</div>
<?php }?>
That should give each div a unique id.
Step 3: Create a JavaScript function that will toggle the 'active' class of the selected item.
Ex:
function ToggleClass(elemID){
// Get selected element
selElement = document.getElementById(elemID);
//Use selElement to change class. Code goes here
}
Step 4: Add the JavaScript function to the div.
Note the quotes -- use the double and singles as needed.
Ex:
<div id="carousel_<?php echo $id ?>" class="carousel-item active" onclick="ToggleClass('carousel_<?php echo $id ?>')">
<?php echo $images; ?>
</div>

Display Wordpress Post Category Image

I want to display placeholder image if there is no category image and no featured image for a post
Then if there is a category image display then instead
Then if there is a featured image for a post display this instead
<?php if (has_post_thumbnail()) { ?>
<img src="<?php echo the_post_thumbnail_url(); ?>" class="single-blog-post-image" alt="Post Featured Image" />
<?php } else {
$category = get_the_category();
$categoryImage = 'http://akjservices.foamydev.com/wp-content/uploads/'.$category[0]->category_nicename.'.png';
if (!has_post_thumbnail() && file_exists($categoryImage)) {
?>
<img class="single-blog-post-image" src="<?php bloginfo('url'); ?>/wp-content/uploads/<?php echo $category[0]->category_nicename ; ?>.png" alt="Category Featured Image" />
<?php } else { ?>
<img src="/wp-content/themes/akj/img/placeholder-part-image.jpg" alt="AKJ Services Default Image" class="single-blog-post-image">
<?php } }?>
It is working apart from it only gets the category image from the very first category nothing else
How can I amend my code to fix it so I can get any post category image?
Can see an example here
http://akjservices.foamydev.com/product/fr-sfj-2-7-5k/ => default placeholder image
The code specifies that the image name must be the same as the category name for example A06B-6044.png - however happy to ignore this and for it just to show the category image no matter what the name is.
You should loop the categories, You are accessing the only first one, don't forget to remove [0] after $category then.
$categories = get_the_category()
foreach( $categories as $cateogry ){
$category->category_nicename;
...
}

Creating PHP previous page button on first page in WordPress

I have this PHP code that works excellent in displaying arrows to previous/next pages of WordPress child pages:
<?php // Button Code for Prev and Next Page
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(
array('post_type' => 'page'));
$portfolio = get_page_by_title('Services');
$args = array (
'child_of' => $portfolio->ID,
'sort_order' => 'asc',
'post_type' => 'page'
);
$portfolio_children = get_pages( $args );
$pages = array();
foreach ($portfolio_children as $page) {
$pages[] += $page->ID;
}
$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="previousarrow">
<a href="<?php echo get_permalink($prevID); ?>" title="<?php echo get_the_title($prevID); ?>">
<img src="<?php bloginfo('url');?>/wp-content/uploads/2016/06/previousbutton.png">
</a>
</div>
<?php }
if (!empty($nextID)) { ?>
<div class="nextarrow">
<a href="<?php echo get_permalink($nextID); ?>" title="<?php echo get_the_title($nextID); ?>">
<img src="<?php bloginfo('url');?>/wp-content/uploads/2016/06/nextbutton.png">
</a>
</div>
<?php } ?>
</div>
Problem is that on the first and last pages, I can't get it to show the previous/next button to go to the 'last or first' page of the child pages.
Example
A (parent)
1 (child page 1)
2 (child page 2)
3 (child page 3)
I want the previous option on page 1 to go to page 3 and vice versa but I can't figure that out. Any help is appreciated.
The buttons are not appearing because of the php if parameters, which eliminates the previous arrow on page 1 and next arrow on page 3 because it recognizes them as empty. One way around this would be to remove the if clauses and use if statements to define your prevID and nextID variables, or you can simply add else statements after each if statement defines the previous/next buttons to be shown. However, both of these solutions are temporary, as you would require the number of pages in order to implement them, which is subject to change. Instead, use this code to ensure this feature will stay intact even if you add additional pages:
<?php if (empty($prevID)) { $firstpage = $pages[$current]; }
if (empty($nextID)) {$lastpage = $pages[$current]; }
This will define the value of the first and last pages, and can simply be inserted after you define prevID and nextID. Now simply add php else statements like so:
</div> <?php }
else { ?>
<div class="previousarrow>
<a href="<?php echo get_permalink($lastpage); ?>"
title="<?php echo get_the_title($lastpage); ?>">
<img src="<?php bloginfo('url');?>
/wp-content/uploads/2016/06/previousbutton.png"></a></div>
<?php }
This block of code would be inserted after your first div class, as the previous arrow should navigate to the last page when no previous page exists. Do the exact same after the second div class, replacing the lastpage variable with the firstpage one we created. Hope this helps!

How to get product category URL Key?

How can I get the product category's URL Key?
I can get the category name by $category->getName() but it does not work if I use this $category->getURLKey(),
$categories = $_product->getCategoryCollection()
->addAttributeToSelect('name');
foreach($categories as $category) {
$productCategoryName = $category->getName();
var_dump($category->getURLKey());
}
Return null
Any ideas?
I'm not entirely sure what your goal is, but you're not going to be able to get category information from $_product. Feel free to ask questions based upon my code below. But my code below will grab the active categories children and their info. However, this should be a good enough base for what you're looking to do regardless.
<!-- Use this section if you're trying to grab children categories and their info -->
<?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 Feel Free to remove the img src section to remove the image -->
<?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>
<?php endforeach; ?>
</ul>
</div>
<!-- End -->
Any questions feel free to ask! Sorry, I just stole an example from a project so the HTML may not line up for a direct copy.
foreach($categories as $category) {
// for category URL key
var_dump($category->getUrlPath());
// for category URL
var_dump($category->getUrl());
}

PHP/MySQL category div wrapping issue

I'm fairly certain I'm overlooking something obvious, but here it goes.
I'm working with two tables in MySQL, one for categories and the other for works. I've successfully joined the tables together and am referencing both with mysql_fetch_array. (I am aware this is deprecated.)
I am also able to successfully display the category name once, followed by all of the works in that category.
I run into an issue, however, with wrapping a div around all of the entries in a category. The reason I want to wrap one div around the entries is I'm using collapsibles to keep the information organized. The category name serves as trigger for the collapsible, and the works are expanded/collapsed when the user clicks the category name.
(I'm also opening each image in a FancyBox, but that's irrelevant to my question.)
Here is my code:
$sql = "SELECT *
FROM gallery
LEFT JOIN gallery_categories ON gallery.category=gallery_categories.category_id
ORDER BY category ASC";
$result = mysql_query($sql) or print ("Can't join tables.<br />" . $sql . "<br />" . mysql_error());
$category = null;
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$title = stripslashes($row['title']);
$image = stripslashes($row['image']);
$timestamp = stripslashes($row['timestamp']);
if ($category != $row['category']) {
$category = $row['category'];
?>
<div class="page_collapsible" id="<?php echo $row['category_id']; ?>"><h3><div class="title"><?php echo $row['category_name']; ?></div></h3></div>
<div class="gallery-grid">
<div class="item">
<div class="content"><a class="fancybox" href="images/works/<?php echo $image ?>" title="<?php echo $title ?>, <?php echo $timestamp ?>"><img src="images/thumbs/<?php echo $image ?>" class="thumb" /><div class="thumb-border"></div></a></div>
</div>
</div>
<?php
}
}
The page_collapsible class contains the collapsible trigger. The gallery-grid class is the wrapper for the content to be collapsed. The item class contains information about each entry in the works table.
The problem I run into is the code only iterates the first row in the works table that matches the category. I have tried removing the IF statement for the category display and instead tried GROUP BY, but ran into the same problem. If I move the item class and the </div> for the gallery-grid class outside of the IF statement, each entry in the works table is displayed successfully, but the gallery-grid class wraps each subsequent category and corresponding entries. If I move the item class but leave the </div> for the gallery-grid class where it is currently, the collapsible content is blank, and the entries are outside of the collapsible, though properly displayed under their corresponding category.
Ultimately, how do I wrap all of the entries in a given category ONCE, while displaying ALL entries in a given category?
As far as I understand your question, aren't you looking for something like this?
You need to write the start/end of the wrapper only when category changes (preserve order by category). The items are written unconditionally in every loop pass.
Note if($category != null){...} is because of not closing the first wrapper and the last </div> outside the loop is for closing the last wrapper
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$title = stripslashes($row['title']);
$image = stripslashes($row['image']);
$timestamp = stripslashes($row['timestamp']);
if ($category != $row['category']) {
if($category != null){
?>
</div>
<?php
}
$category = $row['category'];
?>
<div class="page_collapsible" id="<?php echo $row['category_id']; ?>"><h3><div class="title"><?php echo $row['category_name']; ?></div></h3></div>
<div class="gallery-grid">
<?php
}
?>
<div class="item">
<div class="content"><a class="fancybox" href="images/works/<?php echo $image ?>" title="<?php echo $title ?>, <?php echo $timestamp ?>"><img src="images/thumbs/<?php echo $image ?>" class="thumb" /><div class="thumb-border"></div></a></div>
</div>
<?php
}
?>
</div>

Categories