Support » Developing with WordPress » Help with site hierarchy

  • Resolved wpdsbarnes

    (@wpdsbarnes)


    I’m 100% sure this has been answered several times, but I cannot for the live of me find an implementation that works.

    I have this structure:
    some_fake_site/topics/math/arithmetic/lesson_0.html

    In the main nav are the various topics.
    When I navigate to one, I want to see the various subjects in that topic.
    So on /topics/math, I see arithmetic, geometry, algebra, etc.
    When I click on one of those topics, I want to arrive at a list of lessons for that topic. /topics/math/geometry should be a list of geometry lessons.

    That sounds so much easier than…

    Ideally, I’d like to accomplish this with categories and the loop,

    In the admin panel, I have post categories set up such that there are only posts in child categories. (No single post is tagged algebra, because that’s a list not a single, right).

    I’ve been using this documentation to lead myself here:

    (I’m very, very aware this code doesn’t work. Very.)

    if ( have_posts() ) {
    		// If the category page is a top level category
    		// display links to the child categories
    
    		// else (It's a child category)
    		// display each post in the child category
    
    		$category = get_category( get_query_var( 'cat' ) );
    		$cat_id = $category->$cat_ID;
    		$the_query = new WP_Query( array( 'category__in' => $cat_id ) );
    
    		echo "Cat ID: " . $cat_id; // null? Wtf?
    
    		while ( $the_query->have_posts() ) {
    			$the_query->the_post();
    			get_template_part( 'template-parts/content/content', get_theme_mod( 'display_excerpt_or_full_post', 'excerpt' ) );
    		}
    
    	} else {}

    If I had pages that were in a top-level category, It would list those.
    How would I do this backwards – If it’s a category that is a top-level category, list the names of the subcategories that belong to ‘this’ category. Once I click that, I’m to the list of ‘lessons’

    Any and all help is appreciated.
    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Given a category term object, you can verify it’s top level by checking its parent property. Top level terms will have a value of 0. On a category archive template, you can get the requested term object with get_queried_object().

    You can get a top level term’s immediate children with get_categories(), passing the top level ID as the “parent” argument. Use “child_of” instead of you also want greater descendants beyond immediate children.

    If the term’s parent property is not 0, then query for posts having that term assigned.

    FYI, even if the top level term is not assigned to a post, it’s still implicitly part of the top level category. If you were to query for posts assigned “math”, you’d get all posts with any of math’s children, even though none actually have “math assigned. Shouldn’t be an issue here, but something to keep in mind.

    Thread Starter wpdsbarnes

    (@wpdsbarnes)

    Thank you so much! I had myself in a … loop (had to).

    Basically one just needs to say, if this category has children, links pls,
    otherwise, normal archive page.

    In the child theme I copied the contents of “archive.php” to “category.php” (So, archive.php will be a show all page) which looks like this for the next person that wonders:

    <?php
    /**
     * The template for displaying archive pages
     *
     * @link https://developer.ww.wp.xz.cn/themes/basics/template-hierarchy/
     *
     * @package WordPress
     * @subpackage Twenty_Twenty_One
     * @since Twenty Twenty-One 1.0
     */
    
    get_header();
    $description = get_the_archive_description();
    ?>
    
    <?php if ( have_posts() ) : ?>
    	<header class="page-header alignwide">
    		<?php the_archive_title( '<h1 class="page-title">', '</h1>' ); ?>
    		<?php if ( $description ) : ?> <div class="archive-description"><?php echo wp_kses_post( wpautop( $description ) ); ?></div> <?php endif; ?>
    	</header><!-- .page-header -->
    
    	<?php $category_id = get_query_var( 'cat' ); ?>
    	<?php $parent_categories = get_categories( array('child_of' => $category_id) ); ?>
    	
    	<div class="entry-content">
    		<?php foreach($parent_categories as $category) { 
    		//  echo title of the subcategory as a link
    			echo '<p> <a href="' . get_category_link( $category->term_id ) .  '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' .  $category->name.'</a> </p>';
    		} ?>
    	</div>
    
    	<?php if( ! $parent_categories ){
    		while ( have_posts() ) : 
    			the_post();
    			get_template_part( 'template-parts/content/content', get_theme_mod( 'display_excerpt_or_full_post', 'excerpt' ) );
    		endwhile;
    	} ?>
    
    <?php else : ?>
    	<?php get_template_part( 'template-parts/content/content-none' ); ?>
    <?php endif; ?>
    
    <?php get_footer(); ?>

    Resolved

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Help with site hierarchy’ is closed to new replies.