shortcodes.php 3.48 KB
<?php 
function promos()
{
    $custom_args = array(
        'post_type' => 'promo',
        'posts_per_page' => 3,
        'paged' => 1,
    );
    $custom_query = new \WP_Query($custom_args);
      
    ob_start(); 
   $uniqid = uniqid();
    if ($custom_query->have_posts()):?>
    <div class="carousel">
    <div id="<?php echo  $uniqid; ?>" class="promo-carousel carousel-items">
        <div class='swiper-wrapper'>
                <?php while ($custom_query->have_posts()): $custom_query->the_post();
                    $id = get_the_ID();
                    $post = get_post($id);
                    $link = get_permalink($id);
                    if ($custom_link = get_post_meta($id, 'link', true)) {
                        $link = $custom_link;
                    }
                    $link_text = "Read More...";
                    if ($custom_link_text = get_post_meta($id, 'link_text', true)) {
                        $link_text = $custom_link_text;
                    }
                    $text = strip_shortcodes($post->post_content);
                    $text = apply_filters( 'the_content', $text );
                    $text = str_replace(']]>', ']]&gt;', $text);

                    $excerpt_length = apply_filters( 'excerpt_length', 40 );
                    $text = wp_trim_words( $text, $excerpt_length, ' ...' );
                    ?>
                         <div class="swiper-slide container">  
                            <div class="promo row align-items-center">
                                <div class="col-lg-4 col-md-12">
                                <div class="promo-img">
                                    <?php echo get_the_post_thumbnail($id, 'full' ); ?>
                                    <div class="promo-img-over"> </div>
                                </div>    
                                </div>
                                <div  class="promo_content col-lg-8 col-md-12">
                                    <?php  echo '<h3>'.$post->post_title.'</h3><p>'. $text.'</p>'; ?>
                                    <a class="promo-link" target="_blank" href="<?php echo $link; ?>" title="<?php the_title_attribute(); ?>"><?php echo  $link_text;?></a>
                                </div>
                            </div>
                        </div>
                <?php endwhile; ?> 
                </div>
                        <div class="swiper-pagination"></div>
                        <div class="swiper-button-prev" data-id="<?= $uniqid; ?>"></div>
                        <div class="swiper-button-next" data-id="<?= $uniqid; ?>"></div>
               </div>        
               </div>  
    <?php endif; ?>
    <?php  wp_reset_query(); ?>
   <?php $output = ob_get_clean();
 
     return $output;
}

add_shortcode( 'promos', 'promos' );



function share_this($atts)
{
    $atts = shortcode_atts(array(
        'post_id' => '',
    ), $atts, 'current-cover-articles');
    $post = get_post($atts['post_id']);
    ob_start();?>

<div class="custom-addtoany">
	<div class="shareIcons" id="shareRoundIcons<?php echo $post->post_name; ?>"></div>
</div>		
<script>
	jQuery(document).ready(function ($) {
		$("#shareRoundIcons<?php echo $post->post_name; ?>").jsSocials({
			url: "<?php echo site_url() . '/' . $post->post_name; ?>",
			text: ": <?php echo get_the_title($post->ID); ?>",
			showLabel: false,
			showCount: false,
			shares: ["facebook",  "twitter", "linkedin" ]
		});
	});
</script>
<?php
return ob_get_clean();
}

add_shortcode('share-this', 'share_this');