• I’m looking for an elegant and flexible approach to a simple need:

    I have a series of merchants to display in various categories such as Retail, Restaurant, Entertainment, etc. There are pages to match each category with some intro text entered in the body field. They then show in the site’s navigation.

    To fill the above pages dynamically, I want to create a post for each merchant that has the name, description, image and so on with a relevant category selected.

    DESIRED DISPLAY

    With these two Pages and Posts elements set up, I want the merchant pages to show 3 things:

    1) Page title at the top (e.g., “Retail” or “Restaurant”, etc.)

    2) Brief intro text

    3) Beneath the above two, a Category loop that displays all posts that match the current page’s title.

    In other words, The Retail page would have a “Retail” title, a short intro about the retail shops, and then a loop of every Post that has the Retail category checkbox selected.

    BONUS) I want to use the WP SNAP! plugin that creates a alphanumeric horizontal list at the top of the Category loop containing just the posts that match the current category. It requires just one php hook within the loop like so:
    <?php if (function_exists('wp_snap')) { echo wp_snap('category_name=retail&child=true&firstload=all'); } ?>

    SOLUTION…?

    So, I assume I have to create a new template page (such as page-merchants.php) that contains some sort of get_posts or query_posts directive to grab the posts for given category and inject it into a matching page. Then each page would be directed to use this new template page file.

    Ideally, this would be just one template page file using multiple if/else statements that looks for a page’s slug and then injects the matching category posts (as opposed to a custom page template file for each category).

    CUMBERSOME SUCCESS(-ish)

    I was able to get almost what I needed with the cumbersome approach of creating multiple page templates for EACH page category and then putting the following code in each (using the Retail example again):

    <?php query_posts('category_name= retail&showposts=1000&orderby=title&order=asc'); ?>
    
    <?php if (function_exists('wp_snap')) { echo wp_snap('category_name=retail&child=true&firstload=all'); } ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    Once I set this I could get a nice loop of all the posts that have the Retail category checked. Unfortunately, this approach has 3 limitations:

    1) It kills the Page’s title and body text so that ONLY the titles and body content of the category Posts display. I have to hardcode the Page’s title and intro text into the individual template files.

    2) It requires I make a bunch of nearly identical template files that would require manual updating and management should anything change.

    3) Also, concerning proper SEO, I can hardcode a h2 header for the Page title within the template, but can’t find a way to make the category Post titles use an h3 header. They all display in h2 tags too.

    SINGLE TEMPLATE PAGE TRY

    As mentioned above, I’d like to get this category rendering code into a single template file that all of the Pages utilize (Retail, Restaurant, Entertainment, etc.). I found this simple code idea but it doesn’t even work:

    `<?php
    if ($page_id==’retail’){
    query_posts(‘category_name=retail&showposts=10&orderby=title&order=asc’);
    };
    if($page_id==’restaurant’){
    query_posts(‘category_name=restaurant&showposts=10&orderby=title&order=asc’);
    };
    if($page_id==’entertainment’){
    query_posts(‘category_name= entertainment&showposts=10&orderby=title&order=asc’);
    }
    ?>

    Seems like it’s missing else bits. Anyhow, this still would probably kill the Page’s title and intro body text so that I’d have to hardcode those somehow.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Look here for idea on using a page title to get posts in a category named the same as the page title:

    http://wordpress.org/support/topic/318531

    Thread Starter sassymonkey

    (@sassymonkey)

    That seems to be close, however it puts only the titles of the Category posts at the top of the page with the Page’s title and body content below.

    I can probably figure out how to add the full body text below each post title, but I’m not proficient enough with PHP to get the Page title and body content ABOVE the category posts.

    I also could not figure out where to place the WP SNAP! hook in order to get it to show below the Page title and body text and above the Category posts listing.

    Any one have a clue as to how to adjust the code from the above link?

    <?php
    // on a page, get category that has slug equal to the page slug, display posts in that category
    if ( is_page() ) {
      $page_name = $posts[0]->post_name; //or use $posts[0]->post_title
      $category = get_term_by('slug',$page_name,'category');
      if ($category) {
        $args=array(
          'category__in' => array($category->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Pages that Show Their Title and Text Plus a Category’s Posts’ is closed to new replies.