Last active
June 11, 2020 22:35
-
-
Save dsebao/4a10bf7167c08653432c1d366a15eb3a to your computer and use it in GitHub Desktop.
More readed Shortcode WordPress
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
/* | |
* | |
* More readed Shortcode | |
* | |
*/ | |
//Define the function to add counter visits (its a metabox) | |
function setPostViews($postID) { | |
//This is the key | |
$count_key = 'tm_views'; | |
$count = get_post_meta($postID, $count_key, true); | |
if($count==''){ | |
$count = 0; | |
delete_post_meta($postID, $count_key); | |
add_post_meta($postID, $count_key, '0'); | |
} else{ | |
$count++; | |
update_post_meta($postID, $count_key, $count); | |
} | |
} | |
//The javascript for the tabbed functionality | |
function masleidos_js(){?> | |
<script> | |
$('.tabbed-container').each(function(index, el) { | |
var navtab = $(this).find('#navtab'); | |
var contenttab = $(this).find('#contenttab'); | |
navtab.find('li:eq(0)').addClass('navactivo'); | |
contenttab.find('> div:eq(0)').nextAll().hide(); | |
$('#navtab li',this).click(function(e) { | |
e.preventDefault(); | |
navtab.find('li').removeClass(); | |
contenttab.find('>div').hide(); | |
$(this).addClass('navactivo'); | |
var index = navtab.find('li').index(this); | |
contenttab.find('> div:eq('+index+')').fadeIn(); | |
}); | |
}); | |
</script> | |
<?php } | |
add_action('wp_footer', 'masleidos_js', 100); | |
//Custom CSS if need it | |
function masleidos_css(){?> | |
<style> | |
</style> | |
<?php } | |
add_action('wp_head', 'masleidos_css', 100); | |
/* | |
Add the counter after the content using genesis action, if you are not using Genesis simple put setPostViews($post->ID) in single.php file | |
*/ | |
add_action( 'genesis_after_content', 'shotcounter' ); | |
function shotcounter() { | |
global $post; | |
setPostViews($post->ID); | |
} | |
//Create the shortcode | |
function masleido_func( $atts ){ | |
//Here are the attributes | |
$a = shortcode_atts( array( | |
'id' => 'leidoscontainer', | |
'posts' => 5, | |
'dias' => 60, | |
'cat' => '', | |
'titulo' => 'More readed' | |
), $atts ); | |
wp_reset_query(); | |
//Create the loop | |
$q = array ( | |
'posts_per_page' => $a['posts'], | |
'meta_key' => 'tm_views', | |
'date_query' => array( | |
array( | |
'before' => $a['dias'] .' days ago', | |
'inclusive' => true, | |
), | |
), | |
'orderby' => 'meta_value_num', | |
'order' => 'DESC' | |
); | |
if(!empty($a['cat'])) | |
$q['category_name'] = $a['cat']; | |
$the_query = new WP_Query($q); | |
$theposts = $the_query->posts; | |
//Create the first part of the html | |
if(!empty($theposts)){ | |
$html = '<div id="'.$a['id'].'" class="tabbed-container"><div class="container my-5"><div class="row"><div class="one-half first"><div id="contenttab">'; | |
foreach ($theposts as $key => $value) { | |
$url = get_the_permalink($post->ID); | |
$urlimg = get_the_post_thumbnail_url($value->ID,'portrait'); | |
$html .= '<div> | |
<article> | |
<a href="'.$url.'"><img src="'.$urlimg.'" alt="'.$value->post_title.'" class="w-100"></a> | |
<div class="content-inside"> | |
<h2>'.$value->post_title.'</h2> | |
<p>'.wp_trim_words($value->post_content,30).'</p> | |
<div class="ssfade read-more" style="opacity: 1;"> | |
<a href="'.$url.'"> | |
READ <em> the </em> POST</a></div> | |
</div> | |
</article> | |
</div>'; | |
} | |
$html .= ' </div></div><div class="one-half"><h2>'.$a['titulo'].'</h2><ul id="navtab" class="clearfix">'; | |
//Create the ul nav | |
foreach ($theposts as $key => $value) { | |
$url = get_the_permalink($value->ID); | |
$html .= '<li><a href="'.$url.'"><h3>'.$value->post_title.'</h3></a></li>'; | |
} | |
$html .= '</ul></div><div class="clearfix"></div></div></div></div>'; | |
return $html; | |
} else { | |
return ""; | |
} | |
} | |
add_shortcode( 'masleidos', 'masleido_func' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment