Skip to content

Instantly share code, notes, and snippets.

@pixelbart
Last active November 15, 2019 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelbart/31474e452d8f533f20887ba3523b59a8 to your computer and use it in GitHub Desktop.
Save pixelbart/31474e452d8f533f20887ba3523b59a8 to your computer and use it in GitHub Desktop.
In this example you will learn how to add Helpful to an existing Post Type (here FAQ), including its own heading.
<?php
/**
* In this example you will learn how to add Helpful to an
* existing Post Type (here FAQ), including its own heading.
*
* @package Helpful Example
* @author Pixelbart
*/
$args = array(
'post_type' => 'faq', /* custom post type */
'post_status' => 'publish',
'fields' => 'ids', /* only retrieves ids, makes queries faster */
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
if ( $query->found_posts ) {
foreach ( $query->posts as $post_id ) :
/**
* Some variables
*/
$post = get_post( $post_id );
$title = sprintf( 'How do you like %s?', $post->post_title );
$content = apply_filters( 'the_content', $post->post_content );
/**
* Here the Helpful Shortcode with its own heading and Post-ID is used.
*/
$shortcode = "[helpful heading=\"{$title}\" post_id=\"{$post->ID}\"]";
echo '<div class="faq-item">';
echo '<h2 class="faq-heading">' . $post->post_title . '</h2>';
echo '<div class="faq-content">';
echo $content . '<br>';
echo do_shortcode( $shortcode ); /* echo helpful shortcode */
echo '</div><!-- .faq-content -->';
echo '</div><!-- .faq-item -->';
endforeach;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment