Skip to content

Instantly share code, notes, and snippets.

View raphaelchaib's full-sized avatar

Raphael Chaib raphaelchaib

View GitHub Profile
@raphaelchaib
raphaelchaib / functions.php
Last active December 16, 2015 23:39
Remove default css classes and IDs from wp_nav_menu
<?php
function my_nav_menu_css_class($classes) {
$custom_classes = array();
foreach($classes as $class) {
if($class=='menu-item') return $custom_classes;
$custom_classes[] = $class;
}
}
function my_nav_menu_css_id($id) {
@raphaelchaib
raphaelchaib / functions.php
Created April 11, 2013 19:13
Remove links from images on Wordpress posts
<?php
// Remove links from images on Wordpress posts
add_filter( 'the_content', 'attachment_image_link_remove_filter' );
function attachment_image_link_remove_filter( $content )
{
$content =
preg_replace(array(
'{<a(.*?)(wp-att|wp-content/uploads)[^>]*><img}',
'{ wp-image-[0-9]*" /></a>}'
@raphaelchaib
raphaelchaib / custom_functions.php
Created April 10, 2013 14:57
Excerpt or content word limit in WordPress
<?php
// Custom excerpt word limit
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
@raphaelchaib
raphaelchaib / general.php
Created April 9, 2013 16:37
Check if page/post is parent or if it has ancestors on Wordpress.
<?php
if ( is_page('americas') || $post->post_parent == '2348' || ($post->ancestors && in_array( '2348', $post->ancestors) ) ) {
}
@raphaelchaib
raphaelchaib / functions.php
Last active December 15, 2015 20:49
Custom excerpt class for Wordpress Add to functions.php
<?php
//Custom excerpt class for Wordpress
class Excerpt {
// Default length (by WordPress)
public static $length = 55;
// Default more (by WordPress)
public static $more = '[...]';
@raphaelchaib
raphaelchaib / app.js
Created April 1, 2013 18:38
Sticky float box
(function (c) {
c.fn.stickyfloat = function (l) {
var b = this,
f = c(document),
h = parseInt(b.parent().css("padding-top")),
g = b.parent().offset().top,
a, d, i, j, k, e;
a = c.fn.stickyfloat.opts;
c.extend(a, {
startOffset: g,
@raphaelchaib
raphaelchaib / functions.php
Last active December 15, 2015 15:59
Customize "Featured Image" in Wordpress post types
<?php
// Use "Featured Image" box as a custom box
add_action('do_meta_boxes', 'customposttype_image_box');
function customposttype_image_box()
{
// remove_meta_box( $id, $page, $context );
remove_meta_box('postimagediv', 'post_type', 'side');
// add_meta_box( $id, $title, $callback, $post_type, $context,
@raphaelchaib
raphaelchaib / functions.php
Created March 28, 2013 19:59
WordPress: Show post's attachment images
<?php
// Show post's attachment images
function show_attachment_images() {
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => 42,
'exclude' => get_post_thumbnail_id()
) );
@raphaelchaib
raphaelchaib / taxonomy.php
Created March 25, 2013 14:24
Wordpress: Taxonomy default schema
<?php
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_book_taxonomies', 0 );
//create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name' ),
@raphaelchaib
raphaelchaib / app.js
Created March 21, 2013 18:40
Javascript: Scroll window to top. For this snippet work the container of icon (or text) must be "display: none" for fade-in and fade-out.
// Scroll to top
jQuery(window).scroll(function() {
if( jQuery(this).scrollTop() != 0 ) {
jQuery('.scroll-to-top').fadeIn();
} else {
jQuery('.scroll-to-top').fadeOut();
}
});