• Resolved slideaway

    (@modsuperstar)


    I am trying to build a restaurant menu structure as a single page, organized by category. I currently have it setup to display all the entries on one page, in groups according to the category. The only thing I haven’t been able to figure out it how to get the category title to only display once. Obviously if I put it in the loop it shows with each entry.

    I’ve found this is the code I have so far

    <?php if (is_page(menu)) { ?>
    
    <?php $recent = new WP_Query("cat=".get_cat_id('menu')."&showposts=50");
    while($recent->have_posts()) : $recent->the_post();?>
    
    <?php foreach((get_the_category()) as $category) {
        echo $category->cat_name . ' ';
    } ?>
    
    			<div id="post-<?php the_ID() ?>" class="<?php sandbox_post_class() ?>">
    				<h2 class="entry-title"><?php the_title() ?></h2>
    				<div class="entry-content">
    <?php the_content() ?>
    <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'sandbox' ) . '&after=</div>') ?>
    <?php edit_post_link( __( 'Edit', 'sandbox' ), '<span class="edit-link">', '</span>' ) ?>
    				</div>
    			</div><!-- .post -->
    <?php endwhile; ?>
    <?php } ?>

    Now I was looking at his page about multiple loops https://codex.www.ads-software.com/The_Loop#Multiple_Loops_in_Action but I can’t figure out how I would go about getting that concept to function with what I’m currently working with. I’m not a PHP guy, so I’m just trying to combine parts here and hope I can get it working.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Here’s a different way to get there:

    <?php
    //for each category, show 3 posts
    $cat_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => 3,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
          if ($posts) {
            echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>
    Thread Starter slideaway

    (@modsuperstar)

    That works perfectly. Thank you very much, greatly appreciated.

    This is great. I was wondering if its possible to grab the images from the posts as well. I am currently using Justin Tadlock’s get_image script/plugin but its not grabbing anything. Ermm…im not really a php coder, really appreciate the help. Tks!

    How could I modify this so that instead of just displaying 3 posts it displays them all?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show category name once PHP’ is closed to new replies.