0

I want to display all my tags on one web page for example, www.example.com/tags/ and then when some one clicks on one of the tags I want to display all the posts related to that tag on another web page for example, www.example.com/tags/clicked-tag/ how would I be able to do this since it really would not be a good idea to create a web page for each tag?

I will be creating an index.php page for the tags folder www.example.com/tags/.

But how would I be able to display the posts related to that tag on another page for example, www.example.com/tags/clicked-tag/?

snag
  • 393
  • 1
  • 3
  • 5

3 Answers3

1

Mod_rewrite for generating get variable in "nice" form

You have to add mysql field called "tag" or something

Misiur
  • 5,019
  • 8
  • 38
  • 54
  • but where would all tags post be display at? – snag Aug 08 '10 at 00:14
  • You'll have to check if tag variable (f.e. isset($_GET['tag']) )exists, else display all. – Misiur Aug 08 '10 at 00:19
  • so I would display all the tags on web page along with the clicked tags post and it will still give the web page layout I'm looking for? – snag Aug 08 '10 at 00:22
1

One possibility is to use a rewrite rule. Create a PHP file named say "details.php" inside the tag folder.

Then, in httpd.conf:

RewriteRule /tags/([^/]+)/?$ /tags/details.php?tag=$1 [B,L]

or, if you must, in a .htaccess file inside /tags:

RewriteCond $1 !=details.php
RewriteRule ([^/]+)/?$ details.php?tag=$1 [B]

Another possibility would be to activate the option MultiViews and serve all requests to /tags/* from a file named tags.php in the root (using the path info to determine what to show).

Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • @snag I assumed you were using Apache. If you are, it has [an extension](http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html) usually available that allows you to rewrite URLs. – Artefacto Aug 08 '10 at 00:30
  • I was just wondering how would my pages look like before rewrite is what I meant. – snag Aug 08 '10 at 00:31
  • @snag Before rewrite they'll look the way you want. One a request to those URLs arrives the server internally rewrites them to `/tag/details.php` or whatever (the client doesn't see them). – Artefacto Aug 08 '10 at 00:32
1

Am I missing something here? You seem to want to display all posts for a given tag <my_tag> on the page www.example.com/tags/clicked-tag.php?

So, can't your tags link to www.example.com/tags/clicked-tag?tag=<tag_name>, where <tag_name> is different for each tag, obviously, and then www.example.com/tags/clicked-tag.php simply look at a variable $_GET["tag"]?

That seems the way to do it in PHP, but since others are giving rewrite answers I fear that I may have misunderstood the question. If so, maybe you could clarify?


i want all the tags to have their own unique file name like www.example.com/tags/tag1 www.example.com/tags/tag2 www.example.com/tags/tag3 and so on - thanks for the clarification.

How about ... mmmm ... all links on the first page lead to an intermediate page which the user will never see ... www.example.com/tags/clicked-tag.php for example, and that does something like

   if file www.example.com/tags/<tag> not exists then crete it  
   header("Location: www.example.com/tags/<tag>");

and each page includes a common file with a function which takes as a paramater and build the output page ...

That would do it, wouldn't it?

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551