WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display text based on category of custom post (20 posts)

  1. Daniel
    Member
    Posted 1 year ago #

    Hello,

    I have a theme based restaurant menu and I'd like to add some additional information after the post for that specific category.

    For example...

    <?php if (is_category('white')) : ?>
    <p>White wine is yummy!</p>
    <?php endif; ?>

    The problem is, it doesn't work.

    If I do:

    <?php if (is_archive('white')) : ?>
    <p>White Wine is yummy!</p>
    <?php endif; ?>

    It works, but it displays on all archive pages.

    I'm guessing I need to specify the category of the custom post type which would be "wines" but I'm just not sure how to implement it.

    I have very little knowledge of php, so any help would be appreciated.

    Thank you!

  2. Andrew Bartel
    Member
    Posted 1 year ago #

    You're close! Use in_category()

    <?php if (in_category('white')) : ?>
    <p>White Wine is yummy!</p>
    <?php endif; ?>
  3. Daniel
    Member
    Posted 1 year ago #

    Hey Andrew!!!

    Thanks so much for taking the time to help a brother out! The feeling of community here gives me the "warm and fuzzies!" ;)

    Unfortunately, I tried it and it didn't work... I was told by another source (who's limited to the support he can give) that I was close, but it's a custom post type, therefore I needed to specify the category of the custom post type.

    I'm just not sure how to go about doing this!

    Thanks again!

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    inside the loop you can use this:

    <?php if (in_category('white') && ( $post->post_type == 'wines' ) ) : ?>
    <p>White Wine is yummy!</p>
    <?php endif; ?>

    Where do you want to use the conditional? On a category archive template or somewhere else? Outside or inside the loop?

  5. Daniel
    Member
    Posted 1 year ago #

    Hey keeslemeijer! I think it's getting there, thank you very much for your help!!! :)

    I believe it's in an archive template... Let me show you just to be clear! :)

    File Name: taxonomy-menu_type.php

    <?php get_header(); ?>
    
    <div id="container-sidebar"><!-- sidebar content container -->
    
    	<h2 class="header-underline"><?php _e(single_cat_title()); ?></h2>
    	<div class="menu-description"><?php _e(category_description()); ?></div>
    
    <?php
    	$menuloop = new WP_Query(array(
    		'posts_per_page' => -1,
    		'meta_key' => 'menuoption_menu_pricing',
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		'post_type' => 'menu',
    		'tax_query' => array(
    			array(
    			'taxonomy' => 'menu_type',
    			'field' => 'slug',
    			'terms' => wp_get_post_terms($post->ID, 'menu_type', array("fields" => "slugs"))
    			)
    			)
    	));
    ?>
    
    	<?php if ( have_posts() ) : while ( $menuloop->have_posts() ) : $menuloop->the_post(); ?>
    
    <ul class="menu-items">
      <li>
        <div class="grid2column">
          <?php the_title(); ?>
        </div>
        <div class="grid2column lastcolumn">
          <?php if(get_post_meta($post->ID, 'menuoption_menu_pricing', true)): ?>
          <?php echo get_post_meta($post->ID, 'menuoption_menu_pricing', true) ?>
          <?php endif; ?>
        </div>
        <div class="clearfix"></div>
        <div class="item-description-menu"><?php echo get_the_excerpt(); ?></div>
      </li>
    </ul>
    
    <?php endwhile; ?>
    <?php endif; ?>
    
    *************************** PART I NEED HELP WITH!!!! ***************************
    
    <?php if (in_category('kids-menu') && ( $post->post_type == 'menu' ) ) : ?>
    <p>A portion of all meals will go to the Montreal Children’s Hospital Foundation.</p>
    <?php endif; ?>
    
    **********************************************************************************
    
    <div class="clearfix"></div>
    </div><!-- close #container-sidebar -->
    <?php get_sidebar( 'menu' ); ?>
    <?php get_footer(); ?>
  6. keesiemeijer
    moderator
    Posted 1 year ago #

    I think I don't understand it completely. 'kids-menu' is a custom taxonomy (I think this is not the category taxonomy). On the template file for that taxonomy you query for the post type 'menu'. My question is, why would you need a conditional?

    Are you sure the "taxonomy-menu_type.php" template file is only used for the "menu_type" taxonomy?

    Also, you are using $post->ID outside of the loop:

    'terms' => wp_get_post_terms($post->ID, 'menu_type', array("fields" => "slugs"))

    Maybe query like this:

    $args = array(
    		'posts_per_page' => -1,
    		'meta_key' => 'menuoption_menu_pricing',
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		'post_type' => 'menu',
    	)
    $all_args = array_merge( $wp_query->query_vars, $args );
    $menuloop = new WP_Query( $all_args );
  7. Daniel
    Member
    Posted 1 year ago #

    I'm trying to customize an existing theme through reference, copy and paste, etc... I'm really not a php guy (though wish I was!) but I'm trying to learn! :)

    I tried your query but it broke due to this line:
    $all_args = array_merge( $wp_query->query_vars, $args );

    I think "menu_type" is used in another template file for "Wines" which is likely why it's queried the way it is.

    I can get it work by using this:
    if (is_archive('kids-menu'))

    But it shows on all the menu pages!

    Does that help?

    The way it shows in the URL is:
    http://www.restaurant.com/menu_type/kids-menu/

    I believe menu_type in the custom taxonomy and kids-menu is the category?

    Thanks again, and please forgive my ignorance!

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    Sorry, I've made a typo.
    Change this:

    $args = array(
    		'posts_per_page' => -1,
    		'meta_key' => 'menuoption_menu_pricing',
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		'post_type' => 'menu',
    	)
    $all_args = array_merge( $wp_query->query_vars, $args );
    $menuloop = new WP_Query( $all_args );

    to this:

    $args = array(
    		'posts_per_page' => -1,
    		'meta_key' => 'menuoption_menu_pricing',
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		'post_type' => 'menu',
    	);
    $all_args = array_merge( $wp_query->query_vars, $args );
    $menuloop = new WP_Query( $all_args );

    is_archive() does not accept any parameters ('kids-menu'), it will return true on all archive pages.

    What happens when you correct the typo and change the conditional:

    <?php if (in_category('kids-menu') && ( $post->post_type == 'menu' ) ) : ?>
    <p>A portion of all meals will go to the Montreal Children’s Hospital Foundation.</p>
    <?php endif; ?>

    to:

    <p>A portion of all meals will go to the Montreal Children’s Hospital Foundation.</p>
  9. Daniel
    Member
    Posted 1 year ago #

    Gosh! No need for apologies.. LOL! I really appreciate your efforts!!!

    Unfortunately, I get an error:
    Notice: Undefined variable: menuloop on line 28

    Which is:
    <?php if ( have_posts() ) : while ( $menuloop->have_posts() ) : $menuloop->the_post(); ?>

    Would it help if I PM'ed you a URL to the site?

  10. keesiemeijer
    moderator
    Posted 1 year ago #

    Would it help if I PM'ed you a URL to the site?

    Not really.

    Can you post what you have now in that template file.

  11. Daniel
    Member
    Posted 1 year ago #

    I tried it again removing the conditional tag I was trying to implement... Here it is:

    <?php get_header(); ?>
    
    <div id="container-sidebar"><!-- sidebar content container -->
    
    	<h2 class="header-underline"><?php _e(single_cat_title()); ?></h2>
    	<div class="menu-description"><?php _e(category_description()); ?></div>
    
    <?php
    	$args = array(
    		'posts_per_page' => -1,
    		'meta_key' => 'menuoption_menu_pricing',
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		'post_type' => 'menu',
    	);
    $all_args = array_merge( $wp_query->query_vars, $args );
    $menuloop = new WP_Query( $all_args );
    ?>
    
    	<?php if ( have_posts() ) : while ( $menuloop->have_posts() ) : $menuloop->the_post(); ?>
    
    <ul class="menu-items">
      <li>
        <div class="grid2column">
          <?php the_title(); ?>
        </div>
        <div class="grid2column lastcolumn">
          <?php if(get_post_meta($post->ID, 'menuoption_menu_pricing', true)): ?>
          <?php echo get_post_meta($post->ID, 'menuoption_menu_pricing', true) ?>
          <?php endif; ?>
        </div>
        <div class="clearfix"></div>
        <div class="item-description-menu"><?php echo get_the_excerpt(); ?></div>
      </li>
    </ul>
    
    <?php endwhile; ?>
    <?php endif; ?>
    
    <!-- *** NEED HELP HERE!!! *** -->
    
    <p>A portion of all meals will go to the Montreal Children's Hospital Foundation.</p>
    
    <!-- ************************* -->
    
    <div class="clearfix"></div>
    </div><!-- close #container-sidebar -->
    <?php get_sidebar( 'menu' ); ?>
    <?php get_footer(); ?>

    And this is what is spits out:

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1' at line 1]
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (33) AND tt1.term_taxonomy_id IN (33) ) AND wp_posts.post_type = 'menu' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND (wp_postmeta.meta_key = 'menuoption_menu_pricing' ) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 ASC LIMIT 0, -1
    A portion of all meals will go to the Montreal Children's Hospital Foundation.

    I really feel that I need a conditional tag as there are other items:

    • Starters
    • Salads
    • Traditional Style Chicken
    • Portuguese Style Chicken
    • Mains
    • Sandwiches
    • Sides
    • Kid’s Menu
    • Desserts
    • Wines

    I really don't want to waste too much of your time as I thought the solution would be a more obvious one... But I think there's a lot more pieces in play which complicates it a little more. I may just approach the developer of the theme and contract out his services.

    What do you think? :)

  12. keesiemeijer
    moderator
    Posted 1 year ago #

    This is getting stranger and stranger. I really don't know why you have an error in your SQL syntax as the template doesn't do any direct sql queryies.

    I may just approach the developer of the theme and contract out his services.

    Maybe this would be best.

  13. keesiemeijer
    moderator
    Posted 1 year ago #

    One more thing you could try is changing this:

    $all_args = array_merge( $wp_query->query_vars, $args );

    to this:

    global $wp_query;
    $all_args = array_merge( $wp_query->query, $args );
  14. Daniel
    Member
    Posted 1 year ago #

    Hey keesiemeijer,

    WTG!!! Your query now works!!!! And I must say, it looks a hell of a lot cleaner than the mess I has going before!!!!

    My original problem remains however in regards to this part:

    <?php if (in_category('kids-menu') && ( $post->post_type == 'menu' ) ) : ?>
    <p>A portion of all meals will go to the Montreal Children's Hospital Foundation.</p>
    <?php endif; ?>

    By removing the conditional tags, the text appears on all the menu categories!

  15. keesiemeijer
    moderator
    Posted 1 year ago #

    Maybe using the term description to set the extra text is a better idea than have multiple conditionals for all the terms:
    http://codex.wordpress.org/Function_Reference/term_description
    You can set the description when you edit a menu_type term (category).

    After that you can replace the conditional with this:

    <?php if (term_description() ) : ?>
    <?php echo term_description(); ?>
    <?php endif; ?>
  16. Daniel
    Member
    Posted 1 year ago #

    Good idea, it's just that I've already utilized the description for just that...

    I was going to include this information to the bottom of the post like so...

    Menu Item Title
    ————————
    Description of menu item set in the category description.

    • Item 1
    • Item 2
    • Item 3

    [LOGO] A portion of all meals will go to the Montreal Children's Hospital Foundation.

    I guess I may have to reconsider and put it at the top! ;)

    Thanks so much keesiemeijer for all your help! :)

  17. keesiemeijer
    moderator
    Posted 1 year ago #

    Ah, try it with is_tax():
    http://codex.wordpress.org/Function_Reference/is_tax

    <?php if(is_tax( 'menu_type', 'kids-menu' ) ) : ?>
    <p>A portion of all meals will go to the Montreal Children's Hospital Foundation.</p>
    <?php endif; ?>
  18. Daniel
    Member
    Posted 1 year ago #

    HA HA!!!!! keesiemeijer, you freekin' GENIUS!!! It worked!!!!

    I REALLY appreciate your persistence!!!! If you ever find yourself in Cornwall, the first 10 beers are on me!!! ;)

    Thank you! Thank you! Thank you!!!!!!! :D

  19. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome :-) I also found another solution by adding an extra description field to the taxonomy "menu_type" edit page.
    If you want you can put this in your theme's functions.php: http://pastebin.com/gbucRFL7

    Add the descriptions to the terms and use a conditional as this to show the extra description:

    <?php if (get_term_extra_description()) : ?>
    <p><?php echo get_term_extra_description(); ?></p>
    <?php endif; ?>

    I'm glad you've got it resolved.

  20. Daniel
    Member
    Posted 1 year ago #

    Heck! You just don't stop giving, do you?! :P

    I'm glad you shared this as an alternate solution! This would be a great option if the client is the one doing the editing allowing them to easily include additional category information in a separate area!

    Great share! Thanks again keesiemeijer! :)

Topic Closed

This topic has been closed to new replies.

About this Topic