Last active
January 14, 2018 23:07
-
-
Save harshclimate/0f1066980f8081dbc3cf274ebe70f6c9 to your computer and use it in GitHub Desktop.
Append the date to main navigation menu via functions.php
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 | |
// I need to make sure that scripts and styles are able to be recognized on the website. | |
function HC_Scripts() { | |
wp_enqueue_style('style' , get_stylesheet_uri()); | |
wp_enqueue_style('grid' , get_template_directory_uri() .'/grid.css', array(), null, 'all'); | |
wp_enqueue_style('fonts' , 'https://fonts.googleapis.com/css?family=PT+Sans:400,400i|Roboto+Condensed:400,700', false ); | |
wp_enqueue_style('load-fa', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css' ); | |
} | |
add_action( 'wp_enqueue_scripts', 'HC_Scripts' ); | |
?><?php | |
// Here I'm making sure that menus are sorted out and flexible | |
register_nav_menus(array( | |
'primary' => __('Primary Header Menu'), | |
'footer' => __('Primary Footer Menu') | |
)); | |
?><?php | |
// I'm enabling featured images here | |
function addThemeSupport() { | |
add_theme_support('post-thumbnails'); | |
} | |
add_action('after_setup_theme', 'addThemeSupport'); | |
?><?php | |
// Customizing the excerpt | |
// remove the [...] from the excerpt | |
function new_excerpt_more( $more ) { | |
return ' '; | |
} | |
add_filter('excerpt_more', 'new_excerpt_more'); | |
?><?php | |
// Replacing parentheses from category post counts with a span | |
function replace_post_count_parentheses($cat) { | |
$cat = str_replace('(', '<span class="count">', $cat); | |
$cat = str_replace(')', '</span>', $cat); | |
return $cat; | |
} | |
add_filter('wp_list_categories','replace_post_count_parentheses'); | |
?><?php | |
// custom excerpt lengths depending on page | |
function isacustom_excerpt_length($length) { | |
global $post; | |
if ($post->post_type == 'post') | |
return 32; | |
else if ($post->post_type == 'uncategorized') | |
return 20; | |
else if ($post->post_type == 'entries') | |
return 75; | |
else | |
return 80; | |
} | |
add_filter('excerpt_length', 'isacustom_excerpt_length'); | |
?><?php | |
// If more than one page exists, return TRUE. | |
function show_posts_nav() { | |
global $wp_query; | |
return ($wp_query->max_num_pages > 1); | |
} | |
?><?php | |
// removing the P tags from around post content images | |
function fb_unautop_4_img( $content ) { | |
$content = preg_replace('/<p>\\s*?(<a rel=\"attachment.*?><img.*?><\\/a>|<img.*?>)?\\s*<\\/p>/s','<figure>$1</figure>',$content); | |
return $content; | |
} | |
add_filter( 'the_content', 'fb_unautop_4_img', 99 ); | |
?><?php | |
// adding the current menu item when on single page | |
function add_custom_class($classes=array(), $menu_item=false) { | |
if ( is_single() && 'Home' == $menu_item->title && | |
!in_array( 'current-menu-item', $classes ) ) { | |
$classes[] = 'current-menu-item'; | |
} | |
if ( is_category() && 'Archives' == $menu_item->title && | |
!in_array( 'current-menu-item', $classes ) ) { | |
$classes[] = 'current-menu-item'; | |
} | |
return $classes; | |
} | |
add_filter('nav_menu_css_class', 'add_custom_class', 100, 2); | |
?><?php | |
function empty_content($str) { | |
return trim(str_replace(' ','',strip_tags($str))) == ''; | |
} | |
?><?php | |
// making the archive pages have unlimited posts | |
function wpd_testimonials_query( $query ){ | |
if( ! is_admin() | |
&& $query->is_category() | |
&& $query->is_year() | |
&& $query->is_archive() ){ | |
$query->set( 'posts_per_page', -1 ); | |
} | |
} | |
add_action( 'pre_get_posts', 'wpd_testimonials_query' ); | |
?><?php | |
// adding the date to the main menu and linking it to something | |
add_filter('wp_nav_menu_items','add_date', 10, 2); | |
function add_date($items, $args) { | |
if( $args->theme_location == 'primary' ) | |
return $items . '<li class="navdate"><a href="https://www.timeanddate.com/on-this-day/">' . date("l F jS, Y") . '</a></li>'; | |
return $items; | |
} | |
?> |
Oh, and just FYI, I added individual php tags to better differentiate each function. I'm old, what can I say!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I invite anyone to help me revise anything in my functions file. If you see anything that can be more bullet-proof or a better way of writing something, please please please do it!