Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Show posts in Category with Tag (Wordpress) WordPress

  • SOLVED

Hi everyone,

I am trying to display a lists of posts on a custom post template that are in a 'category' and that have a specific 'tag' that matches the title of the post.

For example I have posts by city name,

* New York
* Chicago
* Texas
* ...etc

I then have several categories,

* News
* Events
* Classifieds
* ...etc

For every post I make to one of those categories I then assign a post tag that matches the name of the city.

For example if I have a news item for New York, I select the 'News' category and assign the post the 'New York' tag.

When I am on the custom post template page for New York I want to retrieve posts associated with the News category that have the tag name = New York (the title of the post).

However my dilema is that I can not figure out how to dynamically generate the tag name from the title of the post.

Eg.


<?php
$tag = wp_title('', FALSE);
query_posts( 'tag=' . $tag . '' );

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

echo '<li>';

the_title();

echo '</li>';

endwhile;

wp_reset_query(); ?>

But when getting the tag name using wp_title() it would print 'New York' with a space between the words 'New' and 'York'. This doesn't work.

To get it to work it would need to be 'new-york' with the hyphen in place. But I can not figure out how to generate the tag name based on title with hyphens in place of spaces.

I hope this makes sense.

I appreciate any and all efforts in helping.

Thank you.

Answers (3)

2011-04-25

David Navarrete answers:

try this

$tags = strtolower(str_replace(' ', '-', wp_title('', false)));


or get the category_name with

global $category_name if you are in category.php

for complete

global $category_name;
query_posts("$query_string&tag=$category_name");


greatings


Wordpressing comments:

Hi David,

If I do the following,


<?php $tags = strtolower(str_replace(' ', '-', wp_title('', false)));
query_posts('category_name=News&tag='.$tag.'&showposts=5'); ?>

<?php while (have_posts()) : the_post(); ?>

<ul>

<li>

<?php the_title(); ?>

</li>

</ul>

<?php endwhile; ?>


...it outputs all posts associated with the category 'News' but does not filter by cat='.$tag.'

If I replace the code with,



query_posts('category_name=News&tag=my-tagname-here&showposts=5'); ?>

<?php while (have_posts()) : the_post(); ?>

<ul>

<li>

<?php the_title(); ?>

</li>

</ul>

<?php endwhile; ?>


Then it filters by both the category of 'News' and whatever tag name I insert.

So it seems I can't get the cat= to work with tag= $tag (variable)

Thank you for taking the time to look at my question (and also to Derek below)).


David Navarrete comments:

where put the code? category.php? give me the name of file. ( or template )


Wordpressing comments:

Also it's worth while mentioning I am not in category.php

I am using a custom post type to display for example,

www.domain.com/new-york

This page then pulls in data from various categories or meta information associated with the custom post type.

Otherwise if that suggestion would have been perfect almost..


David Navarrete comments:

try with this


<?php
global $post_type;
query_posts("category_name=news&tag=$post_type&showposts=5"); ?>



<?php while (have_posts()) : the_post(); ?>



<ul>



<li>



<?php the_title(); ?>



</li>



</ul>



<?php endwhile; ?>


the "category_name" you must use the slug of category, not the name

greatings


Wordpressing comments:

I have created a custom post template named,

single-city.php

Via the dashboard menu I have also added an option to "add new city" like you would a new post.

This custom post type is designed with special meta boxes etc that are applicable to this particular post type.

Normal blog posts are then made to each of the categories,

News
Events
Classifieds

The custom post template for the city (i.e. New York) is then supposed to pull in all items from each of the categories where post tag = city name (or the title of the post)

Because I am constantly adding cities it would be impractical to add each city as a category and then create news, events and classified sub-categories for each. That'd be a nightmare.


David Navarrete comments:

you need that search in every category if post type? or, if you go to the category new-york filter the content to this category? if you need the category.. try this

<?php

global $category_name;

query_posts("category_name=news&tag=$category_name&showposts=5"); ?>







<?php while (have_posts()) : the_post(); ?>







<ul>







<li>







<?php the_title(); ?>







</li>







</ul>







<?php endwhile; ?>


Wordpressing comments:

Hi Dave,

I don't think that would work you see...

The category = news
The tag = the 'title' of the custom post type which in this case is the City Name (i.e. New York)

So I need to return posts that are conditonally meet the requirements of being in the category 'news' and have the tag 'new-york'

I recon you first suggestion was along the right lines, by getting the title of the post and replacing the spaces between words with hyphens would then equal the slug of the tag

But I couldn't get the following to work...


$tags = strtolower(str_replace(' ', '-', wp_title('', false)));
query_posts('category_name=news&tag='.$tag.'&showposts=5');


David Navarrete comments:

as i have understood,

if you get the slug of post type ( the post type give the name to the tag )

ex: www.qwerty.com/new-york

if you print the $post_type this should show "new-york" then if you tag ( "New York") have the same slug, should get all of post with this tag.

now, test with this.

query_posts("tag=$post_type");

if you get the posts with tag new-york the quuery_posts work and you have some problem with the category.

bye


Wordpressing comments:

Sorry David to be precise the structure is as follows,

www.domain.com/city/new-york

When creating a custom post type you need to define your slug in your functions file or it grabs it's slug from the register_post_type (in functions.php)

So it's prepends city to the name of the city i.e. city/new-york/






David Navarrete comments:


query_posts(array('category_name' => 'news', 'tag' => $post->post_name, 'showpost' => 5));


as you can tell me, your structure /post_type/post

you should call the slug of the post.


bye


Wordpressing comments:

David what about this...

I've assigned the tag name of "new-york" to the post which is also titled "New York", now if I run the following...

<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->slug . ' ';
}
}
?>

It echo's "new-york"

So if I can somehow turn that into something usable within my custom query I'd be set... but I don't know how to turn that value into something other than an echo statement


Wordpressing comments:

Ah... I think that worked! Let me run some more tests


David Navarrete comments:

i have a liltle error is showposts no showpost. in the query_posts.


bye


Wordpressing comments:

One thing I forgot to ask whether or not its possible to NOT show any posts if there is no tag assigned because this code will show all posts for a city that has no items with no post tag associated with it


Wordpressing comments:

So for example

if city Chicago has no items in the category 'news' with tag equal to 'chicago' then do not display anything.

As it stands it's displaying all the posts IF it does not have a post in that category with a tag that matches it's city name.


Wordpressing comments:

Ignore my last two replies above this one as this problem only occurs if under post draft and preview mode.

2011-04-25

derekshirk answers:

From what I understand of your question, you would benefit from creating Category Page Templates.

You can check them out here:

http://codex.wordpress.org/Category_Templates

Pay attention to how you can specify the category template by naming your template file category-ID.php. It can be helpful to take your existing category.php and duplicate it, then rename it.

-----

You could also make use of the Query Posts, which you can read about here:

http://codex.wordpress.org/Function_Reference/query_posts

----

I'd be happy to help you out with this more if you send me an email directly.

2011-04-25

Sébastien | French WordpressDesigner answers:

try this

<?php

global $post;
$tag=$post->post_name;

query_posts( 'tag=' . $tag . '' );
if ( have_posts() ) while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
wp_reset_query(); ?>


Wordpressing comments:

Ah sorry Sebastien I just saw your reply after I had awarded David the votes.

Your solution also works!!! Thank you.

So for everyone viewing this thread the solution to my problem can either be what Sebastien has provided which is,



<?php

global $post;

$tag=$post->post_name;



query_posts( 'tag=' . $tag . '' );

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

echo '<li>';

the_title();

echo '</li>';

endwhile;

wp_reset_query(); ?>


...and or David's solution of,

query_posts(array('category_name' => 'news', 'tag' => $post->post_name, 'showpost' => 5));


Thanks to Derek for also helping and offering to help via e-mail.

Appreciated!