WordPress common them functions
<?php
/* Function to return current page/post/cat/etc specific title in header - outside wp loop */
function header_title() {
if (!is_home()) {
if (is_category()) { $category = get_category( get_query_var( 'cat' ) );
$current_header_title = $category->cat_name; }
elseif (is_single() || is_page() ) { $current_header_title = the_title(); }
elseif (is_tag()) { $current_header_title = single_tag_title(); }
elseif (is_day()) { $current_header_title = "Archive for "; the_time('F jS, Y'); }
elseif (is_month()) { $current_header_title = "Archive for "; the_time('F, Y'); }
elseif (is_year()) { $current_header_title = "Archive for "; the_time('Y'); }
elseif (is_author()) { $current_header_title = "Author Archive"; }
elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { $current_header_title = "Blog Archives"; }
elseif (is_search()) { $current_header_title = "Search Results for '" . get_search_query() ."'"; }
}
return $current_header_title;
}
?>
get_header() - Includes the header.php template file from your current theme's directory.
wp_link_pages() - Displays page-links for paginated posts (Next page & Previous Page)
edit_post_link() - display link to edit current post from dashboard
Registering Navigation Menu:
<?php
// This registers wp_nav_menu() in two locations.
register_nav_menus( array(
'primary' => __( 'Top primary menu', 'twentyfourteen' ),
'secondary' => __( 'Secondary menu in left sidebar', 'twentyfourteen' ),
) );
?>
get_template_part()
Load a template part (other than header, sidebar, footer) into a template. Makes it easy for a theme to reuse sections of code and an easy way for child themes to replace sections of their parent theme.
<?php get_template_part( $slug, $name ); ?>
<?php get_template_part( 'loop', 'index' ); // Loads (loop-index.php) ?>
<?php get_template_part( 'nav' ); // Navigation bar (nav.php) ?>
Important Link: https://codex.wordpress.org/Function_Reference/get_template_part
Enabling Support for Post Thumbnails:
Themes have to declare their support for post thumbnails (Featured Image) before the interface for assigning these images will appear on the Edit Post and Edit Page screens.
<?php add_theme_support( 'post-thumbnails' ); ?>
set_post_thumbnail_size() - Set the default Featured Image (formerly Post Thumbnail) dimensions. To register additional image sizes for Featured Images use: add_image_size().
<?php set_post_thumbnail_size( $width, $height, $crop ); ?>
Register a new image size:
<?php add_image_size( 'custom-size', 220, 180, true ); // 220 pixels wide by 180 pixels tall, hard crop mode ?>
Registering a Dynamic WordPress Sidebar:
Registering a sidebar or multiple sidebar is fairly simple stuff. Most common approach is to add the register_sidebar( $args ); function with widgets_init() action hook in your theme functions.php file.
This will create a sidebar named "Main Sidebar":
<?php
register_sidebar( array(
'name' => __( 'Main Sidebar', 'theme-slug' ),
'id' => 'sidebar-1',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
) );
?>
Displaying Dynamic Sidebars:
The dynamic_sidebar() function takes a single parameter of $index, which can either be the sidebar’s id or name argument that you have set while registering sidebar.
<?php dynamic_sidebar( 'sidebar-1' ); ?>
<?php
/* more functions here */
?>
<?php
/* Starts: To allow shortcodes in sidebar widgets */
add_filter('widget_text', 'do_shortcode');
/* Ends: To allow shortcodes in sidebar widgets */
/**
* Starts: Register widgetized areas for left and sidebar as per Trsy design requirement
* Using <Div> as a container rather than default <li>
*/
function Trsy_widgets_init() {
// Trsy Left Widget Area
register_sidebar( array(
'name' => __( 'Trsy Left Widget Area - Default', 'coraline' ),
'id' => 'Trsy-left-area-1',
'description' => __( 'The Trsy left widget area', 'coraline' ),
'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
// Trsy Right Widget Area
register_sidebar( array(
'name' => __( 'Trsy Right Widget Area - Default', 'coraline' ),
'id' => 'Trsy-right-area-1',
'description' => __( 'The Trsy right widget area', 'coraline' ),
'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
add_action( 'widgets_init', 'Trsy_widgets_init' );
/** Ends: Register widgetized areas for left and sidebar as per Trsy design requirement */
?>
<?php
/* Starts: Popular Applications Dropdown select menu */
/* Creating Select Dropdown Menu of Posts using specific category (Name: Popular Applications) and
On Select, it will take you to the Option Value URL (getting from custom field)*/
function popular_apps_dropdown( ) {
$args = array('category_name' => 'applications', 'posts_per_page' => '-1' , 'orderby' => 'title', 'order' => 'ASC' );
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) : ?>
<select onchange="if (this.options[this.selectedIndex].value) window.open(this.options[this.selectedIndex].value,'_blank'); " name="applications" class="styled">
<option selected="selected" value=""> Select application</option>
<?php
while($category_posts->have_posts()) :
$category_posts->the_post();
$app_login_link = get_post_meta(get_the_ID(), 'app_login_link', true);
if (!empty($app_login_link)) {
/* List application in dropdown only if has login link */
?>
<option value="<?php echo $app_login_link; ?>"> <?php the_title(); ?></option>
<?php }
endwhile; ?>
</select>
<?php else: ?>
Oops, there are no Apps.
<?php endif; wp_reset_query();
}
/* Ends: Popular Applications Dropdown select menu */
/* Starts: Custom Menu select Dropdown */
/* Get nav menu items based on https://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items */
function menu_dropdown_fx( $atts ) {
extract( shortcode_atts( array(
'title' => '',
'name' => 'dropdown-menu',
), $atts ) );
ob_start();
$menu_obj = get_term_by( 'name', $name, 'nav_menu' );
$menu_id = $menu_obj->term_id;
$menu = wp_get_nav_menu_object($menu_id );
$menu_items = wp_get_nav_menu_items($menu->term_id); ?>
<form id="menu_links_form" action="../">
<!-- <span id="selectapplications" class="select"> Please Select</span> -->
<select name="select_links" class="styled">
<option selected="selected" value="#"> Please Select</option>
<?php
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
echo "<option value=".$menu_item->url."> ".$menu_item->title."</option>";
}
?>
<input type="button" value="" onclick="ob=this.form.select_links;window.open(ob.options[ob.selectedIndex].value)">
</form>
<?php
$output_string=ob_get_contents();
ob_end_clean();
return $output_string;
}
add_shortcode( 'menu_dropdown', 'menu_dropdown_fx' );
/* Ends: Custom Menu select Dropdown */
<?php
/* Using WordPress Navigation menu as select dropdown */
/* Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu) */
// This code based on wp_nav_menu's code to get Menu ID from menu slug
function nav_dropdown() {
$menu_name = 'site-navigation-menu';
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<select id="$menu_name" name="applications" class="styled">';
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
// $menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
$menu_list .= '<option value="' . $url . '">' . $title . '</option>';
}
echo $menu_list .= '</select>';
} else {
echo $menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
}
// $menu_list now ready to output
}
?>