admin管理员组

文章数量:1130349

I am New To Wordpress Plugin Development. I want to Create Short Code using Custom Post Type Method

function baztag_func( $atts, $content = "" ) {
    return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );

I am New To Wordpress Plugin Development. I want to Create Short Code using Custom Post Type Method

function baztag_func( $atts, $content = "" ) {
    return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
Share Improve this question edited Oct 26, 2018 at 6:28 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Oct 26, 2018 at 4:53 RagupathiRagupathi 1012 bronze badges 9
  • Sorry! But not getting you. What exactly want you ? – Pratik Patel Commented Oct 26, 2018 at 6:09
  • [contact-form-7 id="11" title="Contact form 1"] – Ragupathi Commented Oct 26, 2018 at 6:28
  • create short code like this – Ragupathi Commented Oct 26, 2018 at 6:28
  • but i want use cusome post type – Ragupathi Commented Oct 26, 2018 at 6:29
  • You want custom post type posts listing ,using shortcode right ? – Pratik Patel Commented Oct 26, 2018 at 6:30
 |  Show 4 more comments

1 Answer 1

Reset to default 1

Try this

    /**
     * Register all shortcodes
     *
     * @return null
     */
    function register_shortcodes() {
        add_shortcode( 'listing', 'shortcode_mostra_produtos' );
    }
    add_action( 'init', 'register_shortcodes' );

    /**
     * Produtos Shortcode Callback
     * 
     * @param Array $atts
     *
     * @return string
     */
    function shortcode_mostra_produtos( $atts ) {
        global $wp_query,
            $post;

        $atts = shortcode_atts( array(
            'type' => ''
        ), $atts );

        $loop = new WP_Query( array(
            'posts_per_page'    => '-1',
            'post_type'         => $atts['type']
        ) );

        if( ! $loop->have_posts() ) {
            return false;
        }

        while( $loop->have_posts() ) {
            $loop->the_post();
            echo the_title();
// DO YOUR HTML STUFF HERE
        }

        wp_reset_postdata();
    }

Use short-code like:

[listing type="YOUR CUSTOM POST TYPE HERE"]

Please try and let me know if any query

Thanks!

I am New To Wordpress Plugin Development. I want to Create Short Code using Custom Post Type Method

function baztag_func( $atts, $content = "" ) {
    return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );

I am New To Wordpress Plugin Development. I want to Create Short Code using Custom Post Type Method

function baztag_func( $atts, $content = "" ) {
    return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
Share Improve this question edited Oct 26, 2018 at 6:28 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Oct 26, 2018 at 4:53 RagupathiRagupathi 1012 bronze badges 9
  • Sorry! But not getting you. What exactly want you ? – Pratik Patel Commented Oct 26, 2018 at 6:09
  • [contact-form-7 id="11" title="Contact form 1"] – Ragupathi Commented Oct 26, 2018 at 6:28
  • create short code like this – Ragupathi Commented Oct 26, 2018 at 6:28
  • but i want use cusome post type – Ragupathi Commented Oct 26, 2018 at 6:29
  • You want custom post type posts listing ,using shortcode right ? – Pratik Patel Commented Oct 26, 2018 at 6:30
 |  Show 4 more comments

1 Answer 1

Reset to default 1

Try this

    /**
     * Register all shortcodes
     *
     * @return null
     */
    function register_shortcodes() {
        add_shortcode( 'listing', 'shortcode_mostra_produtos' );
    }
    add_action( 'init', 'register_shortcodes' );

    /**
     * Produtos Shortcode Callback
     * 
     * @param Array $atts
     *
     * @return string
     */
    function shortcode_mostra_produtos( $atts ) {
        global $wp_query,
            $post;

        $atts = shortcode_atts( array(
            'type' => ''
        ), $atts );

        $loop = new WP_Query( array(
            'posts_per_page'    => '-1',
            'post_type'         => $atts['type']
        ) );

        if( ! $loop->have_posts() ) {
            return false;
        }

        while( $loop->have_posts() ) {
            $loop->the_post();
            echo the_title();
// DO YOUR HTML STUFF HERE
        }

        wp_reset_postdata();
    }

Use short-code like:

[listing type="YOUR CUSTOM POST TYPE HERE"]

Please try and let me know if any query

Thanks!

本文标签: pluginsHow to Create Short Code Using Custom Post type