Skip to content

Instantly share code, notes, and snippets.

@tomblanchard
Last active December 25, 2015 11:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomblanchard/6966999 to your computer and use it in GitHub Desktop.
Save tomblanchard/6966999 to your computer and use it in GitHub Desktop.
Regular WP snippets I use.
/***********************************************************************************
THEME NAME: Parent Theme - Child
***********************************************************************************
Template: parent-theme
Author: Tom Blanchard
Author URI: http://tomblanchard.co.uk
Version: 1.0
**********************************************************************************/
@import "../parent-theme/style.css";
<?php
/**
* Custom images sizes, hard cropped and soft.
*/
add_image_size('200-hard', 200, 200, true);
add_image_size('200-soft', 200, 200);
/**
* Theme directory URIs.
*
* 1. Parent theme.
* 2. Child theme.
*/
get_template_directory_uri(); /* [1] */
get_stylesheet_directory_uri(); /* [2] */
/**
* Enqueue custom CSS.
*/
wp_enqueue_style('custom-css', 'custom.css', array());
/**
* Remove size attributes from the "the_post_thumbnail()".
*/
function remove_thumb_size_attribute($html) {
$html = preg_replace('/(width|height)="\d*"\s/', "", $html);
return $html;
}
add_filter('post_thumbnail_html', 'remove_thumb_size_attribute', 10);
add_filter('image_send_to_editor', 'remove_thumb_size_attribute', 10);
/**
* "/?s=" to "/search/" in URLs.
*/
function change_search_url_rewrite() {
if (is_search() && ! empty($_GET['s'])) {
wp_redirect(home_url("/search/") . urlencode(get_query_var('s')));
exit();
}
}
add_action('template_redirect', 'change_search_url_rewrite');
/**
* Truncate string with dynamic string source / character limit, usage:
*
echo text_truncate('Text here.', 1);
*/
function text_truncate($text, $limit) {
$excerpt = explode(' ', $text, $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`[[^]]*]`','',$excerpt);
return $excerpt;
}
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_ID(); ?>
<?php post_class(); ?>
<?php the_permalink() ?>
<?php the_title(); ?>
<?php echo get_the_author_link(); ?>
<?php the_title_attribute(); ?>
<?php echo get_the_category_list(', '); ?>
<?php echo get_the_time('Y-m-j'); ?>
<?php echo get_the_time(get_option('date_format')); ?>
<?php the_post_thumbnail('full', array('class' => '')); ?>
<?php $the_post_thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full', false, ''); ?>
<?php echo $the_post_thumbnail_url[0]; ?>
<?php the_content(); ?>
<?php the_excerpt(); ?>
<?php echo get_the_tag_list('', ', ', ''); ?>
<?php comments_template(); ?>
<?php endwhile; ?>
<?php next_posts_link('&laquo; Older Entries'); ?>
<?php previous_posts_link('Newer Entries &raquo;'); ?>
<?php else : ?>
<p><?php _e('Oops, Not Found!', 'theme_name'); ?></p>
<p><?php _e('Uh Oh. Something is missing. Try double checking things.', 'theme_name'); ?></p>
<?php endif; ?>
<?php
$paged1 = isset($_GET['paged1']) ? (int) $_GET['paged1'] : 1;
$paged2 = isset($_GET['paged2']) ? (int) $_GET['paged2'] : 1;
$post_type_1_query = new WP_Query(
array(
'posts_per_page' => 1,
'post_type' => 'post_type_1',
'paged' => $paged1
)
);
if($post_type_1_query->have_posts()) :
?>
<div>
<?php
while($post_type_1_query->have_posts()) :
$post_type_1_query->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
<?php
endwhile;
echo paginate_links(
array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $post_type_1_query->max_num_pages,
'add_args' => array('paged2' => $paged2)
)
);
wp_reset_postdata();
?>
</div>
<?php endif; ?>
<?php
$post_type_2_query = new WP_Query(
array(
'posts_per_page' => 1,
'post_type' => 'post_type_2',
'paged' => $paged2
)
);
if($post_type_2_query->have_posts()) :
?>
<div>
<?php
while($post_type_2_query->have_posts()) :
$post_type_2_query->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
<?php
endwhile;
echo paginate_links(
array(
'format' => '?paged2=%#%',
'current' => $paged2,
'total' => $post_type_2_query->max_num_pages,
'add_args' => array('paged1' => $paged1)
)
);
wp_reset_postdata();
?>
</div>
<?php endif; ?>
<?php get_header(); ?>
<?php get_footer(); ?>
<?php get_sidebar(); ?>
<?php dynamic_sidebar(''); ?>
<?php language_attributes(); ?>
<?php bloginfo('name'); ?><?php wp_title('-'); ?>
<?php echo get_template_directory_uri(); ?>
<?php get_theme_mod('foo'); ?>
<?php bloginfo('pingback_url'); ?>
<?php wp_head(); ?>
<?php wp_footer(); ?>
<?php echo date('Y'); ?>
<?php echo apply_filters('the_content', $foo); ?>
<?php body_class(); ?>
<?php echo home_url(); ?>
<?php bloginfo('name'); ?>
<?php bloginfo('description'); ?>
<?php bones_main_nav(); ?>
<?php get_search_form(); ?>
<?php single_cat_title(); ?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php echo esc_attr(get_search_query()); ?>
<?php
class example_widget extends WP_Widget {
function example_widget() {
parent::WP_Widget(false, $name = 'Example Widget');
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;if ($title) echo $before_title . $title . $after_title;
?>
WIDGET CONTENT HERE
<?php
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
function form($instance) {
$title = esc_attr($instance['title']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<?php
}
}
add_action('widgets_init', create_function('', 'return register_widget("example_widget");'));
<?php
/**
Template Name: The name of the template
*/
get_header();
?>
<?php
$simple_query = new WP_Query(
array(
'posts_per_page' => 5
)
);
if($simple_query->have_posts()) :
?>
<ul class="grid">
<?php while($simple_query->have_posts()) : $simple_query->the_post(); ?><!--
--><li class="grid__item">
<a href="<?php the_permalink(); ?>">
<div><?php the_title(); ?></div>
</a>
</li><!--
--><?php
endwhile;
wp_reset_postdata();
?>
</ul>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment