• Resolved staceppa

    (@staceppa)


    hello everyone
    i have tried to handle this myself by reading lots of documentation but i seem not to be able to solve it, so here i am asking for some help.

    i am trying to create a page that will show all the posts that have a certain tag, like “vacation”.

    i have read this doc here
    https://codex.www.ads-software.com/Template_Tags/query_posts

    so i created a template page called “tag.php” and put the following code in it

    <?php
    /**
          * Show latest post for Tags
          *
          * @return
          * @param object $tag
          * @param object $show[optional]
          */
          function show_title_tag( $tag, $show=10 ) {
    	      global $post;
    	      $posts = query_posts('tag=' . $tag . '&showposts=' . $show );
    	      echo "<ul>";
    	      foreach ($posts as $post ) : setup_postdata($post); ?>
    	      <li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
          <?php endforeach;
          echo "</ul>";
          }
    ?>

    then i created a new page, associated this page with the template page called “tag”.

    at this point, i don’t know how i can recall the page that shows the posts with a certain tag.

    i am not sure i did everything as i was supposed to, maybe i just don’t understand where i should put the code with the query_posts() and how i should recall the page with the parameters.

    any help? thank you!

Viewing 13 replies - 1 through 13 (of 13 total)
  • If I understood your request correctly, you do not need a custom page template… you can use conditional query..
    but .. if you DO want a tag.php , you need to create the template and add this :
    query_posts(‘tag=bread+baking+recipe’);

    and then , what you want to show, like title, writings, divs, styling, excerpt, images etc …
    asically, just take the archive.php, duplicate it and change the query..

    Thread Starter staceppa

    (@staceppa)

    ok, how does it work if i don’t create a template page?
    where do i put this code?
    query_posts('tag=bread+baking+recipe');
    thanks!

    Let us make things a bit clearer.

    By default your archive.php will show the posts corresponding to a particular tag, if the name of the tag is clicked on the tag cloud or a list of tags.

    If you want to have a sperate page showing posts from a particular tag or set of tags, make a page template and add the query_posts snippet ahead of the loop, and associate this template to the special page that you create.

    S.K

    Thread Starter staceppa

    (@staceppa)

    ok great!, it seems to work… the only problem that i still have is that when i click on “previous entries” i get 404 not found.
    https://www.noiduepercaso.com/tag/

    how can i solve this?
    thanks!

    404 because there is no SECOND page for tags at the moment , not with YOUR permalink structure..

    @staceppa,

    If you have more posts tagged with the selected tag(s) and the previous entries link returns 404, then we have an issue.

    If you are using a single tag only, then you try making a template like “tag-slug.php” (slug=your-selected-tag-slug) and link it as “https://www.noiduepercaso.com/tag/your-tag&#8221;. You may try if this solves the previous entries issue, if it is a real problem.

    S.K

    Tag pages are like archive.php and category.php files, they are not templates, but an archive page.

    Just create a copy of your archive.php (or category.php), rename it tag.php … you don’t need to do anything with the query_posts line ….

    https://codex.www.ads-software.com/images/1/18/Template_Hierarchy.png

    As kichu said above, you can do this on a per-tag basis to (like categories), with tag-NAME.php , where NAME is the name of a tag…

    Just create a copy of your archive.php (or category.php), rename it tag.php …

    Like I wrote 7 posts ago ??

    you don’t need to do anything with the query_posts line …

    IMHO , if he wants to use more than one tag, he should cahnge the query line to accomodate more tags ..

    Thread Starter staceppa

    (@staceppa)

    great! it works now
    i created a page called tag-MYTAG.php and recalled the page as
    https://URL/tag/MYTAG

    one last thing… with the query_posts(‘tag=bread+baking+recipe’); i could decide the order, like ASC, can i do the same with this?

    I think you can use this string before the loop without affecting the rest of the parameters.

    query_posts($query_string . "&order=ASC");

    S.K

    Thread Starter staceppa

    (@staceppa)

    great!
    it works
    thank you very much people!

    prego !

    If you want your posts to be sorted by tags, like

    Tag 1
    – post 1
    – post 2
    – etc

    Tag 2
    – post 1
    – post 2

    You can use Multiple Loops.

    Create a page template based on ‘archive.php’ and call it e.g. ‘tag-archive.php’

    Put

    <?php
    /*
    Template Name: Tag list
    */
    ?>

    in your first lines.

    In this template you create a loop
    Rewind your posts
    Create the second loop
    Upload the template in your theme directory
    Create a page based on this new template

    Example:

    <?php } ?>
    <?php query_posts('tag=fun'); ?>
    <div class="posttitle"><h2>These posts were fun!</h2></div>
    
    <?php while (have_posts()) : the_post(); ?>
    
    <!-- do your stuff -->
    
    <?php endwhile; ?>
    
    <?php rewind_posts(); ?>
    
    <?php query_posts('tag=pleasure'); ?>
    <div class="posttitle"><h2>All Pleasure posts!</h2></div>
    <?php while (have_posts()) : the_post(); ?>
    
    <!-- do the stuff again -->
    
    <?php endwhile; ?>
Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘show only posts for a tag’ is closed to new replies.