Search code examples
phpwordpressdivi-theme

is_singular doesn't works


I wrote this function to customize a divi theme module through a specific theme hook, this works but I want to apply this only to the custom post type strutture, but my if statement is always returning true, so the module is customized on all the pages with that module and not only to the struttura custom post type. Any suggestions?

add_filter('et_pb_module_shortcode_attributes', 'galleria_divi_acf', 20, 3);
function galleria_divi_acf($props, $atts, $slug) {
    $gallery_module_slugs = array('et_pb_gallery');
    if (!in_array($slug, $gallery_module_slugs)) {
        return $props;
    }
    if (is_singular( 'struttura' )) {
    $props['gallery_ids'] = get_field('galleria_struttura', false, false);
        return $props;
    }
}

Solution

  • I was missing else return $props; at the end of the statement. This is working code to populate a divi gallery module with an acf gallery field type, very usefull for custom post types single pages.

    add_filter('et_pb_module_shortcode_attributes', 'galleria_divi_acf', 20, 3);
    function galleria_divi_acf($props, $atts, $slug) {
        $gallery_module_slugs = array('et_pb_gallery');
        if (!in_array($slug, $gallery_module_slugs)) {
            return $props;
        }
        if ( 'struttura' == get_post_type() ) {
        $props['gallery_ids'] = get_field('galleria_struttura', false, false);
            return $props;
        }
        else return $props;
    }