How to use all tags in post permalinks
My site with pictures is set up so that the posts do not have a title and have only tags.
I would like to know how to use all tags in permalinks settings?
Topic tags urls permalinks Wordpress
Category Web
My site with pictures is set up so that the posts do not have a title and have only tags.
I would like to know how to use all tags in permalinks settings?
Topic tags urls permalinks Wordpress
Category Web
To use all tags in a post permalink, try a variation of my similar answer for How to use first tag in permalinks
add_rewrite_tag( $tag, $regex )
adds a new placeholder you can use in Settings/Permalinks.
The filter on post_link
translates the placeholder into something useful, here a list of all tag slugs, separated by a -
.
Adjust the static variables $default
and $placeholder
to your needs.
Then install and activate the code as a plugin, go to Settings/Permalinks and use the new placeholder like this:
add_action( 'init', array ( 'T5_All_Tags_Permalink', 'init' ) );
/**
* Adds '%tag%' as rewrite tag (placeholder) for permalinks.
*/
class T5_All_Tags_Permalink
{
/**
* What to use when there is no tag.
*
* @var string
*/
protected static $default = 'tag';
/**
* Used in Settings/Permalinks
*
* @var string
*/
protected static $placeholder = '%tags%';
/**
* Add tag and register 'post_link' filter.
*
* @wp-hook init
* @return void
*/
public static function init()
{
add_rewrite_tag( self::$placeholder, '([^/]+)' );
add_filter( 'post_link', array( __CLASS__, 'filter_post_link' ) , 10, 2 );
}
/**
* Parse post link and replace the placeholder.
*
* @wp-hook post_link
* @param string $link
* @param object $post
* @return string
*/
public static function filter_post_link( $link, $post )
{
static $cache = array (); // Don't repeat yourself.
if ( isset ( $cache[ $post->ID ] ) )
return $cache[ $post->ID ];
if ( FALSE === strpos( $link, self::$placeholder ) )
{
$cache[ $post->ID ] = $link;
return $link;
}
$tags = get_the_tags( $post->ID );
if ( ! $tags )
{
$cache[ $post->ID ] = str_replace( self::$placeholder, self::$default, $link );
return $cache[ $post->ID ];
}
$slugs = wp_list_pluck( $tags, 'slug' );
$cache[ $post->ID ] = str_replace( self::$placeholder, join( '-', $slugs ), $link );
return $cache[ $post->ID ];
}
}
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.