Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
Plugin Name: Microblog
Author: Josh Pollock
Version: 0.1.0
*/
/**
* Make sure theme supports aside post format
*/
add_action( 'after_setup_theme', function() {
$formats = get_theme_support( 'post-formats' );
$formats = $formats[0];
if ( ! is_array( $formats ) ) {
$formats = array( 'aside');
}elseif( ! in_array( 'aside', $formats ) ) {
$formats[] = 'aside';
}
add_theme_support( 'post-formats', $formats );
}, 50 );
/**
* Add rewrite rules for small-things microblog
*/
add_action( 'init', function (){
//pagination rule
add_rewrite_rule( 'small-things/page/?([0-9]{1,})/?$', 'index.php?post_format=aside&paged=$matches[1]', 'top' );
//first page rule
add_rewrite_rule( 'small-things/?$', 'index.php?post_format=aside', 'top' );
});
/**
* Customize and shorten the asides term link.
*/
add_filter( 'term_link', function( $termlink, $term, $taxonomy ) {
if ( 'post_format' == $taxonomy && 'Aside' == $term->name ) {
$termlink = home_url( 'small-things' );
}
return $termlink;
}, 10, 3 );
/**
* Remove asides from main blog index
*/
add_action( 'pre_get_posts', function( $query ) {
if (
$query->is_home() && $query->is_main_query() &&
( ! isset( $query->query_vars[ 'post_format' ] ) ||
( isset( $query->query_vars[ 'post_format' ] ) ) &&
'post-format-aside' != $query->query_vars[ 'post_format' ] )
) {
$query->set( 'tax_query',
array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-aside' ),
'operator' => 'NOT IN',
)
)
);
}
});
@sigul

This comment has been minimized.

Copy link

@sigul sigul commented Mar 15, 2017

hi, the small-things rewrite rule don't work, nor in the plugin nor on your website.

anyway, thanks for the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment