Link to home
Start Free TrialLog in
Avatar of Member_2_7966984
Member_2_7966984

asked on

Wordpress PHP - How to show a div only if it is in a specific category within a custom Taxonomy

Hello - I am trying to show a div only if it exists within a specific category in my custom taxonomy

here is what I have but nothing is showing up so guessing I'm missing something

         <!-- Category back buttons -->
         
               
          <? if ( is_tax( 'project-category', '5' ) ) { ?>
            <!-- Retail -->
            <div>
               <a href="/category-retail/" title="view more retail projects">View More Retail Projects</a>
            </div>   
         <? } ?>
            
            
          <? if ( is_tax ( 'project-category', '20' ) ) { ?>
            <!-- Education -->
            <div>
               <a href="/category-education/" title="view more retail projects">View More Education Projects</a>
            </div>   
         <? } ?>            
            
         <!-- End Category back buttons -->

Open in new window


this is a custom post type with slug "project" that has a custom taxonomy with slug "project-category" on my singles page.

within my custom taxonomy "retail" has a category id of 5 and "education" has a category id of 20 

I want to only display the div and its contents only if the specific category id is associated with the post on the singles page...

Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

this is a custom post type with slug "project" that has a custom taxonomy with slug "project-category" on my singles page within my custom taxonomy "retail" has a category id of 5 and "education" has a category id of 20.I want to only display the div and its contents only if the specific category id is associated with the post on the singles page... 

Try using your custom taxonomy text instead

Example:
<? if ( is_tax('project-category', '5' ) ) { ?>
change to
<? if ( is_tax('project-category', 'retail' ) ) { ?>

Open in new window

<? if ( is_tax ( 'project-category', '20' ) ) { ?> 
change to
<? if ( is_tax ( 'project-category', 'education' ) ) { ?>

Open in new window

Avatar of Member_2_7966984
Member_2_7966984

ASKER

yeah I tried it that way as well but still nothing outputs and I would rather use the tax id incase the category name ever changes

         <!-- Category back buttons -->
         
               
          <?php if ( is_tax( 'project-category', 'retail' ) ) { ?>
            <!-- Retail -->
            <div>
               <a href="/category-retail/" title="view more retail projects">View More Retail Projects</a>
            </div>   
         <?php } ?>
            
            
          <?php if ( is_tax ( 'project-category', 'education' ) ) { ?>
            <!-- Education -->
            <div>
               <a href="/category-education/" title="view more retail projects">View More Education Projects</a>
            </div>   
         <?php } ?>            
            
         <!-- End Category back buttons -->

Open in new window

Also tried doing it without the shorthand <? and changed it to <?php ?> - still no luck

- here is an example of a loop that does work ... I'm thinking I may need to include $post->ID  someplace perhaps?


         <? $image_gallery = get_post_meta($post->ID, "wpcf-additional-images");array_push($image_gallery, get_post_meta($post->ID, "wpcf-primary-image", true));
         
         foreach ($image_gallery as $image) {
         
            if ( count($image_gallery) == 2 ) { $layout = "layout2"; }
            if ( count($image_gallery) == 3 ) { $layout = "layout3"; }
            if ( count($image_gallery) == 4 ) { $layout = "layout3"; }
            if ( count($image_gallery) > 4 ) { $layout = "layout3"; }
            
            ?>
            <div class="portfolio-box <?=$layout;?>">
               <a href="<?=$image;?>"><div class="bck-holder" style="background-image:url(<?=$image;?>)"></div></a>
            </div>
            <?
         
         }
         
         ?>

Open in new window

Any idea on what I might be able to try next?
yeah I tried it that way as well but still nothing outputs and I would rather use the tax id incase the category name ever changes 
Use echo to output the result once the condition meet.

You can check the returned result before use the is_tax() method.

$taxonomy = get_query_var('taxonomy');
$queried_object = get_queried_object();
$term_id = (int)$queried_object->term_id;

Open in new window


here is an example of a loop that does work ... I'm thinking I may need to include $post->ID  someplace perhaps? 
You can use $wp_query or get_queried_object_id().
global $wp_query;    
$page_id = $wp_query->post->ID;

Open in new window

or
get_queried_object_id() 

Open in new window


Thanks I am just trying to output the the <div> if the condition is met - I did not need any help with the loop code I just included that as an example of what DOES work ... Wasn't sure if it would be helpful or not ...

but at any rate... I'm not certain how I would implement your suggestion - this is what the code looks like for now  

         <!-- Category back buttons -->
         
               
          <?php
            $taxonomy = get_query_var('taxonomy');
            $queried_object = get_queried_object();
            $term_id = (int)$queried_object->term_id;            
            
            if ( is_tax( 'project-category', 'retail' ) ) { ?>
            <!-- Retail -->
            
            <div>
               <a href="/category-retail/" title="view more retail projects">View More Retail Projects</a>
            </div>
               
         <?php } ?>
            
            
          <?php
            $taxonomy = get_query_var('taxonomy');
            $queried_object = get_queried_object();
            $term_id = (int)$queried_object->term_id;   
            
            if ( is_tax ( 'project-category', 'education' ) ) { ?>
            <!-- Education -->
            <div>
               <a href="/category-education/" title="view more retail projects">View More Education Projects</a>
            </div>   
         <?php } ?>            
            
         <!-- End Category back buttons -->

Open in new window


Could you please provide an example of how to implement the echo suggestion you gave?

 
Could you please provide an example of how to implement the echo suggestion you gave? 
Check whether the requirement is meet and display the output.
example:
if ( is_tax( 'project-category', 'retail' ) ) { 
  echo "TESTING";
}

Open in new window

Still no luck - (nothing outputs) here is a post within the retail category of my custom taxonomy
https://stagemec.wpengine.com/project/cypress-plaza/ 
Screenshot of post: https://snipboard.io/sSyM8h.jpg

Screenshot of categories: https://snipboard.io/UO01oK.jpg

This is the code in my single-project.php file

<?php
/**
 * Template used for single posts and other post-types
 * that don't have a specific template.
 *
 * @package Avada
 * @subpackage Templates
 */


// Do not allow directly accessing this file.
if ( ! defined( 'ABSPATH' ) ) {
   exit( 'Direct script access denied.' );
}
?>
<?php get_header(); ?>


<section id="content" <?php Avada()->layout->add_style( 'content_style' ); ?>>
   
   
      
   
   
   <?php if ( fusion_get_option( 'blog_pn_nav' ) ) : ?>
      <div class="single-navigation clearfix">
         <?php previous_post_link( '%link', esc_attr__( 'Previous', 'Avada' ) ); ?>
         <?php next_post_link( '%link', esc_attr__( 'Next', 'Avada' ) ); ?>
      </div>
   <?php endif; ?>


   <?php while ( have_posts() ) : ?>
      <?php the_post(); ?>
      <article id="post-<?php the_ID(); ?>" <?php post_class( 'post' ); ?>>
         <?php $full_image = ''; ?>
         <?php if ( 'above' === Avada()->settings->get( 'blog_post_title' ) ) : ?>
            <?php if ( 'below_title' === Avada()->settings->get( 'blog_post_meta_position' ) ) : ?>
               <div class="fusion-post-title-meta-wrap">
            <?php endif; ?>
            <?php $title_size = ( false === avada_is_page_title_bar_enabled( $post->ID ) ? '1' : '2' ); ?>
            <?php echo avada_render_post_title( $post->ID, false, '', $title_size ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
            <?php if ( 'below_title' === Avada()->settings->get( 'blog_post_meta_position' ) ) : ?>
               <?php echo avada_render_post_metadata( 'single' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
               </div>
            <?php endif; ?>
         <?php elseif ( 'disabled' === Avada()->settings->get( 'blog_post_title' ) && Avada()->settings->get( 'disable_date_rich_snippet_pages' ) && Avada()->settings->get( 'disable_rich_snippet_title' ) ) : ?>
            <span class="entry-title" style="display: none;"><?php the_title(); ?></span>
         <?php endif; ?>


         <?php avada_singular_featured_image(); ?>


         <?php if ( 'below' === Avada()->settings->get( 'blog_post_title' ) ) : ?>
            <?php if ( 'below_title' === Avada()->settings->get( 'blog_post_meta_position' ) ) : ?>
               <div class="fusion-post-title-meta-wrap">
            <?php endif; ?>
            <?php $title_size = ( false === avada_is_page_title_bar_enabled( $post->ID ) ? '1' : '2' ); ?>
            <?php echo avada_render_post_title( $post->ID, false, '', $title_size ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
            <?php if ( 'below_title' === Avada()->settings->get( 'blog_post_meta_position' ) ) : ?>
               <?php echo avada_render_post_metadata( 'single' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
               </div>
            <?php endif; ?>
         <?php endif; ?>
         <div class="post-content">
            <?php the_content(); ?>
            <h4>Testing normal content</h4>
         <!-- Category back buttons -->
               
          <?php
            $taxonomy = get_query_var('taxonomy');
            $queried_object = get_queried_object();
            $term_id = (int)$queried_object->term_id;            
            
            if ( is_tax( 'project-category', 'retail' ) ) {
            echo "TESTING";
            }
               
          ?>
            
            
          <?php
            $taxonomy = get_query_var('taxonomy');
            $queried_object = get_queried_object();
            $term_id = (int)$queried_object->term_id;   
            
            if ( is_tax ( 'project-category', 'education' ) ) { ?>
            <!-- Education -->
            <div>
               <a href="/category-education/" title="view more retail projects">View More Education Projects</a>
            </div>   
         <?php } ?>            
            
         <!-- End Category back buttons -->            
            
            
            
            
                  <?php
         $theTerms = wp_get_post_terms( $post->ID, 'project-category', array("fields" => "all"));
         $termLink = get_term_link($theTerms[0]->term_id, 'project-category');
         ?>
         
         
         <? $image_gallery = get_post_meta($post->ID, "wpcf-additional-images");array_push($image_gallery, get_post_meta($post->ID, "wpcf-primary-image", true));
         
         foreach ($image_gallery as $image) {
         
            if ( count($image_gallery) == 2 ) { $layout = "layout2"; }
            if ( count($image_gallery) == 3 ) { $layout = "layout3"; }
            if ( count($image_gallery) == 4 ) { $layout = "layout3"; }
            if ( count($image_gallery) > 4 ) { $layout = "layout3"; }
            
            ?>
            <div class="portfolio-box <?=$layout;?>">
               <a href="<?=$image;?>"><div class="bck-holder" style="background-image:url(<?=$image;?>)"></div></a>
            </div>
            <?
         
         }
         
         ?>          
         
         
         


         <?php if ( ! post_password_required( $post->ID ) ) : ?>
            <?php if ( '' === Avada()->settings->get( 'blog_post_meta_position' ) || 'below_article' === Avada()->settings->get( 'blog_post_meta_position' ) || 'disabled' === Avada()->settings->get( 'blog_post_title' ) ) : ?>
               <?php echo avada_render_post_metadata( 'single' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
            <?php endif; ?>
            <?php do_action( 'avada_before_additional_post_content' ); ?>
            <?php avada_render_social_sharing(); ?>
            <?php $author_info = fusion_get_page_option( 'author_info', $post->ID ); ?>
            <?php if ( ( Avada()->settings->get( 'author_info' ) && 'no' !== $author_info ) || ( ! Avada()->settings->get( 'author_info' ) && 'yes' === $author_info ) ) : ?>
               <section class="about-author">
                  <?php ob_start(); ?>
                  <?php the_author_posts_link(); ?>
                  <?php /* translators: The link. */ ?>
                  <?php $title = sprintf( __( 'About the Author: %s', 'Avada' ), ob_get_clean() ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride ?>
                  <?php $title_size = ( false === avada_is_page_title_bar_enabled( $post->ID ) ? '2' : '3' ); ?>
                  <?php Avada()->template->title_template( $title, $title_size ); ?>
                  <div class="about-author-container">
                     <div class="avatar">
                        <?php echo get_avatar( get_the_author_meta( 'email' ), '72' ); ?>
                     </div>
                     <div class="description">
                        <?php the_author_meta( 'description' ); ?>
                     </div>
                  </div>
               </section>
            <?php endif; ?>
            <?php avada_render_related_posts( get_post_type() ); // Render Related Posts. ?>


            <?php $post_comments = fusion_get_page_option( 'blog_comments', $post->ID ); ?>
            <?php if ( ( Avada()->settings->get( 'blog_comments' ) && 'no' !== $post_comments ) || ( ! Avada()->settings->get( 'blog_comments' ) && 'yes' === $post_comments ) ) : ?>
               <?php comments_template(); ?>
            <?php endif; ?>
            <?php do_action( 'avada_after_additional_post_content' ); ?>
         <?php endif; ?>
      </article>
   <?php endwhile; ?>
</section>
<?php do_action( 'avada_after_content' ); ?>
<?php get_footer(); ?>



Open in new window

Noted. Please screenshots what you get from these declared variables below?

$taxonomy = get_query_var('taxonomy');
echo "taxonomy :".$taxonomy

Open in new window

$queried_object = get_queried_object();
var_dump( get_queried_object() );

Open in new window

$term_id = (int)$queried_object->term_id;   
echo "term_id :".$term_id

Open in new window

IDK if I am doing this correctly - Could you please provide the code you wish me to add including the opening and closing php tags?

guessing this is what you wanted me to try? ...

          <?php
            $taxonomy = get_query_var('taxonomy');
            echo "taxonomy :".$taxonomy   ?>

Open in new window

 here is a screen shot of what the above code looks like
https://snipboard.io/6HBDlg.jpg

On this code
          <?php
   $queried_object = get_queried_object();
var_dump( get_queried_object() );
            ?>

Open in new window

This is what this looks like
https://snipboard.io/6KW3MG.jpg

on this code
          <?php
$term_id = (int)$queried_object->term_id;  
echo "term_id :".$term_id
            ?>

Open in new window

this is what that looks like https://snipboard.io/lsHEad.jpg



this is a custom post type with slug "project" that has a custom taxonomy with slug "project-category" on my singles page within my custom taxonomy "retail" has a category id of 5 and "education" has a category id of 20. 
Noted. I didn't see the 5 or 20 for the taxonomy category based on the screened above. The current term_id is an empty value return.
 
Can you get me the screenshot for these variables as well? 
<?php
$term_id = get_queried_object_id(); 
echo "term_id :".$term_id
?>

Open in new window

<?php
$tax = $wp_query->get_queried_object();   
echo ''. $tax->name . '';   
echo "<br>";   
echo ''. $tax->description .''; 
?>

Open in new window

<?php
$terms = wp_get_object_terms( get_queried_object_id(), 'project-category');
$term_id = $terms[0]->term_id;
echo "term_id :".$term_id;
?>

Open in new window

<?php
$terms = get_the_terms( $post->ID, 'project-category');
foreach ( $terms as $term ) 
{    
  $termID[] = $term->term_id; 
}
echo "term_id :".$termID[0];
?>

Open in new window

<?php
global $post;
$terms = wp_get_post_terms( $post->ID, 'project-category',array('fields' => 'ids') ); 
print_r($terms);
?>

Open in new window


Retail singles page with this code
<?php $term_id = get_queried_object_id(); echo "term_id :".$term_id ?>

Open in new window

https://snipboard.io/HQlwpG.jpg

Retail Singles page with this code (nothing outputs)

<?php $tax = $wp_query->get_queried_object();   echo ''. $tax->name . '';   echo "<br>";   echo ''. $tax->description .''; ?>

Open in new window

https://snipboard.io/EkR4ju.jpg

Retail Singles page with this code  
<?php
$terms = wp_get_object_terms( get_queried_object_id(), 'project-category');
$term_id = $terms[0]->term_id;
echo "term_id :".$term_id;
?>

Open in new window

https://snipboard.io/flPs1S.jpg 

Retail Singles page with this code

<?php $terms = get_the_terms( $post->ID, 'project-category'); foreach ( $terms as $term ) {      $termID[] = $term->term_id; } echo "term_id :".$termID[0]; ?>

Open in new window

https://snipboard.io/SPfDuw.jpg 

 Retail singles page with this code:
<?php global $post; $terms = wp_get_post_terms( $post->ID, 'project-category',array('fields' => 'ids') ); print_r($terms); ?>

Open in new window

https://snipboard.io/r9UC6j.jpg 
 
 
ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THANK YOU!   - That worked!
You are welcome
I had like 7 categories - So I removed the Else statement and adjusted the cat ID for each one -
example:
         <!-- Hospitality -->
         <?php wp_reset_query(); ?>   
         <?php
         $terms = get_the_terms( $post->ID, 'project-category');


         foreach ( $terms as $term )
         {
           $term_id = (int)$term->term_id;


           if($term_id==29){
            echo " <div class='back-button-wrap'>
                     <a href='/category-hospitality/' title='view more hospitality projects'><button class='fusion-button button-flat fusion-button-default-size button-default button-1 fusion-button-default-span fusion-button-default-type'>View More Hospitality Projects</button></a>
                  </div>";
           }
         }
         ?>    

Open in new window