WordPress.org

Ready to get started?Download WordPress

Forums

Taxonomy in loop (14 posts)

  1. Amit Kvint
    Member
    Posted 1 year ago #

    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

  2. Maidul
    Member
    Posted 1 year ago #

    Hi,
    in the query_posts add posts_per_page = -1 Hope this will help
    http://codex.wordpress.org/Function_Reference/query_posts
    Thanks

  3. Amit Kvint
    Member
    Posted 1 year ago #

    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

  4. Maidul
    Member
    Posted 1 year ago #

    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

  5. Amit Kvint
    Member
    Posted 1 year ago #

    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

  6. Maidul
    Member
    Posted 1 year ago #

    Then change this to

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

    Thanks

  7. Amit Kvint
    Member
    Posted 1 year ago #

    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

  8. Maidul
    Member
    Posted 1 year ago #

    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

  9. Amit Kvint
    Member
    Posted 1 year ago #

    Maidul,

    Thanks : )...

    But now no post titles are shown...no error though

    Amit

  10. Maidul
    Member
    Posted 1 year ago #

    Where did you place this loop?

  11. Amit Kvint
    Member
    Posted 1 year ago #

    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

  12. Maidul
    Member
    Posted 1 year ago #

    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

  13. Amit Kvint
    Member
    Posted 1 year ago #

    I think so,

    I will take another look but first... a break,

    Me too

    Thanks,

    Amit

  14. Amit Kvint
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic