I was trying to create a custom tag cloud widget that will look like this:
Also, when I tried including some new tags in my post then it's showing something like this (just one tag):
This is my functions.php
if(function_exists('register_sidebar')) {
register_sidebar(
array(
'name' => __('Main Sidebar'),
'id' => 'main-sidebar',
'description' => __('The main sidebar area'),
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
/****************************************************/
/* Load Custom Widgets */
/***************************************************/
require_once('functions/rh_tags.php');
This is my rh_tags.php
<?php
class RH_Tags extends WP_Widget {
function __construct() {
$params = array(
'name' => 'Creative : Tag Cloud Widget',
'description' => 'Displays info of your blog'
);
parent:: __construct('RH_Tags','',$params);
}
public function form($instance) {
//display our form in widget page
extract($instance);
?>
<p>
<label for="<?php echo $this->get_field_id('title') ?>">Title : </label>
<input class="widefat" id="<?php echo $this->get_field_id('title') ?>" name="<?php echo $this->get_field_name('title') ?>"
value="<?php if(isset($title)) echo esc_attr($title); ?>" />
</p>
<?php
}
public function widget($args,$instance) {
//displays our widget
extract($args);
extract($instance);
echo $before_widget;
echo $before_title .$title. $after_title;
echo '<div class="label">';
echo "<ul>";
echo "<li>";
echo the_tags(' ',' ');
echo "</li>";
echo "</ul>";
echo '</div>';
echo $after_widget;
}
}
add_action('widgets_init','rh_register_tags');
function rh_register_tags() {
register_widget('RH_Tags');
}
I was trying to create a custom tag cloud widget that will look like this:
Also, when I tried including some new tags in my post then it's showing something like this (just one tag):
This is my functions.php
if(function_exists('register_sidebar')) {
register_sidebar(
array(
'name' => __('Main Sidebar'),
'id' => 'main-sidebar',
'description' => __('The main sidebar area'),
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
/****************************************************/
/* Load Custom Widgets */
/***************************************************/
require_once('functions/rh_tags.php');
This is my rh_tags.php
<?php
class RH_Tags extends WP_Widget {
function __construct() {
$params = array(
'name' => 'Creative : Tag Cloud Widget',
'description' => 'Displays info of your blog'
);
parent:: __construct('RH_Tags','',$params);
}
public function form($instance) {
//display our form in widget page
extract($instance);
?>
<p>
<label for="<?php echo $this->get_field_id('title') ?>">Title : </label>
<input class="widefat" id="<?php echo $this->get_field_id('title') ?>" name="<?php echo $this->get_field_name('title') ?>"
value="<?php if(isset($title)) echo esc_attr($title); ?>" />
</p>
<?php
}
public function widget($args,$instance) {
//displays our widget
extract($args);
extract($instance);
echo $before_widget;
echo $before_title .$title. $after_title;
echo '<div class="label">';
echo "<ul>";
echo "<li>";
echo the_tags(' ',' ');
echo "</li>";
echo "</ul>";
echo '</div>';
echo $after_widget;
}
}
add_action('widgets_init','rh_register_tags');
function rh_register_tags() {
register_widget('RH_Tags');
}
Share
Improve this question
edited Nov 20, 2014 at 12:14
kaiser
50.9k27 gold badges151 silver badges245 bronze badges
asked Nov 20, 2014 at 9:27
Raunak HajelaRaunak Hajela
1133 silver badges12 bronze badges
9
|
Show 4 more comments
1 Answer
Reset to default 1What tags/terms/taxons do you get?
The the_tags( $before = null, $sep = ', ', $after = '' )
function is a wrapper for get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 )
.
This function applies the filter the_tags
on the get_the_term_list( 0, 'post_tag', $before = '', $sep = '', $after = '' )
(0
is the $id
and $post_tag
the $taxonomy
argument) for the post_tag
taxonomy and returns it. And get_the_term_list()
is responsible for fetching the tags and building the MarkUp.
To fetch the terms, it uses get_the_terms( $id, $taxonomy )
internally. If you look at the internals of those functions (see links), it uses the get_post()
if no $id
was provided.
This indicates that you always will only get the tags/terms for a single post. To illustrate that, here're the first lines of the get_the_terms( $id, $taxonomy )
function:
function get_the_terms( $post, $taxonomy ) {
if ( ! $post = get_post( $post ) )
return false;
$terms = get_object_term_cache( $post->ID, $taxonomy );
if ( false === $terms ) {
$terms = wp_get_object_terms( $post->ID, $taxonomy );
wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
}
As you can see, the second function in those wrappers provides no ID already, so it falls back to an ID of 0
. And that makes if fall back to the global $post
/\WP_Post
object, which is the one from the main query (where \WP_Query->current_post
points to). That can be a lot. At first it's the first post in the loop of the main query, which is run per default. Then, if the query does not get reset after the loop, it's the last post, etc. If you have later loops for e.g. done by plugins or your theme, it's a different post and so on.
Conclusion: If you fall back to the global post object, then you are using what ever the (unreliable) global provides you with.
What I would suggest is to use get_terms()
instead as its output is easier to control and it does not rely on using data from a post - which you don't want.
Additional notes
Why not to use extract()
? Because
- IDEs (like PHPStorm, Sublime, Aptana, etc.) do not recognize the origins/contents.
It is unreliable as you never know if something is set or not. Use the normal array instead and rely on defaults and check it with
empty()
orisset()
:public function( Array $args ) { $foo = isset( $args['foo'] ) ? $args['foo'] : 'my default'; // work with `$foo`
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744969494a4603881.html
CSS
. You will have to style your tag links accordingly. – Robert hue Commented Nov 20, 2014 at 9:30