Advise on custom Shortcode

Looking to create custom shortcode for eg [menu sectionid="123"] and place it anywhere on the page so I can show the menu sectionid 123

Check the code here:
http://pastehere.xyz/28912/

Support access enabled, for more info check the chat transcripts.

  • J2O

    Thanks guys.. to clarify I'm trying to create a shortcode from custom post type 'menu' with taxonomy 'section' which has identifiers for each section called 'tag_ID'.

    Basically I want to be able to write a shortcode in content like [forqy-menu SectionID="134"] which shows that section content within the content.

    Theme is 'Rib Eye' and code I have currently which doesn't work is: http://pastehere.xyz/28920/
    Not getting any errors or anything...

    Thanks again
    Jay

  • Mahlamusa

    Hello J2O,

    I hope you are doing great today and I would like to thank you for contacting us. I have gone through your code and found a few issues that you need to address.

    Firstly, the 'tag_ID' that you would like to use is supposed to be part of the main array in the args and should be used as follows:

    $loop = new WP_Query( array(
            'posts_per_page'    => 200,
            'post_type'         => 'menu',
          	'tag_id' => 134, //get tag_id from shortcode atts
    //etc

    Then you should remove the second array in the 'tax_query' so you will have it like below since you already have the 'tag_id' as an argument in the post query.

    'tax_query' => array(
          		'relation' => 'AND',
          		array(
          			'taxonomy' => 'section',
          			'field'    => 'slug',
          			'terms'    => 'section',
          		),
        ),

    Now the code that outputs something should not use 'echo' but instead return the value, and wordpress will output the returned value in place of the short code. I quote from the WordPress Codex Page:

    The return value of a shortcode handler function is inserted into the post content output in place of the shortcode macro. Remember to use return and not echo - anything that is echoed will be output to the browser, but it won't appear in the correct place on the page.

    Your code will output something if you use the 'echo' statement in a shortcode, but as quoted above it my output in the wrong place and you may think its not working. So, that part of your code should be as follows:

    $output = ""
    while( $loop->have_posts() ) {
            $loop->the_post();
            $output .= get_the_title(); //the_title() will echo the title, get_the_title() will return the value
        }

    After doing all this, your code should look like the code below. I have tested it and it worked on my case.

    function register_shortcodes() {
        add_shortcode( 'forqy-menu', 'shortcode_forqy_menu' );
    }
    add_action( 'init', 'register_shortcodes' );
    
    function shortcode_forqy_menu( $atts ) {
        global $wp_query, $post;
        $atts = shortcode_atts( array(
            'SectionID' => ''
        ), $atts );
    
        $loop = new WP_Query( array(
            'posts_per_page'    => 200,
            'post_type'         => 'menu',
            'orderby'           => 'menu_order title',
            'order'             => 'ASC',
          	'tag_id' => $atts['SectionID'],
            'tax_query' => array(
          		'relation' => 'AND',
          		array(
          	 		'taxonomy' => 'section',
          	 		'field'    => 'slug',
          	 		'terms'    => 'section',
          		),
          	),
        ) );
    
        if( ! $loop->have_posts() ) {
            return false;
        }
    
        $output = '';
        while( $loop->have_posts() ) {
            $loop->the_post();
            $output .= get_the_title();
        }
        wp_reset_postdata();
        return $output;
    }

    I hope this helps. Please let us know if you need more help with regards to this.

    Cheers,
    Mahlamusa