• I have my site set up like this: a custom field called “type” is in my posts and I want it to be so that the page organizes posts by the type. For example, all posts with the custom field “type” set to “r” are in one box, but all posts with the custom field “type” set to “a” are in another box and displayed differently. If you want an idead of what i mean, look at https://www.dcritic.com or https://www.dcritic.com/idea.gif .

    Thanks,
    Ian

Viewing 8 replies - 1 through 8 (of 8 total)
  • You are much better off using WordPress’s builtin category system than trying to do this with custom fields.

    here is an example of code pulling posts from two categories, each displaying in its own box. Your stylesheet styles the boxes

    <div class="featured">
      <?php $recent = new WP_Query("cat=1&amp;showposts=1");
      while($recent->have_posts()) : $recent->the_post();?>
        <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <?php the_content(__('Read the story &raquo;'));?><div style="clear:both;"></div>
      <?php endwhile; ?>
    </div>
    
    <div class="featured">
      <?php $news = new WP_Query("cat=2&amp;showposts=1");
      while($news->have_posts()) : $news->the_post();?>
        <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <?php the_content(__('Read the story &raquo;'));?><div style="clear:both;"></div>
      <?php endwhile; ?>
    </div>

    where cat=1 means category ID of 1 showposts is how many posts to include

    Thread Starter dcritic

    (@dcritic)

    If I use this, is there a way to make the used categories not show up on the side?

    if your categories are displayed using wp_list_categories, yes – see
    https://codex.www.ads-software.com/Template_Tags/wp_list_categories about “exclude”

    Thread Starter dcritic

    (@dcritic)

    Thank you very much for your support! ??

    Thread Starter dcritic

    (@dcritic)

    Uh… wait. There’s a little bug. When I use this little piece of code the rows go on and on. Check on https://www.dcritic.com and click on USA.
    Here’s the loop I’m using:

    <?php $restaurants = new WP_Query("cat=6&amp;showposts=1");
    while ($restaurants->have_posts()) : the_post(); ?>
    			<tr>
               <td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>
    		   <td><?php echo c2c_get_custom('rating', '', '', '--'); ?></td>
    		   <td><?php echo c2c_get_custom('price', '', '', '--'); ?></td>
    		   <td><?php echo c2c_get_custom('ambiance', '', '', '--'); ?></td>
    		   <td><?php echo c2c_get_custom('service', '', '', '--'); ?></td>
    		   <td><?php echo c2c_get_custom('taste', '', '', '--'); ?></td>
    		   <td><a href="https://<?php echo c2c_get_custom('website', '', '', '--'); ?>"><?php echo c2c_get_custom('website', '', '', '--'); ?></a></td>
    		   <td><?php edit_post_link('Edit', '', '&nbsp;'); ?></td>
    			</tr>
        <?php endwhile; ?>

    Note that I’m using a custom fields plugin.

    Thread Starter dcritic

    (@dcritic)

    Never mind; forgot a variable.

    Thread Starter dcritic

    (@dcritic)

    Okay, for some reason it displays all of my posts on each category. If you go to USA then go to Louisiana, it displays the same: no travel guides (yes I used travel guides and the categories didn’t work) and every food post. Here’s my loops:

    $cat = ",".get_query_var('cat');?>
    
    <?php if (have_posts()) : ?>
    
    <div id='page'>
    <div class="post" id="restaurants">
    			            <fieldset>
    
                <legend class='title'>Restaurants <?php echo $cat; ?></legend>
    
                <div class="entry">
    
    	<table>
    		<tr><td width="200"><b>Restaurant Name</b></td>
    		<td><b>Rating</b></td>
    		<td><b>Price</b></td>
    		<td><b>Ambiance</b></td>
    		<td><b>Service</b></td>
    		<td><b>Taste</b></td>
    		<td><b>Website</b></td>
    		</tr>
        <?php $restaurants = new WP_Query("cat=6".$cat);
    	while ($restaurants->have_posts()) : $restaurants->the_post(); ?>
    			<tr>
               <td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>
    		   <td><?php echo c2c_get_custom('rating', '', '', '--'); ?></td>
    		   <td><?php echo c2c_get_custom('price', '', '', '--'); ?></td>
    		   <td><?php echo c2c_get_custom('ambiance', '', '', '--'); ?></td>
    		   <td><?php echo c2c_get_custom('service', '', '', '--'); ?></td>
    		   <td><?php echo c2c_get_custom('taste', '', '', '--'); ?></td>
    		   <td><a href="https://<?php echo c2c_get_custom('website', '', '', '--'); ?>"><?php echo c2c_get_custom('website', '', '', '--'); ?></a></td>
    		   <td><?php edit_post_link('Edit', '', '&nbsp;'); ?></td>
    			</tr>
        <?php endwhile; ?>
    	</table>
                </div>
    
            </div>
    
    <div class="post" id="travel">
    			            <fieldset>
    
                <legend class='title'>Travel Guides</legend>
    
                <div class="entry">
        <?php $travel = new WP_Query("cat=522".$cat);
    	while ($travel->have_posts()) : $travel->the_post(); ?>
    			<tr>
               <td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>
    		   </tr>
        <?php endwhile; ?>
    	</table>
                </div>
    
            </div>
    </div>
    Thread Starter dcritic

    (@dcritic)

    Please help if possible. Thx.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Multiple categories on a category page’ is closed to new replies.