Skip to content

Instantly share code, notes, and snippets.

@gcherubin
Created November 27, 2012 15:27
Show Gist options
  • Save gcherubin/4154806 to your computer and use it in GitHub Desktop.
Save gcherubin/4154806 to your computer and use it in GitHub Desktop.
Wordpress Snippets
<?php
/**
* WordPress Snippets
* Compiled by goanimal
*/
?>
<?php ////// Metatag and Open Graph ?>
<?php
$thumb = get_post_meta($post->ID,'_thumbnail_id',false);
$thumb = wp_get_attachment_image_src($thumb[0], false);
$thumb = $thumb[0];
$default_img = get_bloginfo('stylesheet_directory').'/images/DEFAULT_IMG.png';
?>
<?php if(is_single() || is_page()) { ?>
<meta name="description" content="<?php
while(have_posts()):the_post();
$out_excerpt = str_replace(array("\r\n", "\r", "\n"), "", get_the_excerpt());
echo apply_filters('the_excerpt_rss', $out_excerpt);
endwhile; ?>" />
<meta property="og:type" content="article" />
<meta property="og:title" content="<?php single_post_title(''); ?> | <?php bloginfo('name'); ?>" />
<meta property="og:description" content="<?php
while(have_posts()):the_post();
$out_excerpt = str_replace(array("\r\n", "\r", "\n"), "", get_the_excerpt());
echo apply_filters('the_excerpt_rss', $out_excerpt);
endwhile; ?>" />
<meta property="og:url" content="<?php the_permalink(); ?>"/>
<meta property="og:image" content="<?php if ( $thumb[0] == null ) { echo $default_img; } else { echo $thumb; } ?>" />
<link rel="image_src" href="<?php if ( $thumb[0] == null ) { echo $default_img; } else { echo $thumb; } ?>" />
<?php } elseif(is_category()) { ?>
<meta name="description" content="<?php bloginfo('description'); ?> | <?php bloginfo('name'); ?>" />
<meta property="og:type" content="article" />
<meta property="og:title" content="<?php single_cat_title(); ?> | <?php bloginfo('name'); ?>" />
<meta property="og:url" content="<?php bloginfo('url'); ?>"/>
<meta property="og:description" content="<?php bloginfo('description'); ?> | <?php bloginfo('name'); ?>" />
<meta property="og:image" content="<?php if ( $thumb[0] == null ) { echo $default_img; } else { echo $thumb; } ?>" />
<link rel="image_src" href="<?php if ( $thumb[0] == null ) { echo $default_img; } else { echo $thumb; } ?>" />
<?php } else { ?>
<meta name="description" content="<?php bloginfo('name'); ?> | <?php bloginfo('description'); ?>" />
<meta property="og:type" content="article" />
<meta property="og:title" content="<?php bloginfo('name'); ?>" />
<meta property="og:url" content="<?php bloginfo('url'); ?>"/>
<meta property="og:description" content="<?php bloginfo('name'); ?> | <?php bloginfo('description'); ?>" />
<meta property="og:image" content="<?php if ( $thumb[0] == null ) { echo $default_img; } else { echo $thumb; } ?>" />
<link rel="image_src" href="<?php if ( $thumb[0] == null ) { echo $default_img; } else { echo $thumb; } ?>" />
<?php } ?>
<?php ////// Query posts (1 single sticky post) ?>
<?php
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
query_posts( $args );
?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to overload this in a child theme then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php
/* Content (content.php) */
?>
<article <?php post_class(); ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark">
<?php the_post_thumbnail('large');?>
<h1><?php the_title(); ?></h1>
<p><?php echo substr(get_the_excerpt(), 0,200); ?> ...</p>
</a>
</article>
?>
<?php ////// Additional Thumbnails (in functions.php)?>
<?php
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
add_image_size( 'square', 100, 100, true);
add_image_size( 'fixed-width', 600, false);
}
?>
<?php ////// Remove Comment Feed in Head (in functions.php)?>
<?php
remove_action( 'wp_head', 'feed_links', 2 );
add_action('wp_head', 'addBackPostFeed');
function addBackPostFeed() {
echo '<link rel="alternate" type="application/rss+xml" title="RSS 2.0 Feed" href="'.get_bloginfo('rss2_url').'" />';
}
?>
<?php ////// Custom Post Type (in functions.php)?>
<?php
/* Create Custom Post Type Books */
add_action('init', 'books_register');
function book_register() {
$labels = array(
'name' => _x('Books', 'post type general name'),
'singular_name' => _x('Book', 'post type singular name'),
'add_new' => _x('Add New Book', 'portfolio item'),
'add_new_item' => __('Add New Book'),
'edit_item' => __('Edit Book'),
'new_item' => __('New Book'),
'view_item' => __('View Book'),
'search_items' => __('Search Book'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/icon_book.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail'),
'taxonomies' => array('category', 'post_tag')
);
register_post_type( 'book' , $args );
flush_rewrite_rules();
}
/* Create Genre Taxonomy */
/* http://codex.wordpress.org/Taxonomies */
register_taxonomy("genres", array("book"), array("hierarchical" => true, "label" => "Genres", "singular_label" => "Genre", "rewrite" => true));
/* Create Custom Fields */
/* http://codex.wordpress.org/Function_Reference/add_meta_box */
add_action("admin_init", "admin_init");
function admin_init(){
add_meta_box("year_meta", "Year", "year", "book", "advanced", "low");
}
function year(){
global $post;
$custom = get_post_custom($post->ID);
$year = $custom["year"][0];
?>
<label>Year:</label>
<input name="year" value="<?php echo $year; ?>" />
<?php
}
global $post;
$custom = get_post_custom($post->ID);
add_action('save_post', 'save_details');
function save_details(){
global $post;
update_post_meta($post->ID, "year", $_POST["year"]);
}
/* Modify Custom Post Type Layout */
add_action("manage_posts_custom_column", "book_custom_columns");
add_filter("manage_edit-book_columns", "book_edit_columns");
function book_edit_columns($columns){
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Book",
"description" => "Description",
"year" => "Year",
"genres" => "Genres",
);
return $columns;
}
function book_custom_columns($column){
global $post;
switch ($column) {
case "description":
the_excerpt();
break;
case "year":
$custom = get_post_custom();
echo $custom["year"][0];
break;
case "genres":
echo get_the_term_list($post->ID, 'genres', '', ', ','');
break;
}
}
/* Add thumbnails support */
add_theme_support('post-thumbnails');
function my_cpt_post_types( $post_types ) {
$post_types[] = 'book';
return $post_types;
}
add_filter( 'cpt_post_types', 'my_cpt_post_types' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment