0

I'm new to Wordpress and I've managed to build most of a full custom theme for wordpress on my own.

But the tricky stuff is starting to show its tail, and I've spent the last days stuck on Taxonomies. I just managed to put a code for registering a custom Taxonomy to sort my articles by level of difficulty.

Now I need to work on a taxonomy.php (maybe I'm wrong here) page template where it will display articles by their Category (ex : "Design") AND the difficulty (ex : "Advanced") the reader will choose.

Frankly, staying on this and searching all the web, trying things and failing, plus me not being that good has somewhat burnt my brain, and I have no clue about how to do it! (well no, I guess it has something to do with the post query loop and the taxonomy term and the category term being in the link to the page (maybe i'm wrong here too, probably !)

  • Some relevant code would be nice. Btw you know wordpress comes with categories by default, right? – Fleuv Oct 20 '15 at 15:45
  • Hi, yes I know this :) and I use it. – HiD3f Oct 20 '15 at 16:08
  • In my blog I have like 6-7 categories for my articles (.Net/Java/Web/Agile/...), but I needed to add a custom taxonomy to let the user sort them by difficulty (Beginner, Intermediate, Advanced). So I created this taxonomy (well, found it, and modified it). Now I need to know how to adapt the wp_query on whatever file i have to put the loop in, so it takes the category taxonomy AND the level taxonomy to display results. If you really need to see my taxonomy code I can put it, but i dont think it is relevant, is it? – HiD3f Oct 20 '15 at 16:14
1

You don't need to create a taxonomy page. The page in which you want to display your posts that have been added to the custom taxonomy you created should use a format like this https://codex.wordpress.org/Class_Reference/WP_Query

Read that page. Here is some code in which I get all the posts in my custom taxonomy. This is in my plugin L7 Admin Help Videos on Wordpress.org. Slightly modified for this example.

$args = array(
            'post_type' => 'post',
            'tax_query' => array(
                array(
                    'taxonomy'  => 'your-custom-taxonomy-slug',
                    'field'     => 'slug',
                    'terms'     => 'your-term',
                    ), 
                ),
            'post_status' => 'publish',
            'no_found_rows' => true,
);

This argument array should be put into your WP-query function like this:

$my_query = new WP_Query( $args );

Then you loop through the results like this:

if ( $my_query->have_posts() ) {
        while ( $my_query->have_posts() ) : $my_query->the_post();
             // Echo all the stuff from a post here
        endwhile;
}

This is a pretty basic example but you should read the codex:

https://codex.wordpress.org/Class_Reference/WP_Query https://codex.wordpress.org/Taxonomies https://codex.wordpress.org/Function_Reference/register_taxonomy

  • OK. I'll be looking into this tomorrow morning! Thank you very much :) I'll come back for the feedback ! – HiD3f Oct 20 '15 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.