Last active
December 27, 2024 11:56
-
-
Save SitesByYogi/c93014c33073af4c398cf7d4f51545d9 to your computer and use it in GitHub Desktop.
Shortcode to Display Full Blog Post by Post ID Without Featured Image or Title
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Shortcode to Display Full Blog Post by Post ID Without Featured Image or Title | |
function display_full_post_no_image($atts) { | |
// Shortcode Attributes | |
$atts = shortcode_atts(array( | |
'id' => '', // Default: no post ID | |
), $atts, 'full_post_no_image'); | |
// Validate the Post ID | |
$post_id = intval($atts['id']); | |
if (!$post_id || get_post_status($post_id) !== 'publish') { | |
return '<p>No valid post found.</p>'; | |
} | |
// Fetch the Post | |
$post = get_post($post_id); | |
if (!$post) { | |
return '<p>No valid post found.</p>'; | |
} | |
// Get Post Content | |
$post_content = apply_filters('the_content', $post->post_content); | |
// Build the Output (exclude the title) | |
$output = '<div class="custom-blog-post" style="text-align:center;">'; | |
$output .= '<div style="text-align:left; max-width:800px; margin:auto;">' . $post_content . '</div>'; | |
$output .= '</div>'; | |
return $output; | |
} | |
add_shortcode('full_post_no_image', 'display_full_post_no_image'); | |
// WooCommerce Product Section by IDs Shortcode | |
function custom_woocommerce_products_by_ids_shortcode($atts) { | |
// Extract attributes | |
$atts = shortcode_atts(array( | |
'ids' => '', // Comma-separated product IDs | |
), $atts, 'product_section'); | |
// Convert IDs to an array | |
$product_ids = array_filter(array_map('trim', explode(',', $atts['ids']))); | |
if (empty($product_ids)) { | |
return '<p>No products found. Please provide valid product IDs.</p>'; | |
} | |
// Query for products | |
$args = array( | |
'post_type' => 'product', | |
'post__in' => $product_ids, | |
'posts_per_page' => -1, | |
'orderby' => 'post__in', // Maintain the order of IDs | |
); | |
$products = new WP_Query($args); | |
// Build the output | |
if ($products->have_posts()) { | |
ob_start(); | |
echo '<div class="custom-product-section">'; | |
while ($products->have_posts()) { | |
$products->the_post(); | |
global $product; | |
?> | |
<div class="product-item"> | |
<a href="<?php the_permalink(); ?>"> | |
<?php echo $product->get_image(); // Display product image ?> | |
<h3><?php the_title(); ?></h3> | |
</a> | |
<p class="price"><?php echo $product->get_price_html(); // Display product price ?></p> | |
<a href="<?php echo esc_url($product->add_to_cart_url()); ?>" class="button add-to-cart"><?php echo esc_html($product->add_to_cart_text()); ?></a> | |
</div> | |
<?php | |
} | |
echo '</div>'; | |
wp_reset_postdata(); | |
return ob_get_clean(); | |
} | |
return '<p>No products found for the provided IDs.</p>'; | |
} | |
add_shortcode('product_section', 'custom_woocommerce_products_by_ids_shortcode'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment