Home > Net >  How do I display data from plugin using shortcode
How do I display data from plugin using shortcode

Time:11-06

I am having a problem in displaying HTML rendered data inside a plugin. I have create custom post type plugin now i want to render the html codes inside plugin function and display it in a post / page using shortcode [displayFaqs].

Creating Custom Post Type

add_action( 'init', 'create_faqs' );

    function create_faqs() {
        register_post_type( 'faqs',
            array(
                'labels' => array(
                    'name' => 'FAQs',
                    'singular_name' => 'FAQ',
                    'add_new' => 'Add New',
                    'add_new_item' => 'Add New Faq',
                    'edit' => 'Edit',
                    'edit_item' => 'Edit Faq',
                    'new_item' => 'New Faq',
                    'view' => 'View',
                    'view_item' => 'View Faq',
                    'search_items' => 'Search FAQs',
                    'not_found' => 'No FAQs found',
                    'not_found_in_trash' => 'No faqs found in Trash',
                    'parent' => 'Parent Faq'
                ),
                 
     
                'public' => true,
                'menu_position' => 15,
                // 'menu_icon' => 'dashicons-editor-help',
                'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
                'taxonomies' => array( '' ),
                'menu_icon' => 'dashicons-editor-help',
                'has_archive' => true
            )
        );
    }

Displaying function within plugin

 function displayFaqs() {
        
        $html .= ' Faqs information';
          return $html;

    }
add_shortcode('customshortcode','displayFaqs');

Now, shortcode display on pages or posts

[displayFaqs]

I am expecting to display faqs posts in a template/pages/posts using shortcode [displayFaqs], but the above code is looping this shortcode [displayFaqs] instade of displaying faq posts.

CodePudding user response:

You have to loop through the faqs posts to render it in other pages or post with shortcode.

try this and edit according to your structure.

<?php

function displayFaqs() {
        
    
    $arg = array(
        'post_type' => 'faqs',
        'posts_per_page' => '-1',
        'post_status' => 'publish'
    );
    // The Query
    $the_query = new WP_Query( $args );

    ob_start();
    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();

    $contents = ob_get_contents();
    ob_end_clean();
      return $contents;

}
add_shortcode('customshortcode','displayFaqs');

Reference : https://developer.wordpress.org/reference/classes/wp_query/

  • Related