WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to loop tags in custom post type

[Resolved] How to loop tags in custom post type

  • Hello,
    i have using custom post type named “katalogas” and i want to show all posts by choosen tags in tag.php file, now i have code that shows absolutely all posts from all categories.

    How to show only posts with selected tag?

    My code: http://pastebin.com/aBUz5dxg

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    @keesiemeijer

    You can use this way of querying on a tag template file (tag.php):

    <?php
    $current_tag = get_query_var('tag');
    
    query_posts(array(
    	'tag' => $current_tag,
            'post_type' => 'katalogas'
    ));
    ?>

    But a better way would be without a query_post on your tag template file (tag.php) and with this in your theme’s functions.php:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        // only use the post_type katalogas on tag pages
        if(is_tag()){
          $query->set('post_type', array('katalogas'));
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    http://www.billerickson.net/customize-the-wordpress-query/
    Querying Posts Without query_posts

    Thanks, works perfect!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] How to loop tags in custom post type’ is closed to new replies.