How do I take this existing array and add submenus? - php

Existing array code:
function get_menu($menu = array(), $ulclass = '', $is_main_menu = false) {
global $menu_selected;
$output = '';
if (empty($menu)) {
return $output;
}
$output .= '<ul' . (!empty($ulclass) ? (' class="' . $ulclass . '"') : '') . '>';
foreach($menu as $item) {
if (!$is_main_menu || !isset($item['hide_in_main']) || !$item['hide_in_main']) {
$li_class = (isset($item['sub']) && !empty($item['href']) ? ('dir') : '');
if (isset($menu_selected) && !empty($menu_selected) && $menu_selected == $item['href']) {
$li_class = (!empty($li_class)) ? $li_class . ' selected' : 'selected';
}
if (isset($item['li_class']) && !empty($item['li_class'])) {
$li_class .= (!empty($li_class)) ? ' ' . $item['li_class'] : $item['li_class'];
}
$output .= '<li' . (!empty($li_class) ? ' class="' . $li_class . '"': '' ) . '>';
$output .= '<a';
if (isset($item['href']) && !empty($item['href'])) {
$output .= ' href="' . $item['href'] .'"';
}
if (isset($item['title']) && !empty($item['title'])) {
$output .= ' title="' . $item['title'] .'"';
}
if (isset($item['class']) && !empty($item['class'])) {
$output .= ' class="' . $item['class'] .'"';
}
if (isset($item['target']) && !empty($item['target'])) {
$output .= ' target="' . $item['target'] .'"';
}
$output .= '>';
if (isset($item['title']) && !empty($item['title'])) {
$output .= $item['title'];
} else if (isset($item['href']) && !empty($item['href'])) {
$output .= $item['href'];
}
$output .= '</a>';
if (isset($item['sub']) && !empty($item['sub'])) {
$output .= get_menu($item['sub'], $ulclass);
}
$output .= '</li>';
}
}
$output .= '</ul>';
return $output;
}
Existing Array:
$menu[] = array(
'title' => 'Home',
'href' => 'index.php'
);
$menu[] = array(
'title' => 'Summer Activites',
'href' => 'activities.php'
);
$menu[] = array(
'title' => 'Winter Activities',
'href' => 'wactivities.php'
);
$menu[] = array(
'title' => 'Image Gallery',
'href' => 'gallery.php'
);
I want to add submenus
ie: summer activities and winter activities would be child menu items under a parent Activities
Any help would be so appreciated
Thanks in advance.

Have you actually tried doing this or are you being lazy?
Practice makes perfect!

$menu[] = array(
'title' => 'Activities',
'sub' => array(
array(
'title' => 'Summer Activites',
'href' => 'activities.php' ),
array(
'title' => 'Winter Activities',
'href' => 'wactivities.php')
)
)
);
Looks like it should work.

Related

Search with um_activity (Ultimate member activity) post with post_meta and show all matching post_meta results

I am using Kleo-child theme. I have an active plugin Ultimate member social activity. I want to search for any keyword from um_shared_link post_meta and see the result that keyword activity what I search for. What exactly should I do for that? Here is my code:
<?php
add_action( 'init', 'update_my_custom_type', 99 );
function update_my_custom_type() {
global $wp_post_types;
if ( post_type_exists( 'um_activity' ) ) {
// exclude from search results
$wp_post_types['um_activity']->exclude_from_search = false;
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );
function enqueue_parent_styles() {
wp_enqueue_style( 'parent-style',
get_template_directory_uri().'/style.css' );
}
add_action( 'wp_ajax_kleo_ajax_search', 'kleo_ajax_search' );
add_action( 'wp_ajax_nopriv_kleo_ajax_search', 'kleo_ajax_search' );
if ( ! function_exists( 'kleo_ajax_search' ) ) {
function kleo_ajax_search() {
//if "s" input is missing exit
if ( empty( $_REQUEST['s'] ) && empty( $_REQUEST['bbp_search'] ) )
{
die();
}
if ( ! empty( $_REQUEST['bbp_search'] ) ) {
$search_string = $_REQUEST['bbp_search'];
} else {
$search_string = $_REQUEST['s'];
}
$output = '';
$context = 'any';
$defaults = array(
'numberposts' => 4,
'posts_per_page' => 20,
'post_type' => 'any',
'post_status' => array('publish','inherit'),
'post_password' => '',
'suppress_filters' => false,
'meta_query' => array(
array(
'key' => '_shared_link',
'value' => $_REQUEST['s'],
'compare' => 'LIKE'
)
)
);
if ( empty( $defaults['post_type'] ) ) {
$posts = null;
} else {
$defaults = apply_filters( 'kleo_ajax_query_args', $defaults );
$the_query = new WP_Query( $defaults );
$posts = $the_query->get_posts();
// echo "the query is".$the_query->request;
//die();
/*$the_query_meta = new WP_Query($defaults_meta);
$posts_meta = $the_query->get_posts();
$posts = array_merge($posts,$posts_meta);*/
}
$members = array();
$members['total'] = 0;
$groups = array();
$groups['total'] = 0;
$forums = false;
if ( function_exists( 'bp_is_active' ) && ( $context == "any" || in_array( "members", $context ) ) ) {
$members = bp_core_get_users( array(
'search_terms' => $search_string,
'per_page' => $defaults['numberposts'],
'populate_extras' => false,
) );
}
if ( function_exists( 'bp_is_active' ) && bp_is_active( "groups" ) && ( $context == "any" || in_array( "groups", $context ) ) ) {
$groups = groups_get_groups( array(
'search_terms' => $search_string,
'per_page' => $defaults['numberposts'],
'populate_extras' => false,
) );
}
if ( class_exists( 'bbPress' ) && ( $context == "any" || in_array( "forum", $context ) ) ) {
$forums = kleo_bbp_get_replies( $search_string );
}
//if there are no posts, groups nor members
if ( empty( $posts ) && $members['total'] == 0 && $groups['total'] == 0 && ! $forums ) {
$output = "<div class='kleo_ajax_entry ajax_not_found'>";
$output .= "<div class='ajax_search_content'>";
$output .= "<i class='icon icon-attention-circled'></i> ";
$output .= __( "Sorry, we haven't found anything based on your criteria.", 'kleo_framework' );
$output .= "<br>";
$output .= __( "Please try searching by different terms.", 'kleo_framework' );
$output .= "</div>";
$output .= "</div>";
echo $output;
die();
}
//if there are members
if ( $members['total'] != 0 ) {
$output .= '<div class="kleo-ajax-part kleo-ajax-type-members">';
$output .= '<h4><span>' . __( "Members", 'kleo_framework' ) . '</span></h4>';
foreach ( (array) $members['users'] as $member ) {
$image = '<img src="' . bp_core_fetch_avatar( array(
'item_id' => $member->ID,
'width' => 25,
'height' => 25,
'html' => false
) ) . '" class="kleo-rounded" alt="">';
if ( $update = bp_get_user_meta( $member->ID, 'bp_latest_update', true ) ) {
$latest_activity = char_trim( trim( strip_tags( bp_create_excerpt( $update['content'], 50, "..." ) ) ) );
} else {
$latest_activity = '';
}
$output .= "<div class ='kleo_ajax_entry'>";
$output .= "<div class='ajax_search_image'>$image</div>";
$output .= "<div class='ajax_search_content'>";
$output .= "<a href='" . bp_core_get_user_domain( $member->ID ) . "' class='search_title'>";
$output .= $member->display_name;
$output .= "</a>";
$output .= "<span class='search_excerpt'>";
$output .= $latest_activity;
$output .= "</span>";
$output .= "</div>";
$output .= "</div>";
}
$output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_members_directory_permalink() . "?s=" . $search_string ) . "'>" . __( 'View member results', 'kleo_framework' ) . "</a>";
$output .= "</div>";
}
//if there are groups
if ( $groups['total'] != 0 ) {
$output .= '<div class="kleo-ajax-part kleo-ajax-type-groups">';
$output .= '<h4><span>' . __( "Groups", 'kleo_framework' ) . '</span></h4>';
foreach ( (array) $groups['groups'] as $group ) {
$image = '<img src="' . bp_core_fetch_avatar( array(
'item_id' => $group->id,
'object' => 'group',
'width' => 25,
'height' => 25,
'html' => false
) ) . '" class="kleo-rounded" alt="">';
$output .= "<div class ='kleo_ajax_entry'>";
$output .= "<div class='ajax_search_image'>$image</div>";
$output .= "<div class='ajax_search_content'>";
$output .= "<a href='" . bp_get_group_permalink( $group ) . "' class='search_title'>";
$output .= $group->name;
$output .= "</a>";
$output .= "</div>";
$output .= "</div>";
}
$output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_groups_directory_permalink() . "?s=" . $search_string ) . "'>" . __( 'View group results', 'kleo_framework' ) . "</a>";
$output .= "</div>";
}
//if there are posts
if ( ! empty( $posts ) ) {
$post_type_str = array();
$post_types = array();
$post_type_obj = array();
foreach ( $posts as $post ) {
$post_types[ $post->post_type ][] = $post;
if ( empty( $post_type_obj[ $post->post_type ] ) ) {
$post_type_obj[ $post->post_type ] = get_post_type_object( $post->post_type );
}
}
foreach ( $post_types as $ptype => $post_type ) {
$output .= '<div class="kleo-ajax-part kleo-ajax-type-' . esc_attr( $post_type_obj[ $ptype ]->name ) . '">';
if ( isset( $post_type_obj[ $ptype ]->labels->name ) ) {
$output .= "<h4><span>" . $post_type_obj[ $ptype ]->labels->name . "</span></h4>";
} else {
$output .= "<hr>";
}
$count = 0;
foreach ( $post_type as $post ) {
$post_type_str[$post->post_type] = $post->post_type;
$count ++;
if ( $count > 4 ) {
continue;
}
$format = get_post_format( $post->ID );
if( $post->post_type == 'attachment') {
$img_url = wp_get_attachment_thumb_url( $post->ID );
$image = '<img src="'.aq_resize( $img_url, 44, 44, true, true, true ).'" class="kleo-rounded"/>';
} else {
if ($img_url = kleo_get_post_thumbnail_url($post->ID)) {
$image = aq_resize($img_url, 44, 44, true, true, true);
if (!$image) {
$image = $img_url;
}
$image = '<img src="' . $image . '" class="kleo-rounded">';
} else {
if ($format == 'video') {
$image = "<i class='icon icon-video'></i>";
} elseif ($format == 'image' || $format == 'gallery') {
$image = "<i class='icon icon-picture'></i>";
} else {
$image = "<i class='icon icon-link'></i>";
}
}
}
$excerpt = "";
if ( ! empty( $post->post_content ) ) {
$excerpt = $post->post_content;
$excerpt = preg_replace( "/\[(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?/s", '', $excerpt );
$excerpt = wp_strip_all_tags($excerpt); //added to remove gogole adsense code from search excerpt
$excerpt = char_trim( trim( strip_tags( $excerpt ) ), 40, "..." );
}
$link = 'activity/?wall_post='.$post->ID;
$result = $post->ID;
$classes = "format-" . $format;
$output .= "<div class ='kleo_ajax_entry $classes'>";
$output .= "<div class='ajax_search_image'>$image</div>";
$output .= "<div class='ajax_search_content'>";
$output .= "<a href='$link' class='search_title'>$result";
$output .= $excerpt;
$output .= "</a>";
$output .= "<span class='search_excerpt'>";
$output .= $excerpt;
$output .= "</span>";
$output .= "</div>";
$output .= "</div>";
}
$output .= '</div>';
}
if ( ! empty( $post_type_str ) ) {
if ( count( $post_type_str ) > 1 ) {
$search_str_posts = '&post_type[]=' . implode( ',', $post_type_str );
} else {
$search_str_posts = '&post_type=' . implode( ',', $post_type_str );
}
} else {
$search_str_posts = '';
}
$output .= "<a class='ajax_view_all' href='" . esc_url( home_url( '/' ) . '?s=' . $search_string ) . $search_str_posts . "'>" . __( 'View all results', 'kleo_framework' ) . "</a>";
}
/* Forums topics search */
if ( ! empty( $forums ) ) {
$output .= '<div class="kleo-ajax-part kleo-ajax-type-forums">';
$output .= '<h4><span>' . __( "Forums", 'kleo_framework' ) . '</span></h4>';
$i = 0;
foreach ( $forums as $fk => $forum ) {
$i ++;
if ( $i <= 4 ) {
$image = "<i class='icon icon-chat-1'></i>";
$output .= "<div class ='kleo_ajax_entry'>";
$output .= "<div class='ajax_search_image'>$image</div>";
$output .= "<div class='ajax_search_content'>";
$output .= "<a href='" . $forum['url'] . "' class='search_title'>";
$output .= $forum['name'];
$output .= "</a>";
//$output .= "<span class='search_excerpt'>";
//$output .= $latest_activity;
//$output .= "</span>";
$output .= "</div>";
$output .= "</div>";
}
}
$output .= "<a class='ajax_view_all' href='" . esc_url( bbp_get_search_url() . "?bbp_search=" . $search_string ) . "'>" . __( 'View forum results', 'kleo_framework' ) . "</a>";
$output .= "</div>";
}
echo $output;
die();
}
}
function searchfilter($query) {
if ($query->is_search) {
$query->set('post_type',array('um_activity'));
}
return $query;
}
add_filter('pre_get_posts','searchfilter');

How to set first <li> link as active in a php menu

Hi I am working with a Wordpress gallery plugin.
I would like the first link in the filter menu to automatically be active when the page loads. This is as by default it loads ALL the images from all the categories and it's too many! Thanks.
<?php
wp_enqueue_script('imagesloaded', plugins_url('/assets/plugins/imagesloaded/imagesloaded.pkgd.min.js', __FILE__), array('jquery'), GO_GALLERY_VERSION, true);
wp_enqueue_script('isotope', plugins_url('/assets/plugins/isotope/isotope.pkgd.min.js', __FILE__), array('jquery'), GO_GALLERY_VERSION, true);
wp_enqueue_script('go-gallery', plugins_url('/assets/js/gallery.js', __FILE__), array('jquery', 'isotope', 'imagesloaded'), GO_GALLERY_VERSION, true);
wp_enqueue_style('go-gallery', plugins_url('/assets/css/gallery.css', __FILE__), null, GO_GALLERY_VERSION);
wp_enqueue_script('tos', plugins_url('/assets/plugins/tos/js/jquery.tosrus.min.custom.js', __FILE__), array('jquery'), GO_GALLERY_VERSION, true);
wp_enqueue_style('tos', plugins_url('/assets/plugins/tos/css/jquery.tosrus.custom.css', __FILE__), null, GO_GALLERY_VERSION);
$labels = array(
'name' => _x('Media Categories', 'taxonomy general name', 'go_gallery'),
'singular_name' => _x('Media Category', 'taxonomy singular name', 'go_gallery'),
'search_items' => __('Search Media Categories', 'go_gallery'),
'all_items' => __('All Media Categories', 'go_gallery'),
'parent_item' => __('Parent Media Category', 'go_gallery'),
'parent_item_colon' => __('Parent Media Category:', 'go_gallery'),
'edit_item' => __('Edit Media Category', 'go_gallery'),
'update_item' => __('Update Media Category', 'go_gallery'),
'add_new_item' => __('Add New Media Category', 'go_gallery'),
'new_item_name' => __('New Media Category Name', 'go_gallery'),
'menu_name' => __('Media Categories', 'go_gallery'),
);
$args = array(
'hierarchical' => TRUE,
'labels' => $labels,
'show_ui' => TRUE,
'show_admin_column' => TRUE,
'query_var' => TRUE,
'rewrite' => TRUE,
);
register_taxonomy('attachment_category', 'attachment', $args );
$output = '';
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => $atts['limit'],
'order' => 'DESC',
'orderby' => $atts['sort'],
'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png'
);
$categories = array();
$atts['icat'] = array_map('sanitize_title', explode(',', $atts['icat']));
foreach ( $atts['icat'] as $category ) {
if ( $term = get_term_by('slug', $category, 'attachment_category') ) {
$categories[$term->term_id] = $term;
}
}
if ( !empty($categories) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'attachment_category',
'field' => 'term_id',
'terms' => array_keys($categories)
)
);
}
$atts['menu_gap'] = min($atts['menu_gap'], 100);
$classes[] = 'go-gallery';
$classes[] = 'menu-' . $atts['menu_pos'];
$classes[] = go_gallery_bool($atts['menu_show']) ? 'menu-show' : '';
$classes[] = 'size-' . $atts['size'];
$classes[] = 'style-' . $atts['style'];
$attributes = array();
$attributes['class'] = join(' ', $classes);
$attributes['id'] = 'go-' . substr(md5(mt_rand(0, PHP_INT_MAX)), 0, 6);
$attributes['data-gap'] = intval($atts['gap']);
$attributes['data-border-color'] = $atts['border_color'];
$attributes['data-lightbox'] = go_gallery_bool($atts['lightbox']) ? 'yes' : 'no';
$attributes['data-desc-color'] = $atts['desc_color'];
$attributes['data-menu-color'] = $atts['menu_color'];
$attributes['data-menu-bg'] = $atts['menu_bg'];
$attributes['data-menu-bg-hover'] = $atts['menu_bg_hover'];
$attributes['data-menu-gap'] = $atts['menu_gap'];
$attributes['data-bg'] = $atts['bg'];
$attributes['data-border-size'] = $atts['border_size'];
$attributes['data-overlay-color'] = go_gallery_hex2rgb($atts['overlay_color']);
$thumb_size = 'medium';
if ( $atts['size'] == 'large' || ($atts['style'] == 'squared' && in_array($atts['size'], array('medium', 'large'))) ) {
$thumb_size = 'large';
}
foreach ( $attributes as $attribute => $value ) {
$attributes[$attribute] = $attribute . '="' . $value . '"';
}
$query = new WP_Query($args);
$output .= '<div ' . join(' ', $attributes) . '>';
$output .= '<ul class="go-gallery-filters">';
$output .= '<li>';
$output .= '<a data-filter="" href="#">' . __($atts['menu_button'], 'go_gallery') . '</a>';
$output .= '</li>';
foreach ( $categories as $category ) {
if ( !empty($category) ) {
$output .= '<li class="active">';
$output .= '<a data-filter="' . $category->slug . '" href="#">' . $category->name . '</a>';
$output .= '</li>';
}
}
$output .= '</ul>';
$output .= '<div class="go-gallery-list-wrapper">';
$output .= '<ul class="go-gallery-list">';
foreach ( $query->posts as $post ) {
$category_terms = wp_get_post_terms($post->ID, 'attachment_category');
$classes = array();
$classes[] = 'go-gallery-item';
foreach ( $category_terms as $category_term ) {
$classes[] = 'category-' . $category_term->slug;
}
$image_source = wp_get_attachment_image_src($post->ID, 'full');
$output .= '<li data-source="' . $image_source[0] . '" class="' . join(' ', $classes) . '">';
$output .= '<a class="image-wrap" href="' . $image_source[0] . '">';
$output .= '<figure>';
$output .= wp_get_attachment_image($post->ID, $thumb_size);
$output .= '<div class="image-overlay">';
if ( go_gallery_bool( $atts['hover_data'] ) ){
$output .= '<h3>' . $post->post_title . '</h3>';
$output .= '<h4>' . $post->post_content . '</h4>';
}
$output .= '</div>';
$output .= '</figure>';
$output .= '</a>';
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
$output .= '</div>';
return $output;
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = array($r, $g, $b);
return 'rgba(' . join(', ', $rgb) . ', ' . $alpha .')';
That depends what you mean with "active".
:active is a CSS pseudo selector that can change the appearance of a link while it is being activated. If you want to add a css-class "active" to the first li-element in your loop, you could try something like the following:
foreach($categories as $category) {
$counter = 0; // COUNTER OF CATEGORIES...
$selected = ''; // THE STRING TO PRINT IN SELECTED LI-ELEMENT
if ( !empty($category) ) {
$selected = ($counter == 0) ? 'class="active"' : ''; // SET CLASS ACTIVE FOR THE FIRST CATEGORY
$output .= '<li '. $selected .'>';
$output .= '<a data-filter="' . $category->slug . '" href="#">' . $category->name . '</a>';
$output .= '</li>';
$counter++; //INCREMENT COUNTER OF CATEGORIES
}
}
Assuming your categories get sent via a simple link like so:
first category
second category
then you would have something like this in php:
if(isset($_GET['category']) {
$category_id = $_GET['category'];
} else {
$category_id = 1; // default
}
// this is a blank guess, we don't know where you get your data from:
$category = $categories[$category_id];
The rest depends on your data-structure.
But this is how you could set a default category.

opencart to show category for each product in search results page

I am using Opencart 2.2.0 and I am trying to show category for each product on search page. So far, I have my controller like this:
<?php
class ControllerProductSearch extends Controller {
public function index() {
$this->load->language('product/search');
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$this->load->model('tool/image');
if (isset($this->request->get['search'])) {
$search = $this->request->get['search'];
} else {
$search = '';
}
if (isset($this->request->get['tag'])) {
$tag = $this->request->get['tag'];
} elseif (isset($this->request->get['search'])) {
$tag = $this->request->get['search'];
} else {
$tag = '';
}
if (isset($this->request->get['description'])) {
$description = $this->request->get['description'];
} else {
$description = '';
}
if (isset($this->request->get['category_id'])) {
$category_id = $this->request->get['category_id'];
} else {
$category_id = 0;
}
if (isset($this->request->get['sub_category'])) {
$sub_category = $this->request->get['sub_category'];
} else {
$sub_category = '';
}
if (isset($this->request->get['sort'])) {
$sort = $this->request->get['sort'];
} else {
$sort = 'p.sort_order';
}
if (isset($this->request->get['order'])) {
$order = $this->request->get['order'];
} else {
$order = 'ASC';
}
if (isset($this->request->get['page'])) {
$page = $this->request->get['page'];
} else {
$page = 1;
}
if (isset($this->request->get['limit'])) {
$limit = (int)$this->request->get['limit'];
} else {
$limit = $this->config->get($this->config->get('config_theme') . '_product_limit');
}
if (isset($this->request->get['search'])) {
$this->document->setTitle($this->language->get('heading_title') . ' - ' . $this->request->get['search']);
} elseif (isset($this->request->get['tag'])) {
$this->document->setTitle($this->language->get('heading_title') . ' - ' . $this->language->get('heading_tag') . $this->request->get['tag']);
} else {
$this->document->setTitle($this->language->get('heading_title'));
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home')
);
$url = '';
if (isset($this->request->get['search'])) {
$url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['tag'])) {
$url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['description'])) {
$url .= '&description=' . $this->request->get['description'];
}
if (isset($this->request->get['category_id'])) {
$url .= '&category_id=' . $this->request->get['category_id'];
}
if (isset($this->request->get['sub_category'])) {
$url .= '&sub_category=' . $this->request->get['sub_category'];
}
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
if (isset($this->request->get['limit'])) {
$url .= '&limit=' . $this->request->get['limit'];
}
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('product/search', $url)
);
if (isset($this->request->get['search'])) {
$data['heading_title'] = $this->language->get('heading_title') . ' - ' . $this->request->get['search'];
} else {
$data['heading_title'] = $this->language->get('heading_title');
}
$data['customer_group_id'] = $this->customer->getGroupId();
$data['logged'] = $this->customer->isLogged();
$data['text_empty'] = $this->language->get('text_empty');
$data['text_search'] = $this->language->get('text_search');
$data['text_keyword'] = $this->language->get('text_keyword');
$data['text_category'] = $this->language->get('text_category');
$data['text_sub_category'] = $this->language->get('text_sub_category');
$data['text_quantity'] = $this->language->get('text_quantity');
$data['text_manufacturer'] = $this->language->get('text_manufacturer');
$data['text_model'] = $this->language->get('text_model');
$data['text_price'] = $this->language->get('text_price');
$data['text_wholesale'] = $this->language->get('text_wholesale');
$data['text_tax'] = $this->language->get('text_tax');
$data['text_points'] = $this->language->get('text_points');
$data['text_compare'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
$data['text_sort'] = $this->language->get('text_sort');
$data['text_limit'] = $this->language->get('text_limit');
$data['entry_search'] = $this->language->get('entry_search');
$data['entry_description'] = $this->language->get('entry_description');
$data['button_search'] = $this->language->get('button_search');
$data['button_cart'] = $this->language->get('button_cart');
$data['button_wishlist'] = $this->language->get('button_wishlist');
$data['button_compare'] = $this->language->get('button_compare');
$data['button_list'] = $this->language->get('button_list');
$data['button_grid'] = $this->language->get('button_grid');
$data['compare'] = $this->url->link('product/compare');
$this->load->model('catalog/category');
$cart_product_detail=$this->cart->getProducts();
//print_r($cart_product_detail);
$data['cart_product_info']=array();
foreach($cart_product_detail as $cart_info){
//print_r($cart_info);
$data['cart_product_info'][] = array(
'cart_product_id' => $cart_info['product_id'],
'cart_product_qty' => $cart_info['quantity']
);
}
// 3 Level Category Search
$data['categories'] = array();
$categories_1 = $this->model_catalog_category->getCategories(0);
foreach ($categories_1 as $category_1) {
$level_2_data = array();
$categories_2 = $this->model_catalog_category->getCategories($category_1['category_id']);
foreach ($categories_2 as $category_2) {
$level_3_data = array();
$categories_3 = $this->model_catalog_category->getCategories($category_2['category_id']);
foreach ($categories_3 as $category_3) {
$level_3_data[] = array(
'category_id' => $category_3['category_id'],
'name' => $category_3['name'],
);
}
$level_2_data[] = array(
'category_id' => $category_2['category_id'],
'name' => $category_2['name'],
'children' => $level_3_data
);
}
$data['categories'][] = array(
'category_id' => $category_1['category_id'],
'name' => $category_1['name'],
'children' => $level_2_data
);
}
$data['products'] = array();
if (isset($this->request->get['search']) || isset($this->request->get['tag'])) {
$filter_data = array(
'filter_name' => $search,
'filter_tag' => $tag,
'filter_description' => $description,
'filter_category_id' => $category_id,
'filter_sub_category' => $sub_category,
'sort' => $sort,
'order' => $order,
'start' => ($page - 1) * $limit,
'limit' => $limit
);
$product_total = $this->model_catalog_product->getTotalProducts($filter_data);
$results = $this->model_catalog_product->getProducts($filter_data);
foreach ($results as $result) {
if ($result['image']) {
$image = $this->model_tool_image->resize($result['image'], $this->config->get($this->config->get('config_theme') . '_image_product_width'), $this->config->get($this->config->get('config_theme') . '_image_product_height'));
} else {
$image = $this->model_tool_image->resize('placeholder.png', $this->config->get($this->config->get('config_theme') . '_image_product_width'), $this->config->get($this->config->get('config_theme') . '_image_product_height'));
}
if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
$wholesale = $this->currency->format($this->tax->calculate($result['wholesale'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
} else {
$price = false;
$wholesale = false;
}
if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
} else {
$special = false;
}
if ($this->config->get('config_tax')) {
$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']);
} else {
$tax = false;
}
if ($this->config->get('config_review_status')) {
$rating = (int)$result['rating'];
} else {
$rating = false;
}
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..',
'price' => $price,
'wholesale' => $wholesale,
'special' => $special,
'tax' => $tax,
'upc' => $result['upc'],
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $result['rating'],
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'] . $url)
);
}
$url = '';
if (isset($this->request->get['search'])) {
$url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['tag'])) {
$url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['description'])) {
$url .= '&description=' . $this->request->get['description'];
}
if (isset($this->request->get['category_id'])) {
$url .= '&category_id=' . $this->request->get['category_id'];
}
if (isset($this->request->get['sub_category'])) {
$url .= '&sub_category=' . $this->request->get['sub_category'];
}
if (isset($this->request->get['limit'])) {
$url .= '&limit=' . $this->request->get['limit'];
}
$data['sorts'] = array();
$data['sorts'][] = array(
'text' => $this->language->get('text_default'),
'value' => 'p.sort_order-ASC',
'href' => $this->url->link('product/search', 'sort=p.sort_order&order=ASC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_name_asc'),
'value' => 'pd.name-ASC',
'href' => $this->url->link('product/search', 'sort=pd.name&order=ASC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_name_desc'),
'value' => 'pd.name-DESC',
'href' => $this->url->link('product/search', 'sort=pd.name&order=DESC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_price_asc'),
'value' => 'p.price-ASC',
'href' => $this->url->link('product/search', 'sort=p.price&order=ASC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_price_desc'),
'value' => 'p.price-DESC',
'href' => $this->url->link('product/search', 'sort=p.price&order=DESC' . $url)
);
if ($this->config->get('config_review_status')) {
$data['sorts'][] = array(
'text' => $this->language->get('text_rating_desc'),
'value' => 'rating-DESC',
'href' => $this->url->link('product/search', 'sort=rating&order=DESC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_rating_asc'),
'value' => 'rating-ASC',
'href' => $this->url->link('product/search', 'sort=rating&order=ASC' . $url)
);
}
$data['sorts'][] = array(
'text' => $this->language->get('text_model_asc'),
'value' => 'p.model-ASC',
'href' => $this->url->link('product/search', 'sort=p.model&order=ASC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_model_desc'),
'value' => 'p.model-DESC',
'href' => $this->url->link('product/search', 'sort=p.model&order=DESC' . $url)
);
$url = '';
if (isset($this->request->get['search'])) {
$url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['tag'])) {
$url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['description'])) {
$url .= '&description=' . $this->request->get['description'];
}
if (isset($this->request->get['category_id'])) {
$url .= '&category_id=' . $this->request->get['category_id'];
}
if (isset($this->request->get['sub_category'])) {
$url .= '&sub_category=' . $this->request->get['sub_category'];
}
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
$data['limits'] = array();
$limits = array_unique(array($this->config->get($this->config->get('config_theme') . '_product_limit'), 25, 50, 75, 100));
sort($limits);
foreach($limits as $value) {
$data['limits'][] = array(
'text' => $value,
'value' => $value,
'href' => $this->url->link('product/search', $url . '&limit=' . $value)
);
}
$url = '';
if (isset($this->request->get['search'])) {
$url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['tag'])) {
$url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['description'])) {
$url .= '&description=' . $this->request->get['description'];
}
if (isset($this->request->get['category_id'])) {
$url .= '&category_id=' . $this->request->get['category_id'];
}
if (isset($this->request->get['sub_category'])) {
$url .= '&sub_category=' . $this->request->get['sub_category'];
}
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['limit'])) {
$url .= '&limit=' . $this->request->get['limit'];
}
$pagination = new Pagination();
$pagination->total = $product_total;
$pagination->page = $page;
$pagination->limit = $limit;
$pagination->url = $this->url->link('product/search', $url . '&page={page}');
$data['pagination'] = $pagination->render();
$data['results'] = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($product_total - $limit)) ? $product_total : ((($page - 1) * $limit) + $limit), $product_total, ceil($product_total / $limit));
// http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html
if ($page == 1) {
$this->document->addLink($this->url->link('product/search', '', true), 'canonical');
} elseif ($page == 2) {
$this->document->addLink($this->url->link('product/search', '', true), 'prev');
} else {
$this->document->addLink($this->url->link('product/search', $url . '&page='. ($page - 1), true), 'prev');
}
if ($limit && ceil($product_total / $limit) > $page) {
$this->document->addLink($this->url->link('product/search', $url . '&page='. ($page + 1), true), 'next');
}
}
$data['search'] = $search;
$data['description'] = $description;
$data['category_id'] = $category_id;
$data['sub_category'] = $sub_category;
$data['sort'] = $sort;
$data['order'] = $order;
$data['limit'] = $limit;
$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
$data['content_top'] = $this->load->controller('common/content_top');
$data['content_bottom'] = $this->load->controller('common/content_bottom');
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header');
$this->response->setOutput($this->load->view('product/search', $data));
}
}
But I do not know how to show the categories for each product. Any suggestions? Thank you.
In your controller file, find: $data['products'][] = array(
before it add:
$product_categories = array();
// get categories of each product, based on product id
$categories = $this->model_catalog_product->getCategories($result['product_id']);
if($categories){
// select each category name and create a link to it
foreach($categories as $category){
$category_info = $this->model_catalog_category->getCategory($category['category_id']);
$product_categories[] = array(
'name' => $category_info['name'],
'href' => $this->url->link('product/category', 'path=' . $category_info['category_id'] )
);
}
}
after it add:
// store categories information
'product_categories' => $product_categories,
Your code must be like this:
$product_categories = array();
// get categories of each product, based on product id
$categories = $this->model_catalog_product->getCategories($result['product_id']);
if($categories){
// select each category name and create a link to it
foreach($categories as $category){
$category_info = $this->model_catalog_category->getCategory($category['category_id']);
$product_categories[] = array(
'name' => $category_info['name'],
'href' => $this->url->link('product/category', 'path=' . $category_info['category_id'] )
);
}
}
$data['products'][] = array(
// store categories information
'product_categories' => $product_categories,
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $result['rating'],
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'] . $url)
);
Now open your template file:
catalog\view\theme\default\template\product\search.tpl
find:
<?php if ($product['rating']) { ?>
add before it:
<?php if($product['product_categories']){ ?>
<div class="product-categories">
<?php foreach($product['product_categories'] as $category){ ?>
<?php echo $category['name']; ?>
<?php } ?>
</div>
<?php } ?>

How to get category name in opencart product search result page

My Controller :
<?php
class ControllerProductSearch extends Controller {
public function index() {
$this->load->language('product/search');
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$this->load->model('tool/image');
if (isset($this->request->get['search'])) {
$search = $this->request->get['search'];
} else {
$search = '';
}
if (isset($this->request->get['tag'])) {
$tag = $this->request->get['tag'];
} elseif (isset($this->request->get['search'])) {
$tag = $this->request->get['search'];
} else {
$tag = '';
}
if (isset($this->request->get['description'])) {
$description = $this->request->get['description'];
} else {
$description = '';
}
if (isset($this->request->get['category_id'])) {
$category_id = $this->request->get['category_id'];
} else {
$category_id = 0;
}
if (isset($this->request->get['sub_category'])) {
$sub_category = $this->request->get['sub_category'];
} else {
$sub_category = '';
}
if (isset($this->request->get['sort'])) {
$sort = $this->request->get['sort'];
} else {
$sort = 'p.sort_order';
}
if (isset($this->request->get['order'])) {
$order = $this->request->get['order'];
} else {
$order = 'ASC';
}
if (isset($this->request->get['page'])) {
$page = $this->request->get['page'];
} else {
$page = 1;
}
if (isset($this->request->get['limit'])) {
$limit = $this->request->get['limit'];
} else {
$limit = $this->config->get('config_product_limit');
}
if (isset($this->request->get['search'])) {
$this->document->setTitle($this->language->get('heading_title') . ' - ' . $this->request->get['search']);
} elseif (isset($this->request->get['tag'])) {
$this->document->setTitle($this->language->get('heading_title') . ' - ' . $this->language->get('heading_tag') . $this->request->get['tag']);
} else {
$this->document->setTitle($this->language->get('heading_title'));
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home')
);
$url = '';
if (isset($this->request->get['search'])) {
$url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
}
// if (isset($this->request->get['name'])) {
// $url .= '&search=' . urlencode(html_entity_decode($this->request->get['name'], ENT_QUOTES, 'UTF-8'));
// }
if (isset($this->request->get['tag'])) {
$url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['description'])) {
$url .= '&description=' . $this->request->get['description'];
}
if (isset($this->request->get['category_id'])) {
$url .= '&category_id=' . $this->request->get['category_id'];
}
if (isset($this->request->get['sub_category'])) {
$url .= '&sub_category=' . $this->request->get['sub_category'];
}
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
if (isset($this->request->get['limit'])) {
$url .= '&limit=' . $this->request->get['limit'];
}
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('product/search', $url)
);
if (isset($this->request->get['search'])) {
$data['heading_title'] = $this->language->get('heading_title') . ' - ' . $this->request->get['search'];
} else {
$data['heading_title'] = $this->language->get('heading_title');
}
$data['text_empty'] = $this->language->get('text_empty');
$data['text_search'] = $this->language->get('text_search');
$data['text_keyword'] = $this->language->get('text_keyword');
$data['text_category'] = $this->language->get('text_category');
$data['text_sub_category'] = $this->language->get('text_sub_category');
$data['text_quantity'] = $this->language->get('text_quantity');
$data['text_manufacturer'] = $this->language->get('text_manufacturer');
$data['text_model'] = $this->language->get('text_model');
$data['text_price'] = $this->language->get('text_price');
$data['text_tax'] = $this->language->get('text_tax');
$data['text_points'] = $this->language->get('text_points');
$data['text_compare'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
$data['text_sort'] = $this->language->get('text_sort');
$data['text_tags'] = $this->language->get('text_tags');
$data['text_limit'] = $this->language->get('text_limit');
$data['entry_search'] = $this->language->get('entry_search');
$data['entry_description'] = $this->language->get('entry_description');
$data['button_search'] = $this->language->get('button_search');
$data['button_cart'] = 'CUSTOMIZE';
$data['button_wishlist'] = $this->language->get('button_wishlist');
$data['button_compare'] = $this->language->get('button_compare');
$data['button_list'] = $this->language->get('button_list');
$data['button_grid'] = $this->language->get('button_grid');
$data['compare'] = $this->url->link('product/compare');
**$this->load->model('catalog/category');
// 3 Level Category Search
$data['categories'] = array();
$categories_1 = $this->model_catalog_category->getCategories(0);
foreach ($categories_1 as $category_1) {
$level_2_data = array();
$categories_2 = $this->model_catalog_category->getCategories($category_1['category_id']);
foreach ($categories_2 as $category_2) {
$level_3_data = array();
$categories_3 = $this->model_catalog_category->getCategories($category_2['category_id']);
foreach ($categories_3 as $category_3) {
$level_3_data[] = array(
'category_id' => $category_3['category_id'],
'name' => $category_3['name'],
);
}
$level_2_data[] = array(
'category_id' => $category_2['category_id'],
'name' => $category_2['name'],
'children' => $level_3_data
);
}
$data['categories'][] = array(
'category_id' => $category_1['category_id'],
'name' => $category_1['name'],
'children' => $level_2_data
);
}**
// $this->load->model('catalog/category');
// $data['categories'] = array();
// $product_category = $this->model_catalog_product->getCategories($product_id);
// foreach ($product_category as $prodcat) {
// $category_info = $this->model_catalog_category->getCategory($prodcat['category_id']);
// if ($category_info) {
// $this->data['categories'][] = array(
// 'name' => $category_info['name'],
// 'href' => $this->url->link('product/category', 'path=' . $category_info['category_id'])
// );
// }
// }
$category_info = $this->model_catalog_category->getCategory($category_id);
if ($category_info) {
$this->document->setTitle($category_info['meta_title']);
$this->document->setDescription($category_info['meta_description']);
$this->document->setKeywords($category_info['meta_keyword']);
$this->document->addLink($this->url->link('product/category', 'path=' . $this->request->get['path']), 'canonical');
$data['heading_title'] = $category_info['name'];
}
$data['products'] = array();
if (isset($this->request->get['search']) || isset($this->request->get['tag'])) {
$filter_data = array(
'filter_name' => $search,
'filter_tag' => $tag,
'filter_description' => $description,
'filter_category_id' => $category_id,
'filter_sub_category' => $sub_category,
//'name' => $category_2['name'],
'sort' => $sort,
'order' => $order,
'start' => ($page - 1) * $limit,
'limit' => $limit
);
$product_total = $this->model_catalog_product->getTotalProducts($filter_data);
$results = $this->model_catalog_product->getProducts($filter_data);
foreach ($results as $result) {
if ($result['image']) {
$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
} else {
$image = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
}
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$price = false;
}
if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$special = false;
}
if ($this->config->get('config_tax')) {
$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
} else {
$tax = false;
}
if ($this->config->get('config_review_status')) {
$rating = (int)$result['rating'];
} else {
$rating = false;
}
$cats = $this->model_catalog_product->getCategories($product_info['product_id']);
$sep = '';
$catnames = '';
foreach ($cats as $cat){
$catarr = $this->model_catalog_category->getCategory($cat['category_id']);
$catnames .= $sep.$catarr['name'];
$sep = ', ';
}
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
//'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
'description' => html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'),
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $result['rating'],
'category' => $catnames,
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'] . $url)
);
// $data['products'][] = array(
// 'product_id' => $result['product_id'],
// 'thumb' => $image,
// 'name' => $result['name'],
// //'category' => $this->model_catalog_category->getCategoryName($result['product_id']),
// //'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
// 'description' => html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'),
// 'price' => $price,
// 'special' => $special,
// 'tax' => $tax,
// 'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
// 'rating' => $result['rating'],
// 'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'] . $url)
// );
}
$data['tags'] = array();
if ($product_info['tag']) {
$tags = explode(',', $product_info['tag']);
foreach ($tags as $tag) {
$data['tags'][] = array(
'tag' => trim($tag),
'href' => $this->url->link('product/search', 'tag=' . trim($tag))
);
}
}
$url = '';
if (isset($this->request->get['search'])) {
$url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['tag'])) {
$url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['filter_tag'])) {
$filter_tag = $this->request->get['filter_tag'];
}
elseif (isset($this->request->get['filter_name'])) {
$filter_tag = $this->request->get['filter_name'];
} else { $filter_tag = '';
}
if (isset($this->request->get['description'])) {
$url .= '&description=' . $this->request->get['description'];
}
if (isset($this->request->get['category_id'])) {
$url .= '&category_id=' . $this->request->get['category_id'];
}
if (isset($this->request->get['sub_category'])) {
$url .= '&sub_category=' . $this->request->get['sub_category'];
}
if (isset($this->request->get['limit'])) {
$url .= '&limit=' . $this->request->get['limit'];
}
$data['sorts'] = array();
$data['sorts'][] = array(
'text' => $this->language->get('text_default'),
'value' => 'p.sort_order-ASC',
'href' => $this->url->link('product/search', 'sort=p.sort_order&order=ASC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_name_asc'),
'value' => 'pd.name-ASC',
'href' => $this->url->link('product/search', 'sort=pd.name&order=ASC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_name_desc'),
'value' => 'pd.name-DESC',
'href' => $this->url->link('product/search', 'sort=pd.name&order=DESC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_price_asc'),
'value' => 'p.price-ASC',
'href' => $this->url->link('product/search', 'sort=p.price&order=ASC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_price_desc'),
'value' => 'p.price-DESC',
'href' => $this->url->link('product/search', 'sort=p.price&order=DESC' . $url)
);
if ($this->config->get('config_review_status')) {
$data['sorts'][] = array(
'text' => $this->language->get('text_rating_desc'),
'value' => 'rating-DESC',
'href' => $this->url->link('product/search', 'sort=rating&order=DESC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_rating_asc'),
'value' => 'rating-ASC',
'href' => $this->url->link('product/search', 'sort=rating&order=ASC' . $url)
);
}
$data['sorts'][] = array(
'text' => $this->language->get('text_model_asc'),
'value' => 'p.model-ASC',
'href' => $this->url->link('product/search', 'sort=p.model&order=ASC' . $url)
);
$data['sorts'][] = array(
'text' => $this->language->get('text_model_desc'),
'value' => 'p.model-DESC',
'href' => $this->url->link('product/search', 'sort=p.model&order=DESC' . $url)
);
$url = '';
if (isset($this->request->get['search'])) {
$url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['tag'])) {
$url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['description'])) {
$url .= '&description=' . $this->request->get['description'];
}
if (isset($this->request->get['category_id'])) {
$url .= '&category_id=' . $this->request->get['category_id'];
}
if (isset($this->request->get['sub_category'])) {
$url .= '&sub_category=' . $this->request->get['sub_category'];
}
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
$data['limits'] = array();
$limits = array_unique(array($this->config->get('config_product_limit'), 25, 50, 75, 100));
sort($limits);
foreach($limits as $value) {
$data['limits'][] = array(
'text' => $value,
'value' => $value,
'href' => $this->url->link('product/search', $url . '&limit=' . $value)
);
}
$url = '';
if (isset($this->request->get['search'])) {
$url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['tag'])) {
$url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['description'])) {
$url .= '&description=' . $this->request->get['description'];
}
if (isset($this->request->get['category_id'])) {
$url .= '&category_id=' . $this->request->get['category_id'];
}
if (isset($this->request->get['sub_category'])) {
$url .= '&sub_category=' . $this->request->get['sub_category'];
}
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['limit'])) {
$url .= '&limit=' . $this->request->get['limit'];
}
$pagination = new Pagination();
$pagination->total = $product_total;
$pagination->page = $page;
$pagination->limit = $limit;
$pagination->url = $this->url->link('product/search', $url . '&page={page}');
$data['pagination'] = $pagination->render();
$data['results'] = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($product_total - $limit)) ? $product_total : ((($page - 1) * $limit) + $limit), $product_total, ceil($product_total / $limit));
}
$data['search'] = $search;
$data['description'] = $description;
$data['category_id'] = $category_id;
$data['sub_category'] = $sub_category;
$data['sort'] = $sort;
$data['order'] = $order;
$data['limit'] = $limit;
$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
$data['content_top'] = $this->load->controller('common/content_top');
$data['content_bottom'] = $this->load->controller('common/content_bottom');
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header');
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/search.tpl')) {
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/product/search.tpl', $data));
} else {
$this->response->setOutput($this->load->view('default/template/product/search.tpl', $data));
}
}
}
My Tempalate file :
<!-- Products -->
<?php foreach ($products as $product) { ?>
<?php $prod_anim=$config->get('glade_product_animation'); ?>
<?php if (!isset($product['thumb_swap']) && $prod_anim!='overlay' || !isset($prod_anim)) { $prod_anim='static'; } ?>
<div class="product-layout product-grid col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="product-thumb">
<?php if ($product['price'] && $product['special']) { ?>
<span class="onsale"><?php echo $language->get('text_onsale'); ?></span>
<?php } ?>
<div class="image">
<div class="thumb">
<?php if ( $product['thumb'] && isset($product['thumb_swap']) && $prod_anim!='overlay' && $prod_anim!='static') { ?>
<div class="front-image">
<img src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" class="img-responsive" alt="<?php echo $product['name']; ?>"/>
</div>
<div class="back-image">
<img src="<?php echo $product['thumb_swap']; ?>" title="<?php echo $product['name']; ?>" class="img-responsive" alt="<?php echo $product['name']; ?>"/>
</div>
<?php } else if ($product['thumb']) { ?>
<img src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" class="img-responsive" alt="<?php echo $product['name']; ?>" />
<span>Categories: </span><?php echo $product['category']; ?>
<?php } ?>
</div>
I have mentioned my Controller and Template file , which i am using products search ... where when search i am getting product name , image , description as well ..
Now i want display the Category name also below the description for each product ..
Replace this code
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
//'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
'description' => html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'),
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $result['rating'],
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'] . $url)
);
BY THIS CODE.
$cats = $this->model_catalog_product->getCategories($product_info['product_id']);
$sep = '';
$catnames = '';
foreach ($cats as $cat){
$catarr = $this->model_catalog_category->getCategory($cat['category_id']);
$catnames .= $sep.$catarr['name'];
$sep = ', ';
}
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
//'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
'description' => html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'),
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $result['rating'],
'category' => $catnames,
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'] . $url)
);
And in the template file, You can simply call it in your loop using $product['category'].

Wordpress - How to get all Author (or all User) Names and their associated ID's?

At the moment I am doing the following to get a list of all authors:
$authors = wp_list_authors('html=0&style=none&echo=0&exclude_admin=1&optioncount=0&show_fullname=1&hide_empty=1&orderby=name&order=ASC');
$authors_array = explode(',', $authors);
for ($j = 0; $j < count($authors_array); $j++)
{
echo '<li id="">'.$authors_array[$j].'</li>';
}
How can I also get the user's id?
I have looked everywhere and I can't think of a way to get all authors and another bit of meta data.
Another way I found was to do the following (swear I couldn't find anything like this earlier):
$authors = get_users('role=author&orderby=display_name&order=ASC');
foreach ($authors as $author) {
if (count_user_posts($author->ID) > 0) {
echo '<li id="' . $author->ID . '">' . $author->display_name . '</li>';
}
}
You can either override the function in functions.php or create a new function based on the original. Looking at the code it's easy enough to just include the $author_id in the foreach loop.
Code for original is here
Try this and make sure you add the new parameter (includeauthorid) into the function call...
$authors = wp_list_authors('html=0&style=none&echo=0&exclude_admin=1&optioncount=0&show_fullname=1&hide_empty=1&orderby=name&order=ASC&includeauthorid=1');
new function - ps I wouldn't recommend changing the original. Just put this in functions.php.
function wp_list_authors($args = '') {
global $wpdb;
$defaults = array(
'orderby' => 'name', 'order' => 'ASC', 'number' => '',
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
'style' => 'list', 'html' => true,
'includeauthorid' => false
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
$return = '';
$query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) );
$query_args['fields'] = 'ids';
$authors = get_users( $query_args );
$author_count = array();
foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
$author_count[$row->post_author] = $row->count;
foreach ( $authors as $author_id ) {
$author = get_userdata( $author_id );
if ( $exclude_admin && 'admin' == $author->display_name )
continue;
$posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
if ( !$posts && $hide_empty )
continue;
$link = '';
if ( $show_fullname && $author->first_name && $author->last_name )
$name = "$author->first_name $author->last_name";
else
$name = $author->display_name;
if( $includeauthorid)
$name .= ' ('. $author_id .')';
if ( !$html ) {
$return .= $name . ', ';
continue; // No need to go further to process HTML.
}
if ( 'list' == $style ) {
$return .= '<li>';
}
$link = '' . $name . '';
if ( !empty( $feed_image ) || !empty( $feed ) ) {
$link .= ' ';
if ( empty( $feed_image ) ) {
$link .= '(';
}
$link .= '<a href="' . get_author_feed_link( $author->ID ) . '"';
$alt = $title = '';
if ( !empty( $feed ) ) {
$title = ' title="' . esc_attr( $feed ) . '"';
$alt = ' alt="' . esc_attr( $feed ) . '"';
$name = $feed;
$link .= $title;
}
$link .= '>';
if ( !empty( $feed_image ) )
$link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />';
else
$link .= $name;
$link .= '</a>';
if ( empty( $feed_image ) )
$link .= ')';
}
if ( $optioncount )
$link .= ' ('. $posts . ')';
$return .= $link;
$return .= ( 'list' == $style ) ? '</li>' : ', ';
}
$return = rtrim($return, ', ');
if ( !$echo )
return $return;
echo $return;
}

Categories