WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to loop tags in custom post type (3 posts)

  1. modeman
    Member
    Posted 1 year ago #

    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

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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/
    http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

  3. modeman
    Member
    Posted 1 year ago #

    Thanks, works perfect!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.