WordPress.org

Support

Support » How-To and Troubleshooting » Taxonomy in loop

Taxonomy in loop

  • Hi there,

    So I have created 2 new taxonomies in my fuctions.php :

    // Adding Taxonomy !

    function create_my_taxonomies() {
    register_taxonomy(‘locations’, ‘post’, array(
    ‘hierarchical’ => true, ‘label’ => ‘Locations’,
    ‘query_var’ => true, ‘rewrite’ => true));

    register_taxonomy(‘activities’, ‘post’, array(
    ‘hierarchical’ => true, ‘label’ => ‘Activities’, ‘query_var’ => true,
    ‘rewrite’ => true));
    }

    add_action(‘init’, ‘create_my_taxonomies’, 0);

    ?>

    And now I try to use them in my loop like that –

    <?php query_posts(array(‘locations’ => ‘vejer’)); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <h2>

    rel = “bookmark” title = “
    <?php the_title_attribute(); ?>”
    alt = “
    <?php the_title_attribute(); ?>”
    >
    <?php the_title(); ?>

    </h2>
    <?php endwhile; ?>
    <?php else : ?>
    <h2>Sorry nothing Found</h2>
    <?php endif; ?>

    This works perfect, but I have a simpler case that i can’t seem to sort out – what if I want to display all the posts that have taxonomy ‘locations’ ?

    Thanks,
    Amit

Viewing 13 replies - 1 through 13 (of 13 total)
  • Hi,
    in the query_posts add posts_per_page = -1 Hope this will help
    http://codex.wordpress.org/Function_Reference/query_posts
    Thanks

    Maidul,

    Wouldn’t that just show all posts ? I am looking for something that will show all posts with the specific ‘location’ taxonomy.

    Thanks
    Amit

    Hi Amit,

    This should display posts tagged with ‘vejer’, under ‘locations’ custom taxonomy
    query_posts(array(‘locations’ => ‘vejer’, ‘posts_per_page’ => -1 ));

    Let me know the outcome

    Thanks

    Thanks Maidful,

    But this shows all post that are under ‘location’ with the value of ‘vejer’ (‘locations’ => ‘vejer’) & I am looking for something that will show all posts that are under locations, any idea ?

    Amit

    Then change this to

    query_posts(array('taxonomy' => 'locations', 'posts_per_page' => -1 ));

    Thanks

    Hey,

    Yeah I know, have tried that too…but I get also posts that are defined as ‘activities’… (set in functions.php as I mentioned earlier) don’t know why…

    Amit

    Hi Amit,

    Can you try this one

    $args = array(
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'locations'
    		)
    	)
    );
    $the_query= new WP_Query( $args );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	echo '<li>';
    	the_title();
    	echo '</li>';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();

    Thanks

    Maidul,

    Thanks : )…

    But now no post titles are shown…no error though

    Amit

    Where did you place this loop?

    Maidul,

    What do you mean ? it goes in the index.php in a div I have made on the bottom for these links, here take a look at the URL – http://educational-adventures.org/ea/

    You can see the post for vejer (‘location’ => ‘vejer’) I have replaced that line with the new vars & arrays you suggested

    Amit

    Did you properly coded the loop ?

    Please find all the info here

    http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

    I am confused why this is not working

    Let me know

    Thanks

    I think so,

    I will take another look but first… a break,

    Me too

    Thanks,

    Amit

    So I have found that little script –

    <?php
    $taxonomies = get_the_term_list($post->ID, 'locations', '', '', '');
    $taxonomies = explode('>', $taxonomies);
    $taxonomies = $taxonomies[1];
    $myq = new WP_Query('locations = '.$taxonomies);
    if ($myq->have_posts()) : while ($myq->have_posts()) : $myq->the_post(); ?>
    
    		<h2>
    		<a href = "<?php the_permalink(); ?>"
    		rel = "bookmark" title = "
    		<?php the_title_attribute(); ?>"
    		alt = "
    		<?php the_title_attribute(); ?>"
    		>
    		<?php the_title(); ?>
    		</a>
    		</h2>
    <?php endwhile; else:?>
    
    <?php endif;?>

    But still no luck – it shows all posts doesn’t mattar what taxonomy they are attached to, I will give up @ the end…

    Amit

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Taxonomy in loop’ is closed to new replies.
Skip to toolbar