• Resolved greendays

    (@greendays)


    Hi.

    I added code on my site for change last breadcrumb, but i don’t know how to change previous breadcrumb. Can you help me with previous breadcrumb?

    I added custom field in WordPress for last breadcrumb: last_breadcrumb_title

    My code for functions.php:

    add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title_swapper', 3, 10);
    
    function my_breadcrumb_title_swapper($title, $type, $id)
    {
    
      $post = get_post();
      $post_id = $post->ID;
    
      if ( $post_id == $id )
      {
        if ( get_post_meta( $post_id, 'last_breadcrumb_title', true) )
        {
          $title = get_post_meta( $post_id, 'last_breadcrumb_title', true);
        }
      }
    
      return $title;
      return $type;
      return $id;
    }

    Thanks for any help.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter greendays

    (@greendays)

    Screen image: https://ibb.co/vXmKrzs.

    Now: Home / Best Bar Asia / Best Bar

    I need: Home / Asia / Best bar

    I can control only Best bar.

    Plugin Author John Havlik

    (@mtekk)

    Is “Best Bar Asia” a post or a term?

    A point of clarification, bcn_breadcrumb_title operates on every breadcrumb in the trail. You have to within it check for the specific type of breadcrumb you’d like to modify the title for. It is not specifically operating on the last breadcrumb in the trail.

    I’m not exactly sure where you found the code you started with for the filter, but it looks similar to something recently posted to the support forums which had a couple of similar issues. You only want to return $tittle rather than trying to return all three parameters that are passed into the filter (the second two returns are literally doing nothing so they shouldn’t be there). Additionally, using get_post() within this filter is not necessarily safe/not guaranteed to yield the desired results, instead you want to use the information passed into the filter (the $type array and the $id). An example is presented below:

    add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title', 3, 10);
    
    function my_breadcrumb_title($title, $type, $id)
    {
        if(in_array('post-your_post_type', $type) && get_post_meta($id, 'last_breadcrumb_title', true)) {
            $title = get_post_meta($id, 'last_breadcrumb_title', true)
        }
    
        return $title;
    }

    However, this code is only going to target posts. If “Best Bar Asia” is a term, then you will need to use get_term_meta() instead of get_post_meta() for that one. And, in the code above, you will want to check for ‘tax-your_taxonomy_type’ instead of ‘post-your_post_type’ where your_taxonomy_type or your_post_type is the taxonomy or post type you’re wanting to try to get the meta for. An example of code that would do both:

    add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title', 3, 10);
    
    function my_breadcrumb_title($title, $type, $id)
    {
        //Handle posts of type your_post_type
        if(in_array('post-your_post_type', $type) && get_post_meta($id, 'last_breadcrumb_title', true)) {
            $title = get_post_meta($id, 'last_breadcrumb_title', true)
        }
        //Handle terms of taxonomy your_taxonomy
        if(in_array('tax-your_taxonomy', $type) && get_term_meta($id, 'last_breadcrumb_title', true)) {
            $title = get_term_meta($id, 'last_breadcrumb_title', true)
        }
        return $title;
    }

    Lastly, if you really don’t want to go through with the write code to do all of this path, the Title Trixx extension (premium) provides the metaboxes for both posts and terms (of any type) to allow you to override what is used for the breadcrumb title.

    Thread Starter greendays

    (@greendays)

    It’s very cool answer. Thank you.

    Thread Starter greendays

    (@greendays)

    Hi, John Havlik.

    I have little issue with breadcrumb: “Warning: Attempt to read property “ID” on null”. It’s issue on 404-page. I hope you help me.

    How can i add rule to check on null parameters?

    My code:

    add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title_swapper', 3, 10);
    
    function my_breadcrumb_title_swapper($title, $type, $id)
    {
    
      $post = get_post();
      $post_id = $post->ID;
    
      if ( $post_id == $id )
      {
        if ( get_post_meta( $post_id, 'last_breadcrumb_title', true) )
        {
          $title = get_post_meta( $post_id, 'last_breadcrumb_title', true);
        }
      }
    
      return $title;
      return $type;
      return $id;
    }

    Sorry for silly question, but i tried all.

    Plugin Author John Havlik

    (@mtekk)

    I recommend using the code example I provided instead of the code you’re currently using. It will not have the PHP warning like your code does as it doesn’t use the $id until the case where it should be valid (i.e. that the breadcrumb is for a post). The only change that my example code needs is replacing the your_post_type in post-your_post_type with the post type you want to target (or replace it with post to target all post types).

    Thread Starter greendays

    (@greendays)

    I work with pages on WordPress.

    My pages:

    /wp-admin/post.php?post=10350&action=edit

    /wp-admin/edit.php?post_type=page

    “post-your_post_type” – it’s example, right?

    How can i find this field in my site? I tried to add:

    add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title', 3, 10);
    
    function my_breadcrumb_title($title, $type, $id)
    {
        //Handle posts of type your_post_type
        if(in_array('post_type=page', $type) && get_post_meta($id, 'last_breadcrumb_title', true)) {
            $title = get_post_meta($id, 'last_breadcrumb_title', true);
        }
        return $title;
    }

    “post_type=page” – It’s code doesn’t work.

    Plugin Author John Havlik

    (@mtekk)

    In this case, you want to replace post-your_post_type with post-page since you’re using the page post type. The in_array() function is a PHP function, not a WordPress function, so it doesn’t understand the post_type=page argument format.

    Thread Starter greendays

    (@greendays)

    If i use just ‘post-your_post_type’, code doesn’t work and breadcrumb doesn’t change. What i need to add in this field ‘post-your_post_type’?

    Plugin Author John Havlik

    (@mtekk)

    You shouldn’t add a field for post-your_post_type. In my example code, you have to replace post-your_post_type with post-page if you want to target the page post type (if you want to target a different post type, you will need to replace the your_post_type with the post type name. The following code makes this replacement and should work:

    add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title', 3, 10);
    
    function my_breadcrumb_title($title, $type, $id)
    {
        if(in_array('post-page', $type) && get_post_meta($id, 'last_breadcrumb_title', true)) {
            $title = get_post_meta($id, 'last_breadcrumb_title', true)
        }
    
        return $title;
    }
    Thread Starter greendays

    (@greendays)

    John Havlik, thanks a lot for help.

    I hope my example to help other people.

    1. I created new custom field with ‘PODS’ plugin in WordPress. I added new breadcrumb title (name – last_breadcrumb_title) in this field in plugin.
    2. I added code by John Havlik in functions.php
    add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title', 3, 10);
    
    function my_breadcrumb_title($title, $type, $id)
    {
        if(in_array('post-page', $type) && get_post_meta($id, 'last_breadcrumb_title', true)) {
            $title = get_post_meta($id, 'last_breadcrumb_title', true)
        }
    
        return $title;
    }

    It’s best solution for SEO. Code works.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘How to change text on previous bread’ is closed to new replies.