Ask your WordPress questions! Pay money and get answers fast! (more info)

Get tags for current category to show in drop down + submit WordPress

  • SOLVED

Already found this but doesn't work for me: http://wpquestions.com/question/showChrono/id/4323

I use the below code and want the tags to show up in a drop down menu with submit/search button.

<ul>
<?php
$category = get_the_category();
if($category[0]){
$cat_id=$category[0]->term_id;

}
query_posts('cat='.$cat_id.'&posts_per_page=-1');
if(have_posts()): while (have_posts()) : the_post();
$all_tag_objects = get_the_tags();
if($all_tag_objects){
foreach($all_tag_objects as $tag) {
if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}
}

}

endwhile;endif;

$tag_ids_unique = array_unique($all_tag_ids);

// Now use the following code for displaying the tags list with each tag having its own link.

foreach($tag_ids_unique as $tag_id) {
$post_tag = get_term( $tag_id, 'post_tag' );
echo '<li><a href="'.get_tag_link($tag_id).'">'.$post_tag->name.' ('.$post_tag->count.')</a></li>';
}?>
</ul>




<strong>@Arnav Joy</strong>,

The code i'm using now is working on category pages.

But i get an error on the search function page where there are no tags to show:

<blockquote>Warning: array_unique() expects parameter 1 to be array, null given in plugins/php-code-widget/execphp.php(44) : eval()'d code on line 20

Warning: Invalid argument supplied for foreach() in php-code-widget/execphp.php(44) : eval()'d code on line 24</blockquote>

<strong>@Ellah A</strong>,

That plugin doesn't do what i would like.


<strong>@Arnav Joy</strong>,

$cat_id=$category[<strong>-1</strong>]->term_id;

works for the search page, but then the other categories show all tags instead of the tags of the current categories.

The suggested code in search.php doesn't work either. And there is no drop down in the code?

search.php looks like this:

<?php include(TEMPLATEPATH."/rich.inc.php"); get_header(); ?>
<?php include(TEMPLATEPATH."/header_includes.php");?>
<div id="wrapper">
<div id="contentcontainer" class="container_16 containermargin">
<?php if ($rich_extraloop == "true") {
$uploads = wp_upload_dir();
$newloop = $uploads['basedir'].'/presets/'.$rich_extralooppath;
if (file_exists($newloop)) {
include ($newloop); }
else { include(TEMPLATEPATH."/extraloop.php"); }

} else { ?>
<?php if ( $rich_sidebarleftright == "true" ): get_sidebar('left'); elseif ( $rich_sidebarleft == "true" ): get_sidebar(); endif; ?>
<div id="content" class="grid_<?php echo 16 - $rich_sbwidth; ?>">
<?php if ( have_posts() ) : ?>
<div class="boxes">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'richwp' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</div>
<?php get_template_part( 'loop', 'Zoeken' );?>
<?php else : ?>
<div id="post-0" class="boxes post no-results not-found">
<h1 class="entry-title"><?php _e( 'Niet gevonden', 'richwp' ); ?></h1>
<div class="entry-content">
<p><?php _e( 'Uw zoekopdracht heeft niets opgeleverd. Wijzig uw zoekopdracht en zoek opnieuw. ', 'richwp' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</div>
<?php endif; ?>
</div><!-- #container -->
<?php if ( $rich_sidebarleftright == "true" ): get_sidebar('right'); elseif ( $rich_sidebarleft == "true" ): else : get_sidebar(); endif; } ?>
<div style="clear:both;"></div>
</div><!-- #contentcontainer -->
</div><!-- #wrapper -->
<?php get_footer(); ?>



<strong>@Arnav Joy</strong>,

It still shows all tags with the code you suggest as last.

<strong>@Arnav Joy</strong>,

It should show up like this:

http://www.hengelsportdeal.com/testtest
http://www.hengelsportdeal.com/?s=testtest (wordpress search)

div class="entry-content">
<p><?php _e( 'Uw zoekopdracht heeft niets opgeleverd. Wijzig uw zoekopdracht en zoek opnieuw. ', 'richwp' ); ?></p>
<?php get_search_form(); ?>


<strong>@Arnav Joy</strong>,

That is how the searchfunction shows up. Showing nothing. ?s= is wordpress search function showing a blank page. When i manually add a word that doesn't exists on my site (/testtest) then you do see a message, nothing found...

But the main thing still remains, getting a <strong>drop down</strong> php code for the <strong>current category tags</strong>.

The current category tags code does already work, now i has to show as a drop down...

<ul>
<?php
$category = get_the_category();
if($category[0]){
$cat_id=$category[0]->term_id;

}
query_posts('cat='.$cat_id.'&posts_per_page=-1');
if(have_posts()): while (have_posts()) : the_post();
$all_tag_objects = get_the_tags();
if($all_tag_objects){
foreach($all_tag_objects as $tag) {
if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}
}

}

endwhile;endif;

$tag_ids_unique = array_unique($all_tag_ids);

// Now use the following code for displaying the tags list with each tag having its own link.

foreach($tag_ids_unique as $tag_id) {
$post_tag = get_term( $tag_id, 'post_tag' );
echo '<li><a href="'.get_tag_link($tag_id).'">'.$post_tag->name.' ('.$post_tag->count.')</a></li>';
}?>
</ul>


<strong>@Arnav Joy</strong>,

Yes it shows drop down. But from 1 tag, it should show all the tags from that current category and made clickable and with submit/go button to go to the tags url's.

<strong>@Arnav Joy</strong>,

Yes, it should become somewhat like this plugin: http://wordpress.org/extend/plugins/cat-tag-filter-widget/

Except for the fact, that this plugin shows all tags and not the tags from the current category.


<strong>@Arnav Joy</strong>,

Then i get:

<blockquote>Parse error: syntax error, unexpected '}' in wp-content/plugins/php-code-widget/execphp.php(44) : eval()'d code on line 107</blockquote>


<strong>@Arnav Joy</strong>,

It still show a list of all tags instead of the tags of the current category, and they are not clickable to go to tag url (domain.com/category/.../tag/...)


<strong>@Arnav Joy</strong>,

Sorry for the confusion, I meant if people landed on the search page where there aren't any categories to show (there is a sidebar with widgets where i place the php code with php code widget) then i get the following mistake:

<blockquote>Warning: array_unique() expects parameter 1 to be array, null given in plugins/php-code-widget/execphp.php(44) : eval()'d code on line 20

Warning: Invalid argument supplied for foreach() in php-code-widget/execphp.php(44) : eval()'d code on line 24</blockquote>


<strong>But the main problem is</strong>, that i have a code which does what i want: (show all the tags from the current category of the page which i'm on)
But this code shows up as a list but i want it to be a drop down list, the code you provided does show a drop down indeed. But when i select a tag from the drop down list then i need to be directed to the page of that particular category (domain.com/category/.../tag/...) and if possible with a submit button.

<strong>@Arnav Joy</strong>,

The redirection to the tag url works, and the submit button works too. But now the drop down list shows all tags instead of the tags of the current category page (i have custom menu with Categories instead of page's as menu-items).

<strong>@Arnav Joy</strong>,

Still the same the drop down list shows all tags instead of the tags of the current category page!

<strong>@Arnav Joy</strong>,

Oke Arnav Joy solved the problem! Here is the code:

Get tags for current category + drop down list + submit:<?php
wp_reset_query();

if( is_single() ) {

$category = get_the_category();

if($category[0])

$cat_id=$category[0]->term_id;

query_posts('cat='.$cat_id.'&posts_per_page=-1');

}

if( is_category() )

query_posts('cat='.get_query_var('cat').'&posts_per_page=-1');

else

query_posts('posts_per_page=-1');

if(have_posts()): while (have_posts()) : the_post();

$all_tag_objects = get_the_tags();

if($all_tag_objects){

foreach($all_tag_objects as $tag) {

if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}

}

}

endwhile;endif;

$tag_ids_unique = array_unique($all_tag_ids);

// Now use the following code for displaying the tags list with each tag having its own link.

?>

<form method="get" style="display:inline;" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">

<select name="tag">

<?php

foreach($tag_ids_unique as $tag_id) {

$post_tag = get_term( $tag_id, 'post_tag' );

?>

<option value="<?php echo $post_tag->slug; ?>"><?php echo $post_tag->name; ?></option>

<?php } ?>

</select>

<input type="submit" name="tag_submit" value="Zoeken">

</form>

Answers (2)

2012-12-17

Arnav Joy answers:

any thing you are getting or just blank?


Arnav Joy comments:

try this is search.php

<ul>

<?php



query_posts('cat=-1&posts_per_page=-1');

if(have_posts()): while (have_posts()) : the_post();

$all_tag_objects = get_the_tags();

if($all_tag_objects){

foreach($all_tag_objects as $tag) {

if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}

}



}



endwhile;endif;



$tag_ids_unique = array_unique($all_tag_ids);



// Now use the following code for displaying the tags list with each tag having its own link.



foreach($tag_ids_unique as $tag_id) {

$post_tag = get_term( $tag_id, 'post_tag' );

echo '<li><a href="'.get_tag_link($tag_id).'">'.$post_tag->name.' ('.$post_tag->count.')</a></li>';

}?>

</ul>


Arnav Joy comments:

the problem in search page is that in the following code

<?php

$category = get_the_category();

if($category[0]){

$cat_id=$category[0]->term_id;



}

query_posts('cat='.$cat_id.'&posts_per_page=-1');

$cat_id is not taking proper value , so give it some value say -1 and it will work


Arnav Joy comments:

try this

<ul>

<?php

if(is_category()){
$category = get_the_category();

if($category[0]){

$cat_id=$category[0]->term_id;



}
}
else
$cat_id = -1;

query_posts('cat='.$cat_id.'&posts_per_page=-1');

if(have_posts()): while (have_posts()) : the_post();

$all_tag_objects = get_the_tags();

if($all_tag_objects){

foreach($all_tag_objects as $tag) {

if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}

}



}



endwhile;endif;



$tag_ids_unique = array_unique($all_tag_ids);



// Now use the following code for displaying the tags list with each tag having its own link.



foreach($tag_ids_unique as $tag_id) {

$post_tag = get_term( $tag_id, 'post_tag' );

echo '<li><a href="'.get_tag_link($tag_id).'">'.$post_tag->name.' ('.$post_tag->count.')</a></li>';

}?>

</ul>


Arnav Joy comments:

can you let me know what should it display at search page?


Arnav Joy comments:

sorry i did not understood

http://www.hengelsportdeal.com/testtest
http://www.hengelsportdeal.com/?s=testtest (wordpress search)


Arnav Joy comments:

try this one , it will show you drop down

<ul>

<?php

$category = get_the_category();

if($category[0]){

$cat_id=$category[0]->term_id;



}

query_posts('cat='.$cat_id.'&posts_per_page=-1');

if(have_posts()): while (have_posts()) : the_post();

$all_tag_objects = get_the_tags();

if($all_tag_objects){

foreach($all_tag_objects as $tag) {

if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}

}



}



endwhile;endif;



$tag_ids_unique = array_unique($all_tag_ids);



// Now use the following code for displaying the tags list with each tag having its own link.

echo '<select name="tag_d">';

foreach($tag_ids_unique as $tag_id) {

$post_tag = get_term( $tag_id, 'post_tag' );

?>
<option value="<?php echo $post_tag->name; ?>"><?php echo $post_tag->name; ?></option>
<?php
echo '</select>';


}?>

</ul>


Arnav Joy comments:

can you explain your last point

<blockquote>Yes it shows drop down. But from 1 tag, it should show all the tags from that current category and made clickable and with submit/go button to go to the tags url's. </blockquote>


Arnav Joy comments:

try this

<ul>

<?php

$category = get_the_category();

if($category[0]){

$cat_id=$category[0]->term_id;



}

query_posts('cat='.$cat_id.'&posts_per_page=-1');

if(have_posts()): while (have_posts()) : the_post();

$all_tag_objects = get_the_tags();

if($all_tag_objects){

foreach($all_tag_objects as $tag) {

if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}

}



}



endwhile;endif;



$tag_ids_unique = array_unique($all_tag_ids);



// Now use the following code for displaying the tags list with each tag having its own link.
?>
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<select name="s">;

foreach($tag_ids_unique as $tag_id) {

$post_tag = get_term( $tag_id, 'post_tag' );

?>
<option value="<?php echo $post_tag->name; ?>"><?php echo $post_tag->name; ?></option>
<?php } ?>
</select>
</form>

</ul>


Arnav Joy comments:

check this



<?php

$category = get_the_category();

if($category[0]){

$cat_id=$category[0]->term_id;



}

query_posts('cat='.$cat_id.'&posts_per_page=-1');

if(have_posts()): while (have_posts()) : the_post();

$all_tag_objects = get_the_tags();

if($all_tag_objects){

foreach($all_tag_objects as $tag) {

if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}

}



}



endwhile;endif;



$tag_ids_unique = array_unique($all_tag_ids);



// Now use the following code for displaying the tags list with each tag having its own link.
?>
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<select name="s">
<?php
foreach($tag_ids_unique as $tag_id) {

$post_tag = get_term( $tag_id, 'post_tag' );

?>
<option value="<?php echo $post_tag->name; ?>"><?php echo $post_tag->name; ?></option>
<?php } ?>
</select>
</form>


Arnav Joy comments:

you was saying that they should go to search.php and now you want them to go to tag url , i think there is some confusion , your problem is not so big but not clear instead


Arnav Joy comments:

ok try this



<?php

wp_reset_query();

if( is_category() ) {
$category = get_the_category();
if($category[0])
$cat_id=$category[0]->term_id;
query_posts('cat='.$cat_id.'&posts_per_page=-1');
}
else
query_posts('posts_per_page=-1');





query_posts('cat='.$cat_id.'&posts_per_page=-1');

if(have_posts()): while (have_posts()) : the_post();

$all_tag_objects = get_the_tags();

if($all_tag_objects){

foreach($all_tag_objects as $tag) {

if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}

}



}



endwhile;endif;



$tag_ids_unique = array_unique($all_tag_ids);



// Now use the following code for displaying the tags list with each tag having its own link.
?>
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<select name="tag">
<?php
foreach($tag_ids_unique as $tag_id) {

$post_tag = get_term( $tag_id, 'post_tag' );

?>
<option value="<?php echo $post_tag->name; ?>"><?php echo $post_tag->name; ?></option>
<?php } ?>
</select>
<input type="submit" name="tag_submit" value="submit">
</form>


Arnav Joy comments:

use this code

<?php



wp_reset_query();



if( is_category() )
query_posts('cat='.get_query_var('cat').'&posts_per_page=-1');
else
query_posts('posts_per_page=-1');




if(have_posts()): while (have_posts()) : the_post();



$all_tag_objects = get_the_tags();



if($all_tag_objects){



foreach($all_tag_objects as $tag) {



if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}



}







}







endwhile;endif;







$tag_ids_unique = array_unique($all_tag_ids);







// Now use the following code for displaying the tags list with each tag having its own link.

?>

<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">

<select name="tag">

<?php

foreach($tag_ids_unique as $tag_id) {



$post_tag = get_term( $tag_id, 'post_tag' );



?>

<option value="<?php echo $post_tag->name; ?>"><?php echo $post_tag->name; ?></option>

<?php } ?>

</select>

<input type="submit" name="tag_submit" value="submit">

</form>


Arnav Joy comments:

in which page you are looking your code in category.php , ok try this one

<?php



wp_reset_query();



if( is_category() ) {

$category = get_the_category();

if($category[0])

$cat_id=$category[0]->term_id;

query_posts('cat='.$cat_id.'&posts_per_page=-1');

}

else

query_posts('posts_per_page=-1');




if(have_posts()): while (have_posts()) : the_post();



$all_tag_objects = get_the_tags();



if($all_tag_objects){



foreach($all_tag_objects as $tag) {



if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}



}







}







endwhile;endif;







$tag_ids_unique = array_unique($all_tag_ids);







// Now use the following code for displaying the tags list with each tag having its own link.

?>

<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">

<select name="tag">

<?php

foreach($tag_ids_unique as $tag_id) {



$post_tag = get_term( $tag_id, 'post_tag' );



?>

<option value="<?php echo $post_tag->name; ?>"><?php echo $post_tag->name; ?></option>

<?php } ?>

</select>

<input type="submit" name="tag_submit" value="submit">

</form>

2012-12-17

Ellah Alzona answers:

Hello i think your problem will be solve using this plugin http://www.don-benjamin.co.uk/projects/foss/wp-plugins/wordpress-custom-search-plugin-01-beta/

Good luck.