Skip to content

Instantly share code, notes, and snippets.

View mdjwel's full-sized avatar
🎯
Focusing

Eh Jewel mdjwel

🎯
Focusing
View GitHub Profile
@mdjwel
mdjwel / social_networks_share_urls.php
Last active July 25, 2018 18:01
A collection of social site's share URL
<?php
function chaoz_social_share() { ?>
<a href="https://facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>"><i class="social_facebook"></i></a>
<a href="https://twitter.com/intent/tweet?text=<?php the_permalink(); ?>"><i class="social_twitter"></i></a>
<a href="https://www.pinterest.com/pin/create/button/?url=<?php the_permalink() ?>"><i class="social_pinterest"></i></a>
<a href="https://plus.google.com/share?url=<?php the_permalink() ?>"><i class="social_google_plus"></i></a>
<a href="https://www.linkedin.com/shareArticle?mini=true&url=<?php the_permalink() ?>"><i class="social_linkedin"></i></a>
<?php
}
@mdjwel
mdjwel / related_posts.php
Last active December 24, 2018 10:37
Get related posts in single post page in WordPress.
@mdjwel
mdjwel / get_post_views.php
Created May 28, 2018 19:49
Get post views count of WordPress single post (without plugin)
<?php
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return ($count/2).' Views';
@mdjwel
mdjwel / the_first_category_name.php
Last active January 27, 2019 19:47
Get the first category name and link in WordPress post's while loop.
<?php
// Get the 1ast category link
function Chaoz_first_category_link() {
$cats = get_the_terms(get_the_ID(), 'category');
$cat = is_array($cats) ? get_category_link($cats[0]->term_id) : '';
echo esc_url($cat);
}
// Get the first category link
function Chaoz_first_category_link() {
@mdjwel
mdjwel / category_array.php
Created May 22, 2018 08:51
Get associative array (cat_slug => cat_name) in WordPress
<?php
// Category array
function chaoz_cat_array($term='portfolio_cat') {
$cats = get_terms(array(
'taxonomy' => $term,
'hide_empty' => true
));
$cat_array = array();
$cat_array['all'] = esc_html__('All', 'gullu');
foreach ($cats as $cat) {
@mdjwel
mdjwel / comment_count.php
Created May 13, 2018 13:50
Get the post comment count text based the comments have in a WordPress post loop.
<?php
// Get comment count text
function prefix_comment_count($post_id) {
$comments_number = get_comments_number($post_id);
if($comments_number==0) {
$comment_text = esc_html__('No comment', 'chaoz');
}elseif($comments_number == 1) {
$comment_text = esc_html__('One comment', 'chaoz');
}elseif($comments_number > 1) {
$comment_text = $comments_number.esc_html__(' Comments', 'chaoz');
@mdjwel
mdjwel / wp_day_link.php
Last active August 10, 2018 12:36
Get the day link in WordPress post loop.
@mdjwel
mdjwel / limit_latter.php
Last active May 13, 2018 13:38
Limit latter (character) length function
<?php
// Limit latter
function prefix_limit_latter($string, $limit_length) {
if (strlen($string) > $limit_length) {
echo substr($string, 0, $limit_length) . '...';
}
else {
echo $string;
}
}
@mdjwel
mdjwel / get_image_alt.php
Last active May 20, 2018 10:36
Get image alt text in WordPress
<?php
// Get image alt text
function aproch_image_alt($image_id) {
$image_title = get_the_title($image_id);
$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true);
echo !empty($image_alt) ? $image_alt : $image_title;
}
@mdjwel
mdjwel / Icon_array_maker.php
Created April 16, 2018 13:57
Make associative icon array ('icon_class'=> 'Icon Name' paired) from css classes indexed array.
<?php
function chaoz_icon_array($k) {
$v = array();
foreach ($k as $kv) {
$kv = str_replace('-', ' ', $kv);
$kv = str_replace('icon', '', $kv);
$v[] = array_push($v, ucwords($kv));
}
foreach($v as $key => $value) if($key&1) unset($v[$key]);
return array_combine($k, $v);