Solved

Change a filter function in php

Posted on 2014-02-26
9
296 Views
Last Modified: 2014-03-04
Hi,

I'd like to change this code but can't seem to figure it out. On page load there is a link/button selected called "ALL". Then two other links/buttons are loaded from Wordpress portfolio categories Web and Branding. I'd like to make Web the default item and be selected and the ALL no longer appear.

Line 15 in the code is where the All filter is, then just below that is where it loads the other categories

add_shortcode('filterable_portfolio', 'vp_filterable_portfolio');
function vp_filterable_portfolio($atts, $content=null) {
	extract(shortcode_atts(array(
		'categories' => '',
		'number' => 15
	), $atts));
	$id = rand(1, 50000);
	$output = '<div class="filterable-' . $id . '">';
	global $post;
	$categories = esc_attr($categories);
	$categories = str_replace(' ', '', $categories);
	$output .= '<div class="filter-categories sixteen columns">
				<div class="filter">
					<ul>
						<li><a href="" data-filter="*" class="selected">ALL</a></li>';
	if($categories == '')
	{
		$cats = get_categories();
		foreach($cats as $cat) {
			$output .= '<li><a href="" data-filter=".' . $cat->term_id . '">' . $cat->name . '</a></li>';
		}
	}
	else
	{
		$cats = explode(",", $categories);
		foreach($cats as $cat) {
			$cat_details = get_category($cat);
			$output .= '<li><a href="" data-filter=".' . $cat . '">' . $cat_details->name . '</a></li>';
		}
	}

	$output .= '</ul>
				</div> <!-- end filter -->

Open in new window


Thanks for the help, hopefully I copied out enough code. Here is a link to a demo site where the code is in use. Click on Work and you'll see the ALL then Beautiful Cars etc http://demo.teothemes.com/?theme=scrn
0
Comment
Question by:Karessa
  • 5
  • 4
9 Comments
 
LVL 25

Expert Comment

by:James Rodgers
ID: 39892674
EDIT: added the web check

i don't recommend doing this but it should work and get rid of the all option, update the above to:

add_shortcode('filterable_portfolio', 'vp_filterable_portfolio');
function vp_filterable_portfolio($atts, $content=null) {
	extract(shortcode_atts(array(
		'categories' => '',
		'number' => 15
	), $atts));
	$id = rand(1, 50000);
	$output = '<div class="filterable-' . $id . '">';
	global $post;
	$categories = esc_attr($categories);
	$categories = str_replace(' ', '', $categories);
	$output .= '<div class="filter-categories sixteen columns">
				<div class="filter">
					<ul>';
					//	<li><a href="" data-filter="*" class="selected">ALL</a></li>';
	if($categories == '')
	{
		$cats = get_categories();
		foreach($cats as $cat) {
			$selected='';
			if ($cat =="Web")[
				$selected='selected';
			}
			$output .= '<li><a href="" data-filter=".' . $cat->term_id . '" class="'.$selected.'">' . $cat->name . '</a></li>';
		}
	}
	else
	{
		$cats = explode(",", $categories);
		foreach($cats as $cat) {
		$selected='';
			if ($cat =="Web")[
				$selected='selected';
			}
			$cat_details = get_category($cat);
			$output .= '<li><a href="" data-filter=".' . $cat . '" class="'.$selected.'">' . $cat_details->name . '</a></li>';
		}
	}

	$output .= '</ul>
				</div> <!-- end filter -->
                                  

Open in new window

0
 

Author Comment

by:Karessa
ID: 39893412
thanks for the edit, I'll give it a try shortly, why do you not recommend it?
0
 

Author Comment

by:Karessa
ID: 39893878
jester,

update on this, so yes the all button is gone thanks

but on page load the web is not selected, no item is selected. can you help me figure that piece out?

thanks for the assistance.
0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 39894594
"thanks for the edit, I'll give it a try shortly, why do you not recommend it? "

anything that is in a core package, WordPress core, plugin, etc. that gets changed without using an action or filter hook will be overwritten in an update.

So a year from now, someone will run an update, the page will look different and the developer will have forgotten what was changed to make it the way it was. someone will spend time, probably unpaid, to track down what changed and correct it.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Expert Comment

by:James Rodgers
ID: 39894599
went a littel to quick for my own good, variable names in the for each were wrong

add_shortcode('filterable_portfolio', 'vp_filterable_portfolio');
function vp_filterable_portfolio($atts, $content=null) {
	extract(shortcode_atts(array(
		'categories' => '',
		'number' => 15
	), $atts));
	$id = rand(1, 50000);
	$output = '<div class="filterable-' . $id . '">';
	global $post;
	$categories = esc_attr($categories);
	$categories = str_replace(' ', '', $categories);
	$output .= '<div class="filter-categories sixteen columns">
				<div class="filter">
					<ul>';
					//	<li><a href="" data-filter="*" class="selected">ALL</a></li>';
	if($categories == '')
	{
		$cats = get_categories();
		foreach($cats as $cat) {
			$selected='';
			if ($cat->name  =="Web")[
				$selected='selected';
			}
			$output .= '<li><a href="" data-filter=".' . $cat->term_id . '" class="'.$selected.'">' . $cat->name . '</a></li>';
		}
	}
	else
	{
		$cats = explode(",", $categories);
		foreach($cats as $cat) {
		$selected='';
			if ($cat_details->name =="Web")[
				$selected='selected';
			}
			$cat_details = get_category($cat);
			$output .= '<li><a href="" data-filter=".' . $cat . '" class="'.$selected.'">' . $cat_details->name . '</a></li>';
		}
	}

	$output .= '</ul>
				</div> <!-- end filter -->
                                  

Open in new window

0
 

Author Comment

by:Karessa
ID: 39895269
Jester,

Appreciate the point on updating, it's a child theme type situation in WP so updates won't overwrite the code change and I'm pretty meticulous note taker for update and changes.

The code change does now select Web but it also selects every other category as well. I have 4, Web, Branding, Social, IT. on page load my hope was to have Web be selected and the 6 images load that belong to that category, the other 3 would not be selected until clicked on.

Make sense? do you need any additional info to help?

thanks again for you assistance.
0
 
LVL 25

Accepted Solution

by:
James Rodgers earned 250 total points
ID: 39895358
do you have a link to the page?

sunds like the category name compre is not functioning properly, need to see teh utput
0
 

Author Comment

by:Karessa
ID: 39896042
the site is under construction and locked out except for admin accounts. I don' t mind sending you temp log in info but I don't want to post it here in the clear? you have somewhere ti can send it?

thanks
0
 

Author Comment

by:Karessa
ID: 39904486
It's all good for now, thanks for the help jester!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
unset shopping cart session 15 32
get domain with php 7 20
html input type 3 22
Wordpress errors showing up, hacked? 4 0
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now