public
Created

Associate SEO Auto Linker links by category.

  • Download Gist
seoal-dynamic.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
<?php
/*
Plugin Name: SEO Auto Linker Dynamic Links
Plugin URI: http
Description: Fully dynamic linking for SEO Auto Linker.
Version: 1.0
Author: Christopher Davis
Author URI: http://christopherdavis.me
License: GPL2
 
Copyright 2012 Christopher Davis
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
 
SEOAL_Dynamic::init();
 
class SEOAL_Dynamic
{
// taxonomy you want to use
const TAX = 'category';
 
public static function init()
{
if(!class_exists('SEO_Auto_Linker_Base'))
return;
 
add_action(
'init',
array(__CLASS__, 'register'),
20
);
 
add_filter(
'seoal_links',
array(__CLASS__, 'add_links')
);
}
 
public static function register()
{
register_taxonomy_for_object_type(
self::TAX,
SEO_Auto_Linker_Base::POST_TYPE
);
}
 
public static function add_links($links)
{
global $post;
$terms = get_the_terms($post->ID, self::TAX);
 
if(is_wp_error($terms) || !$terms)
return array(); // no links!
 
/*
PHP greater than 5.3 could do this:
$new_terms = array_map(function($t) {
return $t->term_id;
}, array_values($terms));
*/
 
$new_terms = array();
foreach($terms as $t)
$new_terms[] = $t->term_id;
 
$links = get_posts(array(
'post_type' => SEO_Auto_Linker_Base::POST_TYPE,
'nopaging' => true,
'tax_query' => array(
array(
'taxonomy' => self::TAX,
'field' => 'id',
'terms' => $new_terms
)
)
));
 
return $links;
}
} // end SEOAL_Dyanmic

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.