• Resolved mnatseah624

    (@mnatseah624)


    Hello GP users
    I had a nice piece of JS to display my Popular Posts (aka stickies). It was inserted in functions.php. But then GP updated, and voila it wasn’t working anymore. What is the best way, pls, to reapply this piece of JS. Is it thru a plugin or a child theme?

    For plugins to add extra code, I’ve already got

      Simple CSS (thanks, Tom)
      Code Snippets
      Custom CSS-JS-PHP
      Simple Custom CSS & JS

    Which of these would be the best plugin for the job?
    Thanks for any advice…

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi there,

    So the same code you added before is still in the child theme’s function.php but it’s no longer working?

    The latest update 2.4.2 only includes a couple of block editor changes so it shouldn’t cause issues like this.

    Usually js can go into WordPress’ default wp_footer hook:
    https://docs.generatepress.com/article/wp_footer/
    https://docs.generatepress.com/article/using-hooks/

    Thread Starter mnatseah624

    (@mnatseah624)

    Hello Leo
    I didn’t have a child theme. I just inserted it straight into functions.php.
    Is a child theme the best way to do it?
    I tried it with Code Snippets, but it’s not working there.
    Btw this was the code…

    function wpb_latest_sticky() { 
     
    /* Get all sticky posts */
    $sticky = get_option( 'sticky_posts' );
     
    /* Sort the stickies with the newest ones at the top */
    rsort( $sticky );
     
    /* Get the 5 newest stickies (change 5 for a different number) */
    $sticky = array_slice( $sticky, 0, 5 );
     
    /* Query sticky posts */
    $the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
    // The Loop
    if ( $the_query->have_posts() ) {
        $return .= '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $return .= '<li><a href="' .get_permalink(). '" title="'  . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>';
             
        }
        $return .= '</ul>';
         
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
     
    return $return; 
     
    } 
    add_shortcode('latest_stickies', 'wpb_latest_sticky');
    add_filter('widget_text', 'do_shortcode');
    • This reply was modified 4 years, 11 months ago by mnatseah624.

    If you inserted into the parent theme’s function.php then it would’ve been erased during the update – that’s why it’s never a good idea to modify the theme’s core files.

    I’d say child theme would be your best bet:
    https://docs.generatepress.com/article/using-child-theme/

    Thread Starter mnatseah624

    (@mnatseah624)

    Hello @leohsiang
    Thanks very much for your advice.
    Happy Easter to you.

    No problem ??

    You as well!

    Thread Starter mnatseah624

    (@mnatseah624)

    Hello @leohsiang and anyone else who can answer
    So I just made a Generate Press Child, my first child theme. When I activated it, I found it deactivated GP. I also found that it, naturally, appeared without all the additional CSS code which I had added. May I just ask….
    Is this how it’s supposed to work? Should GP Child now be the only activated theme? And should I add my CSS code there?
    A yes or no will do.
    Many thanks

    Should GP Child now be the only activated theme?

    Yes

    And should I add my CSS code there?

    My recommendation would be to move Additional CSS from the parent theme to the child theme’s style.css.

    Thread Starter mnatseah624

    (@mnatseah624)

    Hello @leohsiang, I put it in style.css. Works a treat.
    Many thanks

    • This reply was modified 4 years, 11 months ago by mnatseah624.

    No problem ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Applying JS to functions.php…..or?’ is closed to new replies.