Skip to content

Instantly share code, notes, and snippets.

@ozthegreat
Last active July 25, 2017 09:56
Show Gist options
  • Save ozthegreat/0a34ed736d7402351b35549423c16f8c to your computer and use it in GitHub Desktop.
Save ozthegreat/0a34ed736d7402351b35549423c16f8c to your computer and use it in GitHub Desktop.
WordPress Facebook Instant Articles - How to apply different styles to posts
<?php
/**
* Changes the Instant Article style for this post based on author.
*
* This is fired within the loop.
*
* @param string $style The default style.
* @return string The new style to apply.
*/
function wpna_author_style( $style ) {
// If the post was written by the user with ID 5
// then return the custom style.
if ( 5 === get_the_author_meta( 'ID' ) ) {
return 'timsmith';
}
// If the author was joebloggs then return a
// custom style.
if ( 'joebloggs' === get_the_author_meta('user_nicename') ) {
return 'joebloggs';
}
// Else return the default style.
return $style;
}
add_filter( 'wpna_facebook_post_get_style', 'wpna_author_style', 10, 1 );
<?php
/**
* Changes the Instant Article style for this post based on category.
*
* This is fired within the loop.
*
* @param string $style The default style.
* @return string The new style to apply.
*/
function wpna_category_style( $style ) {
// If the post has any of these categories then return a custom style.
if ( has_category( array( 'actor', 'director' ) ) ) {
return 'movies';
}
// Else return the default style.
return $style;
}
add_filter( 'wpna_facebook_post_get_style', 'wpna_category_style', 10, 1 );
<?php
/**
* Changes the Instant Article style for this post based on tag.
*
* This is fired within the loop.
*
* @param string $style The default style.
* @return string The new style to apply.
*/
function wpna_tag_style( $style ) {
// If the post has any of these tags then return a custom style.
if ( has_tag( array( 'romance', 'action', 'comedy' ) ) ) {
return 'movies';
}
// Else return the default style.
return $style;
}
add_filter( 'wpna_facebook_post_get_style', 'wpna_tag_style', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment