0

I have the following CPT and Taxonomy in my WP:

CPT: magazine

  • Hierarchical: true
  • Has Archive: true

Custom Taxonomy: authorname (Magazine Author. Not the same as WP Author)

  • Attached to: CPT magazine

Custom Taxonomy: magazinename (name of the magazine)

  • Attached to: CPT magazine

URL structure:

  • hxxp://domain.zzz/magazine/author-name/magazine-name/magazine-page

How do I do the following:

  • If I go to hxxp://domain.zzz/magazine/author-name/ only the parent pages of that author are listed? (featured images of that page needs to be displayed)

  • If I go to hxxp://domain.zzz/magazine/author-name/magazine-name/ only the first page is displayed, along with a listing (navigation) of other pages + current page belonging to both those taxonomies?

  • If I go to hxxp://domain.zzz/magazine/author-name/magazine-name/magazine-page/ only that page is displayed with a listing of other pages belonging to both taxonomies?

What .php files do I need to create in the theme folder, and how will the queries look in this files?

Thanks in advance for any answers.

Kr,

Andy

EDIT:

I have following code in single-magazine.php:

<?php get_header(); ?>

<?php

$query = new WP_Query( array( 'post_type' => 'magazine') );

while ( have_posts() ) : the_post(); ?>
<div class="entry magazine">
    <?php the_content(); ?>
</div>
<?php endwhile; // end of the loop. ?>

 <div class="navigation">
 <p>nav single-magazine.php</p>
 </div>

 <?php get_footer(); ?>
  • What research have you done so far to solve your issues? It's helpful to users who want to answer if they know what you've tried so far. It may be beneficial to edit your question with some code that you've tried. – Howdy_McGee Jun 23 '15 at 20:23
  • I've edited my question. I can display a single magazine page via single-magazine.php, but now I'm stuck with the rest of the question :/ – andyderuyter Jun 24 '15 at 5:16
  • 1
    Your taxonomy cannot be called author. author is a reserved name and query variable in Wordpress. – Pieter Goosen Jun 24 '15 at 5:18
  • If I read your URL structures correctly and what you want, this is going to be a big project to work on. That kind of hierarchy does not exists in Wordpress, so it is something that you will need to create. This requires some new rewrite rules, new conditional tags outside the normal ones and pre_get_posts to adjust your query. Templates are quite simple, all you need is a unique template name outside of hierarchy, the template_include filter, and the default loop, nothing special here – Pieter Goosen Jun 24 '15 at 6:04
  • I'm not entirely sure, but some parts in my answer here might come in handy for this project – Pieter Goosen Jun 24 '15 at 6:08
0

Okay, it seemed that going with more than 1 taxonomy was just wishful thinking.

I settled with this:

CPT still is 'magazine'

Hierarchical taxonomy terms were used instead:

Parent: The author name Children: The magazines that each author will publish.

With the code below I managed to get a working taxonomy-authorname.php page:

<?php
$taxonomies = array( 
    'authormagazine' // the custom tax
);

$args = array(
    'orderby'           => 'id', 
    'order'             => 'ASC',
    'hide_empty'        => true,
    'fields'            => 'all', 
    'slug'              => 'andy' // the slug of the author name parent term
); 

$terms = get_terms($taxonomies, $args);
$term_id = $terms[0]->term_id;


$taxonomy_name = 'authormagazine';
$termchildren = get_term_children( $term_id, $taxonomy_name );

foreach ( array_reverse($termchildren) as $child ) {

    echo '<div class="row">';
    $term = get_term_by( 'id', $child, $taxonomy_name );

    // Here we get the permalink of the first post from each magazine
    $args2 = array(
        'post_type' => 'magazine',
        'tax_query' => array(
            array(
                'taxonomy' => 'authormagazine',
                'field' => 'slug',
                'terms' => $term->slug
            ),
        'posts_per_page' => 1
        ),
    );

    $query = new WP_Query($args2);
    $firstpost_link = $query->posts[0]->ID;

    echo '<div class="col-md-6 mag-title">';
    echo '<h2><a href="' . get_permalink( $firstpost_link ) . '">' . $term->name . '</a></h2>';
    echo '</div>';

    echo '</div>'; // end row
}


?>

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.