Add class to index.php

Hello,
I want to add class=‘value’ for non front page pages , I want to use functions.php or other file (without using Jquery or Js)
I don’t want to touch index.php directly.

Thanks in advance

This post feels dangerously close to what we call “Free Work” posts. These are posts that seem to be asking members of the forum to do the poster’s job for them, which is something we frown on. Some indications of a “Free Work” post are:

  • Not providing any code
  • Asking subjective or extremely open-ended questions (“What’s the best way” or “How do I”)
  • Not describing a specific problem to be solved
  • Question seems exploratory, rather than focused

If you’d like to hire someone from the Roots community to work on your project, or to do one-on-one troubleshooting or training, we have a “jobs” section for posting work requests. Otherwise, if you’d like this post re-listed please address the points above.

TBH, in Sage 10 I am interested in how to do things that might have been in controllers before (in sage 9) or the layout template before.

In our team we had a colleague asking about how to add typekit fonts to the theme, unfamiliar with Sage 10 and where the body tag and head were.

Since the main body and HTML layout has been moved to the index.php it might be good to document. IIRC it was done for plugin load or race conditions with WordPress right?

With my colleague we found multiple ways to add the font css, wp_enqueue… @import… and he actually started with editing index.php directly (a thing I think wouldn’t be recommended)

Composers cannot do anything inside of index.php right? it is outside of the view system. However built in WordPress filters can be used right?
That is actually, looking back in Sage 9 code, how the filters for body classes and dom loading were handled was with the body_class filter… here is one from a past site of mine where I updated it to include has-acf-modules type classes for javascript functionality.

/**
 * Add <body> classes
 */
add_filter('body_class', function (array $classes) {
    /** Add page slug if it doesn't exist */
    if (is_single() || is_page() && !is_front_page()) {
        if (!in_array(basename(get_permalink()), $classes)) {
            $classes[] = basename(get_permalink());
        }
    }

    global $post;
    if(is_home()) {
        $post_id = get_option('page_for_posts');
    }elseif($post) {
        $post_id = $post->ID;
    }

    if(isset($post_id)) {
        /** Add a class based on ACF Flex module */
        if (have_rows('sections', $post_id)) {
            $classes[] = 'has-flexible-content';
            while (have_rows('sections', $post_id)): the_row($post_id);
                $template = get_row_layout($post_id);
                $classes[] = 'has-' . str_replace('_', '-', $template);
            endwhile;
        }

        if (has_post_thumbnail($post_id)) {
            $classes[] = 'has-post-thumbnail';
        }
    }

    /** Add class if sidebar is active */
    if (display_sidebar()) {
        $classes[] = 'sidebar-primary';
    }

    /** Clean up class names for custom templates */
    $classes = array_map(function ($class) {
        return preg_replace(['/-blade(-php)?$/', '/^page-template-views/'], '', $class);
    }, $classes);


    return array_filter($classes);
});

Are there any other recomendations around the index.php or the correct ways to affect change outside of the views <?php echo view(app('sage.view'), app('sage.data'))->render(); ?> ?

2 Likes