Skip to content

Instantly share code, notes, and snippets.

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 imlinus/5f22fb61ba85f73e8f056b3259ca06e8 to your computer and use it in GitHub Desktop.
Save imlinus/5f22fb61ba85f73e8f056b3259ca06e8 to your computer and use it in GitHub Desktop.
WordPress Custom Post Type with ACF via shortcode
<?php
function price_shortcode($id) {
if($id != '') {
$post_id = $id[0];
$html = '';
global $wpdb;
$args = array(
'post_type' => 'price_list_init',
'name' => $id
);
$wp_posts = new WP_Query($args);
$posts = $wp_posts->posts;
while ( $wp_posts->have_posts() ) : $wp_posts->the_post();
$html = '<div class="' . create_slug(get_field('pl_title')) . ' price-table">';
$html .= '<div class="header">';
$html .= '<span class="price">' . get_field('pl_price') . '</span>';
$html .= '<span class="name">' . get_field('pl_title') . '</span>';
$html .= '</div>';
$html .= get_field('pl_list');
$html .= '<button>';
$html .= '<a href="' . get_field('pl_button_url') . '">';
$html .= get_field('pl_button_text');
$html .= '</a>';
$html .= '</button>';
$html .= '</div>';
endwhile;
wp_reset_query();
return html_entity_decode($html);
wp_reset_postdata();
} else {
return 'Please enter correct shortcode';
}
}
add_shortcode('price', 'price_shortcode');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment