2

I am using Breadcrumb NavXT plug-in in my pages but now I am facing a problem

I need to hide the "Home" link in some pages. I can do that either by hiding it in CSS or by not creating it at all. in PHP

If I want to use CSS - I dont have any indication for first link and its ">>" separator - there is no id or special class for it

If I go for the PHP solution of not creating it at all in those cases - my problem is I can't send any parameters to the "bcn_display()" function

Can I do it with my current plug-in or there is a better one?

thanks!

5 Answers 5

1

Update

Mtekk's answer does basically what I was suggesting w/o the need to touch the plugin itself, so my answer is of quite theoretical nature. He describes how to do that easily.


I think the easiest thing for doing so as you're mainly confident with the plugin (saying something minor to change), it would be to hack the plugin and insert a filter hook. Then you can filter based on your needs. If you share the code this might it make even into the original plugin, so you can benefit of updates.

Example:

See file breadcrumb_navxt_class.php. I've slightly changed a single function to create a filter:

/**
 * add
 * 
 * Adds a breadcrumb to the breadcrumb trail
 * 
 * @return pointer to the just added Breadcrumb
 * @param bcn_breadcrumb $object Breadcrumb to add to the trail
 */
function &add(bcn_breadcrumb $object)
{
    $doAdd = true;
    $doAdd = do_filter('_bcn_breadcrumb_trail_add_filter', $doAdd, $object);
    if ($doAdd) {
        $this->trail[] = $object;  
        return $this->trail[count($this->trail) - 1];
    } else {
        return $object; // NOTE: violates function return spec but prevents errors later on.
    }
}

The introduced filter is named _bcn_breadcrumb_trail_add_filter. You now can hook to that filter and return FALSE which will prevent adding $object to the breadcrumb trail.

In you filter you can decide based on $object wether or not you want to have it added. That is a bcn_breadcrumb so you can check for it's title member to look for home or so:

 $object->title

The only thing you need to do is to add some code:

 add_filter('_bcn_breadcrumb_trail_add_filter', function($doAdd, $object) {
     if ($object->title === 'Home'
         && /* this is a page I don't want to have it displayed */) {
         $doAdd = false;
     }
     return $doAdd;
 });

BTW, the author of the plugin is around here from time to time, so probably he has some feedback on this as well.

2
  • Won't returning a bogus object cause "call to non member function" errors on the next line when it tries to set the anchor for the breadcrumb?
    – mtekk
    Nov 23, 2010 at 20:05
  • Well, the object isn't bogus, that's just the one that would have been added to the trail w/o adding it. But in the end, using the existing action is much better. I just was not aware of it (shame on me :))
    – hakre
    Nov 23, 2010 at 20:06
3

What kind of pages are you trying to hide the home breadcrumb on? If it is just on the home page, wrap the call in a check for is_front_page e.g. if(!is_front_page){bcn_display();}

If you do not want the home breadcrumb at all, make sure "Place the home breadcrumb in the trail." is not checked on the settings page.

If you want to conditionally remove the home breadcrumb (honestly does not make sense to do, but whatever). Look at the bcn_after_trail action. It will pass in the current breadcrumb_trail object, and you can pop off the last member in the trail array (the home breadcrumb).

An example would be: add_action('bcn_after_fill', 'foo_pop'); function foo_pop(&$trail) { if(is_page('foo')) { array_pop($trail->trail); } }

That would remove the home breadcrumb from the trail for the page with slug 'foo'.

2
  • I didn't know that action. Good to see there is one. I'll update my answer accordingly.
    – hakre
    Nov 23, 2010 at 19:58
  • Yeah, I added two actions to the fill() function a few releases back. I have not had much time to write up some examples for using them, and they only have small blurb in the documentation at the moment. So I don't expect them to be well known yet.
    – mtekk
    Nov 23, 2010 at 20:08
1

Try Breadcrumb Trail, I took quick look in source and it seems to accept fitting argument 'show_home' => __( 'Home', $textdomain ),

1

You can also use a custom function in your theme, follow an example with the "Home"; you can commented this or delete

if ( !function_exists('fb_breadcrumb_nav') ) {
        function fb_breadcrumb_nav() {
                if ( !is_home() || !is_front_page() ) {
                        echo '<p class="breadcrumb"><a href="';
                        echo get_option('home');
                        echo '">';
                        bloginfo('name') . _e('</a> &raquo; ', FB_BASIS_TEXTDOMAIN);
                        if ( is_category() || is_single() ) {
                                $category = get_the_category();
                                $ID = $category[0]->cat_ID;
                                echo get_category_parents($ID, TRUE, __(' &raquo; ', FB_BASIS_TEXTDOMAIN), FALSE );
                        } elseif (is_page() && $post->post_parent ) {
                                _e( get_the_title($post->post_parent) );
                                _e(' &raquo; ');
                                _e( the_title() );
                        } elseif ( is_search() ) {
                                _e('Suche nach: ', FB_BASIS_TEXTDOMAIN) . the_search_query() . _e('</p>');
                        }
                }
        }
}

now an example without Home

if ( !function_exists('fb_breadcrumb_nav') ) {
        function fb_breadcrumb_nav() {
                if ( !is_home() || !is_front_page() ) {
                        echo '<p class="breadcrumb">';
                        _e( ' &raquo; ', FB_BASIS_TEXTDOMAIN );
                        if ( is_category() || is_single() ) {
                                $category = get_the_category();
                                $ID = $category[0]->cat_ID;
                                echo get_category_parents($ID, TRUE, __(' &raquo; ', FB_BASIS_TEXTDOMAIN), FALSE );
                        } elseif (is_page() && $post->post_parent ) {
                                _e( get_the_title($post->post_parent) );
                                _e(' &raquo; ');
                                _e( the_title() );
                        } elseif ( is_search() ) {
                                _e('Suche nach: ', FB_BASIS_TEXTDOMAIN) . the_search_query() . _e('</p>');
                        }
                }
        }
}
1

Unbelievable but again I found the solution myself..

it's probably u that gave me the inspiration - thanx :)

I decided to pass a parameter to bcn_display() in case I dont want to get the "home" link in the trail . By default, the parameter is set to true .
When I don't want the "home" link to be displayed ) send true

now I have added the declaration of the parameter in these functions and added some code

//in file breadcrumb_navxt_class.php

function do_home($displayhome=true)
{...

if(($this->opt['home_display'])&&($displayhome))

...}

function fill($displayhome=false)
{...

if(!is_front_page())
{

     $this->do_home($displayhome);

}

...}

function display($displayhome=true,$return = false, $linked = true, $reverse = false)
{...}

//in file breadcrumb_navxt_admin.php

function display($displayhome=true,$return = false, $linked = true, $reverse = false)
{
        //Grab the current settings from the DB

        $this->breadcrumb_trail->opt = $this->get_option('bcn_options');

        //Generate the breadcrumb trail

        $this->breadcrumb_trail->fill($displayhome);

        return $this->breadcrumb_trail->display($displayhome,$return, $linked, $reverse);

}

function bcn_display($displayhome=true,$return = false, $linked = true, $reverse = false) {

        global $bcn_admin;

        return $bcn_admin->display($displayhome,$return, $linked, $reverse);

}
1
  • You really should use the method I presented as it requires no modification of the Breadcrumb NavXT files. On every new release, for example tomorrow when 3.7.0 is released, you will have to reapply your solution, where as mine will continue to work.
    – mtekk
    Nov 28, 2010 at 21:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.