• I trying to code a page which displays category heading with 3 posts which contain a single tag.

    In essence the tag is acting like a parent category.

    I can list all the posts which have the tag no problem but I can’t seem to filter the categories.

    So I’m looking to display…. just for example

    Tag – Germany (page)

    Mercedes
    -Car 1
    -Car 2
    -Car 3
    Audi
    -Car 1
    -Car 2
    -Car 3
    BMW
    -Car 1
    -Car 2
    -Car 3

    I can’t set Germany as a parent category (which would be soooo much easier I know)

    Any ideas…

Viewing 1 replies (of 1 total)
  • Thread Starter hilmon

    (@hilmon)

    Ok this is a real hack and is a little too reliant on elements having the same name but here’s the solution I came up with.

    I created a page for each of the tags and gave them the same name.

    In page.php ….

    <?php
    $post_obj = $wp_query->get_queried_object(); // Get post data outside the loop
    $post_name = $post_obj->post_name; // Post Name and Tag must match
    $post_title = $post_obj->post_title;
    
    $args=array(
      'orderby' => 'name',
      'order' => 'ASC'
      );
    $categories=get_categories($args); // get array of all categories
      foreach($categories as $category) { // loop thru each category
      $tag = explode(" ",$category->name); // get first word of category
      $title = explode(" ",$post_title); //get first word of page title
      if ( $title[0] == $tag[0]) { // if match .... the page matches category matches tag ..... !!!
      echo '<h1><a>term_id ) . '" title="' . sprintf( __( "View all news in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></h1>'; ?>
    
    <ul>
      <?php
      query_posts ( array ( 'category_name' => $category->name, 'posts_per_page' => -1 ));
     while (have_posts()) : the_post(); ?>
    
      <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php  endwhile;
      wp_reset_query();?>
      </ul>
      <?php }
    	}
      ?>

    I’m sure there is a WP query which could have done the same job a lot better …. monday morning deadline :-/

Viewing 1 replies (of 1 total)
  • The topic ‘Display Categories & Posts based on single Tag’ is closed to new replies.