Wordpress - Customizing Breadcrumbs NavXT - php

Right now my breadcrumbs are showing up like Home > Services > X-Ray, but I would like them to just show up like Home > Services, is there a way to customize Breadcrumbs NavXT to do this this?

go in /wp-content/plugins/breadcrumb-navxt/class.bcn_breadcrumb_trail.php
find function display and replace it with:
public function display($return = false, $linked = true, $reverse = false)
{
//Set trail order based on reverse flag
$this->order($reverse);
//Initilize the string which will hold the assembled trail
$trail_str = '';
$position = 1;
//The main compiling loop
foreach($this->breadcrumbs as $key => $breadcrumb)
{
//We do different things for the separator based on the breadcrumb order
if($reverse)
{
//Add in the separator only if we are the 2nd or greater element
if($key > 0)
{
$trail_str .= $this->opt['hseparator'];
}
}
else
{
if($key == 0 ) continue;
//Only show the separator when necessary
if($key < count($this->breadcrumbs) - 1)
{
$trail_str .= $this->opt['hseparator'];
}
}
//Trim titles, if needed
if($this->opt['blimit_title'] && $this->opt['amax_title_length'] > 0)
{
//Trim the breadcrumb's title
$breadcrumb->title_trim($this->opt['amax_title_length']);
}
//Place in the breadcrumb's assembled elements
$trail_str .= $breadcrumb->assemble($linked, $position);
$position++;
}
//Should we return or echo the assembled trail?
if($return)
{
return $trail_str;
}
else
{
//Helps track issues, please don't remove it
$credits = "<!-- Breadcrumb NavXT " . $this::version . " -->\n";
echo $credits . $trail_str;
}
}
if you use a different version i have add this row:
if($key == 0 ) continue;

Related

PHPExcel, If $value = $value then skip?

I'm trying to read a xlsx file using PHPExcel, however I got a problem trying to generate a navbar using column 1 values. Since Most of my column contains same type of value e.g. T-Shirt, T-Shirt... Cap, Cap, Cap.. Hoodies, Hoodies...
I got a problem filtering out the same value, does someone has any idea how to skip the same value as previous one?
Here's my code:
<?php
for ($column = 2; $column <= $highestRow; ++$column) {
$cat = $objWorksheet->getCellByColumnAndRow(0, $column)->getValue();
if ($cat == $cat) {
continue;
} else {
echo '<li>'.$cat.'</li>';
}
}
?>
Would be great if it works under <li></li> for my navbar
Try this.
//Get the value of $column -1 aka the previous column.
$previousColumn = $objWorksheet->getCellByColumnAndRow(0, $column - 1)->getValue();
$cat = $objWorksheet->getCellByColumnAndRow(0, $column)->getValue();
if ($cat == $previousColumn) {
continue;
} else {
echo '<li>'.$cat.'</li>';
}

Return value of an array for each item in a list

I'm still quite new to writing functions and PHP so please excuse me if this is easy. Let me provide a little background...
I have built an e-commerce site using MarketPress by WPMUDev and trying to write a function that will show the stock remaining depending on the product variation selected.
In MarketPress, if you have more than one variation of a product, e.g. Shirt (Blue/Black/White) then you specify the one product with three variations. On the single product page, you have a drop down box for each variant.
The code I have so far will find the stock level of the initial product only and not the variants. Please see below:
function mp_product_stock_sc( $atts ){
global $post;
$product_id = $post->ID;
$stock = get_post_meta($product_id, 'mp_inventory', true);
$high_st = 1;
//return 'Default Stock: ' . $stock[0];
if ($stock[0] <= $high_st AND $stock[0] > 0 ) {
//return 'Hurry! We only have ' . $stock[0] . ' in stock!';
return 'Hurry! Only one left in stock!';
} elseif ($stock[0] == 0) {
return '';
} else {
return 'In Stock';
}
return 'Stock: ' . $stock[0];
}
add_shortcode( 'mp_product_stock', 'mp_product_stock_sc' );
I know that the function is selecting the first variant in $stock[0] because the [0] is explicitly defined. By manually writing [1] it would select the next variant and so on.
What I need to do is, on the product variation drop down list, for each variant to have next to it the stock, e.g:
Shirt (Blue) - In Stock
Shirt (Black) - Hurry only 1 left!
Shirt (White) - In Stock
I know where to put the code, just not how to return the value.
Any advice greatly appreciated. There's probably a much better way of writing this too...
Thanks!
Edit: Adding below the code where the drop down is generated.
//create select list if more than one variation
if (is_array($meta["mp_price"]) && count($meta["mp_price"]) > 1 && empty($meta["mp_file"])) {
$variation_select = '<select class="mp_product_variations" name="variation">';
foreach ($meta["mp_price"] as $key => $value) {
$disabled = (in_array($key, $no_inventory)) ? ' disabled="disabled"' : '';
$variation_select .= '<option value="' . $key . '"' . $disabled . '>' . esc_html($meta["mp_var_name"][$key]) . ' - ';
if ($meta["mp_is_sale"] && $meta["mp_sale_price"][$key]) {
$variation_select .= $mp->format_currency('', $meta["mp_sale_price"][$key]);
} else {
$variation_select .= $mp->format_currency('', $value);
}
$variation_select .= "</option>\n";
}
$variation_select .= "</select> \n";
} else {
$button .= '<input type="hidden" name="variation" value="0" />';
}
This answer is based on the fact that you have an item code that you can feed into the method:
EDIT: I added a Randomizer method that will do two things. 1) You can set it to return a random number to check that the $calltoaction if statement is working (set the second variable in Randomizer to true instead of false) and 2) It will check that your number is numeric and return it back if it is. If not numeric, it will return 'err' which means it's not a number.
class StockCheck
{
public static function Fetch($itemcode, $high_st = 1)
{
// Check your stock on this item code (I pressume this is what it's doing.
// If not, this is what it should to do.)
$stock = get_post_meta($itemcode, 'mp_inventory', true);
// Assign number. If randomizer set to true, it will just generate a random num
// to test the if statement below. Change to false to return true number
$_inStock = self::Randomizer($stock[0],false);
if($_inStock !== 'err') {
if($_inStock !== 0) {
// If stock is less than or equal to 10
if($_inStock <= 10)
$calltoaction = 'Hurry! Only '.$_inStock.' Left in Stock!';
// If stock is greater than 10 but less than or equal to 20
elseif($_inStock > 10 && $_inStock <= 20)
$calltoaction = 'Only a Few Left. Going fast!';
// Anthing else is just in stock
else
$calltoaction = 'In Stock';
}
// If zero, out of stock.
else
$calltoaction = 'Out of Stock, Sorry!';
}
return (isset($calltoaction))? $calltoaction:'Error: Stock value not numeric';
}
protected static function Randomizer($value = 0, $randomizer = false)
{
// If set to true, it will just generate a random number for testing purposes
$defVal = ($randomizer == true)? rand(0,30):$value;
// If $defVal is not a numeric, return "err"
return (is_numeric($defVal) || $defVal == 0)? $defVal:'err';
}
}
if(is_array($meta["mp_price"]) && count($meta["mp_price"]) > 1 && empty($meta["mp_file"])) {
$variation_select = '<select class="mp_product_variations" name="variation">';
foreach ($meta["mp_price"] as $key => $value) {
$disabled = (in_array($key, $no_inventory)) ? ' disabled="disabled"' : '';
$variation_select .= '<option value="' . $key . '"' . $disabled . '>' . esc_html($meta["mp_var_name"][$key]) . ' - ';
$variation_select .= ($meta["mp_is_sale"] && $meta["mp_sale_price"][$key])? $mp->format_currency('', $meta["mp_sale_price"][$key]): $mp->format_currency('', $value);
// This is where you would feed your item code...
$variation_select .= StockCheck::Fetch($meta["mp_itemcode"],1);
$variation_select .= "</option>\n";
}
$variation_select .= "</select> \n";
}
else
$button .= '<input type="hidden" name="variation" value="0" />';

How To Remove A Pipe Symbol From Menu Generated With PHP Loop And XML

Wondering if someone could assist me with this.
I am generating menu options for both an ELEVAT (elevated) and AUTHOR (authorised basic user) using a PHP loop which accesses an XML file set so that secure = 0 or 1 as follows:
<menuitem>
<itemname>Edit Event</itemname>
<itemfilename>editevent.php</itemfilename>
<itemfilepath>scripts</itemfilepath>
<secure>0</secure>
</menuitem>
<menuitem>
<itemname>Archive</itemname>
<itemfilename>eventsarchive.php</itemfilename>
<itemfilepath>scripts</itemfilepath>
<secure>1</secure>
</menuitem>
0 = authorised basic user and 1 = elevated user
Considering that I have so many menu links now I have 2 rows for the elevated user, I am trying to output to the browser so that the last menu item of the first row of page links does not print a pipe.
So far I have managed to get the basic user login to generate without a pipe after the last link however I cannot for the life of me figure out how to remove the | from the last link of the first row of links for the elevated user login.
Here is my PHP function:
function pagemenu($pageFile)
{
# Open the DB connection
$sql = dbconnect();
$linkPath = getlinkpath($pageFile);
# The $pageFile value is the file name of the calling page
# Set the path to the menu source file
$xmlsrc = $linkPath . 'xml/menulist.xml';
# Load the menu source file into a SimpleXML object
if (!$menulist = simplexml_load_file($xmlsrc)) {
# Print an error message if the source file does not load
print 'Unable to load the XML source file';
} # Process the menulist.xml file
else {
# Set a counter for the menu items
$itemCount = 1;
# Find the folder path of the calling page
foreach ($menulist as $menu) {
if ($menu->itemfilename == $pageFile) {
$linkPath = $menu->itemfilepath;
} else {
$linkPath = '';
}
}
# Scroll through all the menu items and build the menu
print '<div class="menubar">' . PHP_EOL;
foreach ($menulist as $menu) {
# If the menu item is the current page don't display a link
if ($menu->itemfilename == $pageFile) {
# Don't print a pipe before the first menu item
if ($itemCount != 1) {
print ' | ';
}
print $menu->itemname;
} # Display a link for all the other menu items
else {
# Construct the link path to the menu item file
# A link to the index file
if ($menu->itemfilepath == 'root') {
$thisLink = '../' . $menu->itemfilename;
} # A link to a file in the same folder
elseif ($menu->itemfilepath == $linkPath) {
$thisLink = $menu->itemfilename;
} elseif ($pageFile == 'index.php' and $menu->itemfilepath != $linkPath) {
$thisLink = $menu->itemfilepath . '/' . $menu->itemfilename;
} # A link to a file in a different folder
elseif ($menu->itemfilepath != $linkPath) {
$thisLink = '../' . $menu->itemfilepath . '/' . $menu->itemfilename;
}
# Create the connection to the mysql database and handle error if there is one
$query = mysqli_query(
$sql, "SELECT user_type_id FROM user_details WHERE user_id = '$_SESSION[validUser]'"
);
$usertype = mysqli_fetch_assoc($query);
# Don't print a pipe before the first menu item
if ($usertype['user_type_id'] == "ELEVAT") {
if ($itemCount != 1) {
# Tried the following added to the above line without success
# && ($itemCount !=11))
print ' | ';
}
} else {
if (($itemCount != 1) && ($itemCount != 5) && ($itemCount != 6) && ($itemCount != 7)
&& ($itemCount != 8)
&& ($itemCount != 9)
&& ($itemCount != 10)
&& ($itemCount != 11)
&& ($itemCount != 12)
&& ($itemCount != 13)
&& ($itemCount != 14)
&& ($itemCount != 15)
&& ($itemCount != 16)
&& ($itemCount != 17)
) {
print ' | ';
}
}
# Display the menu item as a link
if ($menu->secure == 1) {
if ($usertype['user_type_id'] == "ELEVAT") {
print '' . $menu->itemname . '';
}
} else {
print '' . $menu->itemname . '';
}
}
# Increment the menu item counter
$itemCount++;
}
}
print '</div>' . PHP_EOL;
return;
# Close the DB connection
mysqli_close($sql);
}
so that the last menu item of the first row of page links does not print a pipe.
This is a common problem. You need to know when the last element is there or not. You can do this by caching so that you know if an element is the last element or not (compare with CachingIterator::hasNext(), or count() if you're working with arrays).
In your case it is perhaps most easy to create an xpath expression that fetches all those entries from the menu that you're interested in.
That is first for the current page and second for the user-group you have there (secure 0/1).
http://php.net/simplexml.examples-basic covers how to do that.
Once done, the SimpleXMLElement::xpath() method will give you an array you can iterate over to output your menu. You can then use a condition based on count() or do array-iteration and cache it to use the CachingIterator::hasNext() method to find out wheter you're on the last row or not.
$entries = $xml->xpath($query);
$entries = new CachingIterator(new ArrayIterator($entries));
foreach ($entries as $entry)
{
$menutItem = menu_item_create_from_xml($entry);
menu_item_output($menutItem, $entries->hasNext());
# ^^^ tell the menu output function whether or
# not this is the last element of the menu
}

Magento Top Nav: Add CSS class to first category using PHP

I need help with customizing Navigation.php in Magento. I'm using Superfish to create a top nav bar that will always have the first tab open if no other tab has been selected (this Superfish nav bar example has the second tab as the default: http://users.tpg.com.au/j_birch/plugins/superfish/#sample4 )
However, since Magento generates the top menu using PHP, I need to insert CSS class sfHover using PHP as well.
This is the code that Magento uses to insert CSS classes into the top nav links:
$classes = array();
$classes[] = 'level' . $level;
$classes[] = 'nav-' . $this->_getItemPosition($level);
if ($this->isCategoryActive($category)) {
$classes[] = 'active';
}
$linkClass = '';
if ($isOutermost && $outermostItemClass) {
$classes[] = $outermostItemClass;
$linkClass = ' class="'.$outermostItemClass.'"';
}
if ($isFirst) {
$classes[] = 'first';
}
if ($isLast) {
$classes[] = 'last';
}
if ($hasActiveChildren) {
$classes[] = 'parent';
}
This is the code Magento uses to determine each category's position in the top nav:
protected function _getItemPosition($level)
{
if ($level == 0) {
$zeroLevelPosition = isset($this->_itemLevelPositions[$level]) ? $this->_itemLevelPositions[$level] + 1 : 1;
$this->_itemLevelPositions = array();
$this->_itemLevelPositions[$level] = $zeroLevelPosition;
} elseif (isset($this->_itemLevelPositions[$level])) {
$this->_itemLevelPositions[$level]++;
} else {
$this->_itemLevelPositions[$level] = 1;
}
$position = array();
for($i = 0; $i <= $level; $i++) {
if (isset($this->_itemLevelPositions[$i])) {
$position[] = $this->_itemLevelPositions[$i];
}
}
return implode('-', $position);
}
I've tried the following to add my CSS class:
if ($this->_getItemPosition($level) == "1") {
$classes[] = 'sfHover';
}
and
if ($position == "1") {
$classes[] = 'sfHover';
}
But neither of them work, either with the three equal signs === or with single quotes.
Does anyone with more Magento / PHP knowledge than me know what I can do? Thanks in advance!
Update: Superfish script strips out sfHover class. Need to add "active" class instead to Navigation.php.
I can do it in top.phtml with the following code on a manually coded nav:
<?php $_anyActive = false; foreach ($this->getStoreCategories() as $_category) { $_anyActive = $_anyActive || $this->isCategoryActive($_category); } ?>
<li class="level0 nav-1 level-top first parent <?php echo !$_anyActive ? 'active' : '' ?>">
But I don't know how to integrate that with the code from Navigation.php...
I would try the following. Change
if ($isFirst) {
$classes[] = 'first';
}
to
if ($isFirst) {
$classes[] = 'first sfHover';
}
That should insert both the class first and sfHover into the first element in the menu array. The other way you could do this is remove the auto-created menu from your theme and replace it with content blocks to deliver your menu items. It's not as elegant but gets the job done.

Add substr max length to my function

Below is a code snippet from the file I am working with. I will start by saying I have attempted to find out on my own many times with failure, I am not a coder but I wish I knew more. I need some help figuring out how to add a substr length to the string $forum
This function outputs the latest 5 forum topics. The problem I'm having is the topic titles are to long for where the widget is being placed, so I wanted to truncate it to max 35 characters displayed. Can you help me? I know I'm a newb!
function sf_recent_posts_tag($limit=5, $forum=false, $user=true, $postdate=false, $listtags=true, $forumids=0, $posttime=false, $avatar=false, $size=25)
{
global $wpdb, $current_user, $sfvars;
$limit = sf_esc_int($limit);
if (empty($limit)) return;
sf_initialise_globals($sfvars['forumid']);
$out = '';
$forum_ids = '';
# are we passing forum ID's?
if ($forumids != 0)
{
$flist = explode(",", $forumids);
foreach($flist as $thisforum)
{
if (sf_can_view_forum($thisforum))
{
$forum_ids[] = $thisforum;
}
}
} else {
# limit to viewable forums based on permissions
if($current_user->forumadmin == false)
{
$allforums = sf_get_forum_memberships($current_user->ID);
if ($allforums)
{
foreach ($allforums as $thisforum)
{
if (sf_can_view_forum($thisforum->forum_id))
{
$forum_ids[] = $thisforum->forum_id;
}
}
} else {
return '';
}
}
}
# get out if nothing to see
if($current_user->forumadmin == false && empty($forum_ids)) return '';
# create where clause based on forums that current user can view
if ($forum_ids != '')
{
$where = ' AND '.SFPOSTS.".forum_id IN (" . implode(",", $forum_ids) . ") = 1 ";
} else {
$where = '';
}
$sfposts = $wpdb->get_results("SELECT DISTINCT topic_id
FROM ".SFPOSTS."
WHERE post_status = 0 ".$where."
ORDER BY post_id DESC
LIMIT ".$limit);
if($sfposts)
{
foreach($sfposts as $sfpost)
{
$postdetails = sf_get_last_post_in_topic($sfpost->topic_id);
$thisforum = sf_get_forum_record($postdetails->forum_id);
$p=false;
# Start contruction
if($listtags) $out.="<li class='sftagli'>\n";
if ($avatar)
{
if ($postdetails->user_id)
{
$icon = 'user';
if (sf_is_forum_admin($postdetails->user_id)) $icon='admin';
} else {
$icon = 'guest';
}
$out.= sf_render_avatar($icon, $postdetails->user_id, sf_filter_email_display($postdetails->user_email), sf_filter_email_display($postdetails->guestemail), false, $size);
}
$out.= sf_get_topic_url_newpost($thisforum->forum_slug, $sfpost->topic_id, $postdetails->post_id, $postdetails->post_index);
if($forum)
{
if ($p == false) $out.="<p class='sftagp'>";
$out.= __("posted in forum", "sforum").' '.sf_filter_title_display($thisforum->forum_name)." "."\n";
$p=true;
}
if($user)
{
if($p == false) $out.="<p class='sftagp'>";
$poster = sf_build_name_display($postdetails->user_id, sf_filter_name_display($postdetails->display_name));
if(empty($poster)) $poster = sf_filter_name_display($postdetails->guest_name);
$out.=__("by", "sforum").' '.$poster.' '."\n";
$p=true;
}
if($postdate)
{
if($p == false) $out.="<p class='sftagp'>";
$out.=__("on", "sforum").' '.sf_date('d', $postdetails->post_date)."\n";
if ($posttime)
{
$out.=' '.__("at", "sforum").' '.sf_date('t', $postdetails->post_date)."\n";
}
$p=true;
}
if($p) $out.="</p>\n";
if($listtags) $out.="</li>\n";
}
} else {
if($listtags) $out.="<li class='sftagli'>\n";
$out.='<p>'.__("No Topics to Display", "sforum").'</p>'."\n";
if($listtags) $out.="</li>\n";
}
echo($out);
return;
}
I can't find where you get the post titles in your code, but to truncate a string to 35 chars you simply need something like $truncated = substr($title, 0, 35)
You may find it helpful to append '...' to the truncated titles:
$truncated = substr($title, 0, 35).'...';
EDIT:
without seeing a lot of code that you have omitted it's hard to say, but I suspect it's a matter of changing
$out.= sf_get_topic_url_newpost($thisforum->forum_slug, $sfpost->topic_id, $postdetails->post_id, $postdetails->post_index);
to
$out.= substr(sf_get_topic_url_newpost($thisforum->forum_slug, $sfpost->topic_id, $postdetails->post_id, $postdetails->post_index), 0, 35).'...';

Categories