Skip to content

Instantly share code, notes, and snippets.

@richjava
Last active March 7, 2016 07:30
Show Gist options
  • Save richjava/5f0f9296bfbef6457b72 to your computer and use it in GitHub Desktop.
Save richjava/5f0f9296bfbef6457b72 to your computer and use it in GitHub Desktop.
Wordpress Development task 2: Basic one-page theme with separate pages
<?php
/**
* Adds functionality to the theme of the site.
* @see https://codex.wordpress.org/Functions_File_Explained
*/
/**
* Enqueue scripts and styles.
*/
function enqueue_scripts() {
wp_enqueue_style('style', get_stylesheet_uri());
}
add_action( 'wp_enqueue_scripts', 'enqueue_scripts' );
/**
* Register navigation menus so they can be configured in the Admin Dashboard.
*/
register_nav_menus(array(
'primary' => __( 'Primary Menu'),
'footer' => __( 'Footer Menu'),
));
<?php
/**
* The header template file.
* Displays all of the head element and everything up until the "site-content" div.
*/
?><!DOCTYPE html>
<html <?php language_attributes(); ?> class="no-js">
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width">
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>">
<!--[if lt IE 9]>
<script src="<?php echo esc_url(get_template_directory_uri()); ?>/js/html5.js"></script>
<![endif]-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div id = "content" class = "site-content">
<header>
Header content goes here
<nav class = "site-nav">
<?php
//custom menu (not using wp_nav_menu())
$menu_name = 'primary';
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 = '<ul id="menu-' . $menu_name . '">';
foreach ((array) $menu_items as $key => $menu_item) {
$css_class = '';
if ($menu_item->title === 'Home' && $pagename === '' || lcfirst($menu_item->title) === $pagename) {
//is home
$css_class = 'class = "active"';
}
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= '<li ' . $css_class . '><a href="' . $url . '">' . $title . '</a></li>';
}
$menu_list .= '</ul>';
} else {
$menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
}
echo $menu_list;
?>
</nav>
</header>
<script>
//javascript for one page menu functionality
$(window).on('hashchange', function () {
setSelected();
});
$(document).ready(function () {
if (location.hash) {
setSelected();
}
});
var setSelected = function () {
$('#menu-primary li').removeClass('active');
$('a[href$="' + location.hash + '"]').parent().addClass('active');
};
</script>
<?php
/**
* Template Name: Blog
* NOTE: Because of a Wordpress anti-pattern, home.php is used for the the Blog page
* rather than using slugs or id(i.e. "page-blog", "page-{id}").
* @see https://make.wordpress.org/themes/2014/06/28/correct-handling-of-static-front-page-and-custom-blog-posts-index-template/
*/
get_header();
if (have_posts()) :
while (have_posts()) : the_post();
?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php
else :
echo '<p>No content found</p>';
endif;
get_footer();
?>
<?php
/**
* Template Name: Home
* Each post is a child page of Home page. Uses "Onepager" plugin.
*/
get_header();
if (have_posts()) :
while (have_posts()) : the_post();
?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php
else :
echo '<p>No content found</p>';
endif;
get_footer();
?>
<?php
/**
* The page template file.
*/
get_header();
if (have_posts()) :
while (have_posts()) : the_post();
the_content();
endwhile;
else :
echo '<p>No content found</p>';
endif;
get_footer();
?>
/*
Created on : Mar 2, 2016, 10:41:30 AM
Author : richard_lovell
*/
header, footer{
background:#e9e9e9;
}
ul li.active a{
background-color:#006;
color:#fff;
text-decoration: none;
}
section{
height:700px;
}
#features{
background-color: gray;
color:white;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment