• Resolved opicron

    (@opicron)


    I was wondering if we are able to use the arguments of the shortcode in our custom templates. For example if we set titletag=”h3″ can I reach $args[‘titletag’] in my custom template tile?

    This would be very convenient for making template tiles more customizable instead of fully hardcoded.

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

    (@opicron)

    Ok, I adjusted your plugin by adding the $args argument to the custom template call in line 2027 of latest_post_shortcode.php

    if ( $use_custom_markup ) {
    					echo apply_filters( 'lps_filter_use_custom_tile_markup', $tile_pattern, $post, $args ); // WPCS: XSS OK.

    Then in my custom template code I adjusted the call to load the template likewise:

    my_get_template_part( 'template-parts/custom-tile-2', $args, null );

    By using the following function found on StackExchange:

    /**
     * Like get_template_part() put lets you pass args to the template file
     * Args are available in the tempalte as $template_args array
     * @param string filepart
     * @param mixed wp_args style argument list
     */
    function my_get_template_part( $file, $template_args = array(), $cache_args = array() ) {
        $template_args = wp_parse_args( $template_args );
        $cache_args = wp_parse_args( $cache_args );
        if ( $cache_args ) {
            foreach ( $template_args as $key => $value ) {
                if ( is_scalar( $value ) || is_array( $value ) ) {
                    $cache_args[$key] = $value;
                } else if ( is_object( $value ) && method_exists( $value, 'get_id' ) ) {
                    $cache_args[$key] = call_user_method( 'get_id', $value );
                }
            }
            if ( ( $cache = wp_cache_get( $file, serialize( $cache_args ) ) ) !== false ) {
                if ( ! empty( $template_args['return'] ) )
                    return $cache;
                echo $cache;
                return;
            }
        }
        $file_handle = $file;
        do_action( 'start_operation', 'hm_template_part::' . $file_handle );
        if ( file_exists( get_stylesheet_directory() . '/' . $file . '.php' ) )
            $file = get_stylesheet_directory() . '/' . $file . '.php';
        elseif ( file_exists( get_template_directory() . '/' . $file . '.php' ) )
            $file = get_template_directory() . '/' . $file . '.php';
        ob_start();
        $return = require( $file );
        $data = ob_get_clean();
        do_action( 'end_operation', 'hm_template_part::' . $file_handle );
        if ( $cache_args ) {
            wp_cache_set( $file, $data, serialize( $cache_args ), 3600 );
        }
        if ( ! empty( $template_args['return'] ) )
            if ( $return === false )
                return false;
            else
                return $data;
        echo $data;
    }
    Plugin Author Iulia Cazan

    (@iulia-cazan)

    Hi,

    This is useful.
    I will add in the next release the arguments in the filter.

    Regards,
    Iulia

    Plugin Author Iulia Cazan

    (@iulia-cazan)

    Hi,

    I just released 9.3 and included the arguments in the filter. Please upgrade.
    I will mark this as resolved now.

    Regards,
    Iulia

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Use arguments in custom template’ is closed to new replies.