Skip to content

Instantly share code, notes, and snippets.

@jmsmrgn
jmsmrgn / wp-paginate-cpt.php
Created Feb 7, 2014
WordPress - Pagination for custom posts types
View wp-paginate-cpt.php
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&post_type=news'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<!-- LOOP: Usual Post Template Stuff Here-->
View w3tc_errors.php
Edit file /mnt/sofiweb/deploy/sofi-web/releases/20140529180905/web/nginx.conf and add the following rules above the WordPress directives:
# BEGIN W3TC Page Cache core
set $w3tc_rewrite 1;
if ($request_method = POST) {
set $w3tc_rewrite 0;
}
if ($query_string != "") {
set $w3tc_rewrite 0;
}
@jmsmrgn
jmsmrgn / wp-if-page-parent-child.php
Last active Aug 29, 2015
WP - If page parent or child
View wp-if-page-parent-child.php
function is_tree($pid)
{
global $post;
$ancestors = get_post_ancestors($post->$pid);
$root = count($ancestors) - 1;
$parent = $ancestors[$root];
if(is_page() && (is_page($pid) || $post->post_parent == $pid || in_array($pid, $ancestors)))
{
@jmsmrgn
jmsmrgn / wp_change_excerpt_more.php
Created Sep 26, 2014
WP - Change excerpt more - only active where excerpt is customized
View wp_change_excerpt_more.php
//* Change excerpt more - only active where excerpt is customized
function manual_excerpt_more( $excerpt ) {
$excerpt_more = '';
if( has_excerpt() ) {
$excerpt_more = '&nbsp;<a href="' . get_permalink() . '" rel="nofollow">[Read more]</a>';
}
return $excerpt . $excerpt_more;
}
add_filter( 'get_the_excerpt', 'manual_excerpt_more' );
@jmsmrgn
jmsmrgn / wp_custom_css_per_post
Created Oct 23, 2014
WP - Custom CSS per post/page
View wp_custom_css_per_post
/**
* Custom CSS field for posts/pages (inserted into <head></head>)
*/
add_action('admin_menu', 'custom_css_hooks');
add_action('save_post', 'save_custom_css');
add_action('wp_head','insert_custom_css');
function custom_css_hooks() {
add_meta_box('custom_css', 'Custom CSS', 'custom_css_input', 'post', 'normal', 'high');
add_meta_box('custom_css', 'Custom CSS', 'custom_css_input', 'page', 'normal', 'high');
}
@jmsmrgn
jmsmrgn / wp-trim-words.php
Created Jul 3, 2015
WP - Trim Word Count
View wp-trim-words.php
<?php
$content = get_the_content();
echo wp_trim_words( $content , '25' );
?>
@jmsmrgn
jmsmrgn / wp-truncate-field.php
Last active Aug 29, 2015
WP - Truncate Field Content
View wp-truncate-field.php
function truncate_field($title) {
global $post;
$text = get_field($title);
if ( '' != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>>', $text);
$text_length = strlen($text); // Get text length (characters)
$excerpt_length = 100; // 50 desired characters
$excerpt_more = '...';
@jmsmrgn
jmsmrgn / wp-footer-enqueue.php
Created Aug 21, 2015
WP - Force scripts to load in footer
View wp-footer-enqueue.php
@jmsmrgn
jmsmrgn / wp-slug-body-class.php
Created Aug 21, 2015
WP - Add page slug to body class
View wp-slug-body-class.php
/**
* Add page slug to body class
*/
function add_slug_body_class($classes) {
global $post;
if (isset($post)) {
$classes[] = $post->post_type . '-' . $post->post_name;
}
return $classes;
}
@jmsmrgn
jmsmrgn / ie-10-css-hack.css
Created Sep 17, 2015
IE 10 css hack using -ms-high-contrast
View ie-10-css-hack.css
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
/* IE10-specific styles go here */
}