How do I add functions to a child theme functions.php without breaking the site?

  • Author
    Posts
  • #36252
    Chris Garland

    Hi,

    Just got a child theme sorted which has the following functions.php file:

    <?php
    add_action( ‘wp_enqueue_scripts’, ‘theme_enqueue_styles’ );
    function theme_enqueue_styles() {
    wp_enqueue_style( ‘parent-style’, get_template_directory_uri() . ‘/style.css’ );
    
    }
    
    ?>

    I want to add the following function to this file (it works out how many units of a product are still available);

    **/Product Stock Quantity Check For Course Calendar Page/**
    
    add_shortcode(‘product_stock’, ‘product_stock’);
    function product_stock( $atts ) {
    if ( ! $atts[‘id’] ) {
    return ”;
    }
    $product = get_product($atts[‘id’]);
    return (int) $product->stock;
    }

    How do I add this to the functions.php file??

    I’ve tried this (below) and it just breaks the site.

    <?php
    add_action( ‘wp_enqueue_scripts’, ‘theme_enqueue_styles’ );
    function theme_enqueue_styles() {
    wp_enqueue_style( ‘parent-style’, get_template_directory_uri() . ‘/style.css’ );
    
    }
    
    **/Product Stock Quantity Check For Course Calendar Page/**
    
    add_shortcode(‘product_stock’, ‘product_stock’);
    function product_stock( $atts ) {
    if ( ! $atts[‘id’] ) {
    return ”;
    }
    $product = get_product($atts[‘id’]);
    return (int) $product->stock;
    }
    
    ?>

    Anyone know what I should be doing here?

    • This topic was modified 8 years ago by Zed. Reason: code markup
    #36253
    Chris Garland

    Fixed it!

    “You had your /** backwards again! ;)”

    oops!

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘How do I add functions to a child theme functions.php without breaking the site?’ is closed to new replies.