Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Workaround to use Custom Taxonomies in WordPress SEO titles
- *
- * @author: brasofilo
- * @licence: GPL
- *
- * Drop this code in your theme's functions.php
- *
- * Instructions: adjust the function **brsfl_wpseo_taxonomy_title** with your custom values
- *
- * Usage: go to the page /wp-admin/admin.php?page=wpseo_titles
- * and set the tag of your CPT to %%cf__my_cat_title%%
- * ATTENTION to the double underscore after %%cf
- *
- * How it works: the Custom Field "_my_cat_title" is automatically populated with the current categories of the desired CPT
- * and the value of the field will be used for you custom SEO tag
- * ATTENTION: an underscore at the beginning of the Custom Field prevents it from showing up in the post editing screen
- *
- */
- add_action ('save_post','brsfl_wpseo_taxonomy_title',10,2);
- /*
- * @desc Makes an automatic Custom Field with the current categories of a Custom Post Type
- *
- * ADJUST THIS 3 VALUES IN THE FUNCTION TO MATCH YOUR NEEDS
- * @movie Name of the Custom Post Type
- * @category Name of the Custom Taxonomy
- * @_my_cat_title Name of the Custom Field
- *
- */
- function brsfl_wpseo_taxonomy_title($post_ID, $post) {
- $type = get_post_type($post_ID);
- if ( 'movie' != $type ) return;
- $the_cats = brsfl_wpseo_get_terms($post_ID, 'category');
- update_post_meta($post_ID, '_my_cat_title', $the_cats);
- }
- /*
- * @desc Grabs the categories associated with a post
- *
- * @returns Comma separated list of categories
- *
- */
- function brsfl_wpseo_get_terms($ID, $taxonomy) {
- $terms = get_the_terms( $ID, $taxonomy);
- $return = array();
- foreach($terms as $t) {
- $return[] = $t->name;
- }
- return join( ", ", $return );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement