• Resolved Feniks Design

    (@feniks-design)


    Hi, is possible to change last text bread in single custom post type (title post bread) to custom text (from cm)?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Feniks Design

    (@feniks-design)

    add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title', 3, 10);
    
    function my_breadcrumb_title($title, $type, $id)
    {
        $post_id = get_the_ID();
        if ($post_id == $id && is_single() && is_singular('your_cpt')) {
            $title = get_field('your_meta_tax', $post_id);
        }
    
        return $title;
        return $type;
        return $id;
    }
    Plugin Author John Havlik

    (@mtekk)

    bcn_breadcrumb_title is the correct filter to use. However, you filter function has some problems with it that are worth discussing.

    First, it should just have one return, return $title;. Since PHP executes in order, the behavior will be the same as what you currently have (it will return right away so the second 2 returns never get executed). For WordPress filters with multiple parameters, only the first is the item to be filtered (and the one you need to return), the others provide context for the filter.

    I don’t think the conditions within your if statement are going to yield what you want. It would be better to check the $type array for the post type you care about, and then make your call to get_field. In fact, I would not call get_the_ID() either. If you want this to only apply when the breadcrumb is for the current item (not sure if that is the intent), you can check the $type array for 'current_item' being a member. This really is only necessary if the post type is hierarchical as for non-hierarchical post types aren’t going to have children (so a breadcrumb for said posts will always be the current item).

    It appears get_field is not a WordPress function, the best practice is to check for its existence before calling (to prevent PHP errors if the plugin providing that function isn’t present for some reason).

    With these changes, you code would look more like the following:

    add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title', 3, 10);
    
    function my_breadcrumb_title($title, $type, $id)
    {
        if(in_array('your_cpt', $type) && function_exists('get_field')) {
            $title = get_field('your_meta_tax', $id);
        }
    
        return $title;
    }
    Thread Starter Feniks Design

    (@feniks-design)

    Thanks! In the case of a custom post type, the prefix “post-” + custom post type name is in the $type, and the in_array function returns false.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to change text on last bread’ is closed to new replies.