Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Conditional statement for wordpress custom post category WordPress

I am trying to work out a conditional statement for a custom post type category.
The post type is "pathtv" while "project_type" represent my category that is broken down into "season-1" and "season-2" (category slug)


What I want is that when a post is assign to categories "Season 2 with slug season-2" I want the jquery Tab to show up. If no post is assigned, it should not.

I have tried this without success


<?php if($post->post_type == 'pathtv' && project-type == 'season-2') : ?>
<li><a href="#season2">Season Two</a></li>
<?php endif; ?>


and this


<?php if($post->post_type == 'pathtv' && is_category == 'season-2') : ?>
<li><a href="#season2">Season Two</a></li>
<?php endif; ?>


Here is my complete code for the page:


<ul id="menu2" class="menuTab">
<li class="active"><a href="#season1">Season One</a></li>

<?php if($post->post_type == 'pathtv' && project-type == 'season-2') : ?>
<li><a href="#season2">Season Two</a></li>
<?php endif; ?>






</ul>
<div id="season1" class="contentTab">
<h3>Season One</h3>

<?php
query_posts( array( 'post_type' => 'pathtv', 'project-type' => 'season-1', 'posts_per_page' => '20') );
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>

<li><a href="<?php the_permalink() ?>"><?php the_title(); ?> <?php $site= get_post_custom_values('projLink');
if($site[0] != ""){

?> - Episode <?=$site[0]?>




<?php }else{ ?>

<?php } ?></a></li>

<?php endwhile; endif; wp_reset_query(); ?>

</div>


<div id="season2" class="contentTab">
<h3>Season Two</h3>
<?php
query_posts( array( 'post_type' => 'pathtv', 'project-type' => 'season-2' ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>

<li><a href="<?php the_permalink() ?>"><?php the_title(); ?> <?php $site= get_post_custom_values('projLink');
if($site[0] != ""){

?> - Episode <?=$site[0]?>




<?php }else{ ?>

<?php } ?></a></li>

<?php endwhile; endif; wp_reset_query(); ?>

</div>







<script type="text/javascript">
// <![CDATA[

$(document).ready(function () {
$('#menuTab').tabify();
$('#menu2').tabify();
});

// ]]>
</script>





Answers (5)

2015-02-18

Arnav Joy answers:

are you integrating this code in single.php file?


Arnav Joy comments:

you can use this code

<?php

$terms = get_the_terms( get_the_ID(), 'project_type' );
if( $terms ){
$project_type = array();
foreach ( $terms as $term ) {
$project_type[] = $term->slug;
}

if( @in_array( 'season-2',$project_type) ){
echo 'yes';
}
}


eni360 comments:

Where will i wrap the "<li></li" tag in this?


Arnav Joy comments:

here is the complete code.

<ul id="menu2" class="menuTab">

<li class="active"><a href="#season1">Season One</a></li>

<?php

$terms = get_the_terms( get_the_ID(), 'project_type' );

if( $terms ){

$project_type = array();

foreach ( $terms as $term ) {

$project_type[] = $term->slug;

}



if( @in_array( 'season-2',$project_type ) && $post->post_type == 'pathtv' ){

echo '<li><a href="#season2">Season Two</a></li>';

}

} ?>


eni360 comments:

http://ppevideo.com/pathtv/

not working. Can you check?


Arnav Joy comments:

please try changing this line

if( @in_array( 'season-2',$project_type ) && $post->post_type == 'pathtv' )

to this

if( @in_array( 'season-2',$project_type ) )


eni360 comments:

Tried it. Not working.

Also note there is "project-type" and you constantly use "project_type" in some places.

Thanks

2015-02-18

John Cotton answers:

You need to get hold of the terms for each post - you won't have those in the loop without retrieving separately.

Try something like this:

$terms = wp_get_object_terms( $post->ID, 'project-type', array( 'fields' => 'slugs' ) );

$terms will be an array with zero or more slugs for your project-type taxonomy, so you can just do [[LINK href="http://php.net/manual/en/function.in-array.php"]]in_array[[/LINK]] on it to test for what you want to know.

2015-02-18

Luis Abarca answers:

You should use has_term( $term, $taxonomy, $postID)


<?php if($post->post_type == 'pathtv' && has_term('season-2', 'category', $post->ID)) : ?>

<li><a href="#season2">Season Two</a></li>

<?php endif; ?>


Luis Abarca comments:

Or this way.


<?php if($post->post_type == 'pathtv' && in_category('season-2', $post->ID) ) : ?>
<li><a href="#season2">Season Two</a></li>
<?php endif; ?>


Luis Abarca comments:

You may also want to change the active class.


// functions.php
function is_season_2($post)
{
if ($post->post_type == 'pathtv' && in_category('season-2', $post->ID) ) {
return true;
}

return false;
}




<li <?php if (!is_season_2()) : ?>class="active"<?php endif ?>><a href="#season1">Season One</a></li>

<?php if (is_season_2()) : ?>
<li><a class="active" href="#season2">Season Two</a></li>
<?php endif; ?>


eni360 comments:

Active class wont work but another class that can be attached to the Season 2 Tab.
This is because when you click on season 2, it becomes Active while Season One Tab becomes not active.


Luis Abarca comments:

Try this.

I separated the link to a external file.


// episode-link.php
<li>
<a href="<?php the_permalink() ?>">
<?php the_title() ?>
<?php $site = get_post_custom_values( 'projLink'); ?>
<?php if ( !empty($site[0]) ): ?> - Episode <?= $site[0] ?> <?php endif ?>
</a>
</li>



<ul id="menu2" class="menuTab">
<li class="active"><a href="#season1">Season One</a></li>

<?php if ( $post->post_type == 'pathtv' && has_term('season-2', 'project_type', $post->ID) ) : ?>
<li><a href="#season2">Season Two</a></li>
<?php endif ?>

</ul>

<div id="season1" class="contentTab">
<h3>Season One</h3>

<?php
query_posts( array(
'post_type' => 'pathtv',
'project-type' => 'season-1',
'posts_per_page' => '20'
));
?>

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post() ?>
<?php get_template_part('episode-link') ?>
<?php endwhile ?>
<?php endif; wp_reset_query() ?>
</div>

<div id="season2" class="contentTab">
<h3>Season Two</h3>
<?php
query_posts(array(
'post_type' => 'pathtv',
'project-type' => 'season-2'
));
?>

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post() ?>
<?php get_template_part('episode-link') ?>
<?php endwhile ?>
<?php endif; wp_reset_query() ?>
</div>










<script type="text/javascript">
// <![CDATA[



$(document).ready(function() {

$('#menuTab').tabify();

$('#menu2').tabify();

});

// ]]>
</script>


Luis Abarca comments:

And with the old code, also removed the 2 calls to tabify


<ul id="menu2" class="menuTab">
<li class="active"><a href="#season1">Season One</a></li>

<?php if ( $post->post_type == 'pathtv' && has_term('season-2', 'project_type', $post->ID) ) : ?>
<li><a href="#season2">Season Two</a></li>
<?php endif ?>
</ul>

<div id="season1" class="contentTab">
<h3>Season One</h3>

<?php
query_posts( array(
'post_type' => 'pathtv',
'project-type' => 'season-1',
'posts_per_page' => '20'
));
?>

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post() ?>
<li>
<a href="<?php the_permalink() ?>">
<?php the_title() ?>
<?php $site = get_post_custom_values( 'projLink'); ?>
<?php if ( !empty($site[0]) ): ?> - Episode <?= $site[0] ?> <?php endif ?>
</a>
</li>
<?php endwhile ?>
<?php endif; wp_reset_query() ?>
</div>

<div id="season2" class="contentTab">
<h3>Season Two</h3>
<?php
query_posts(array(
'post_type' => 'pathtv',
'project-type' => 'season-2'
));
?>

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post() ?>
<li>
<a href="<?php the_permalink() ?>">
<?php the_title() ?>
<?php $site = get_post_custom_values( 'projLink'); ?>
<?php if ( !empty($site[0]) ): ?> - Episode <?= $site[0] ?> <?php endif ?>
</a>
</li>
<?php endwhile ?>
<?php endif; wp_reset_query() ?>
</div>

<script type="text/javascript">
// <![CDATA[
(function($)
{
$(document).ready(function()
{
$('#menu2').tabify();
});
})(jQuery);
// ]]>
</script>


eni360 comments:

After implementing the code and I have assigned a post to Season 2. I do not edit anything in the function.php file.

http://ppevideo.com/pathtv/


eni360 comments:

I can add more money to get this done. thanks.


Luis Abarca comments:

Did you tried my code ?

You have a Javascript error too.


eni360 comments:

Oh, I tried your code. It is even the one there right now.


Luis Abarca comments:

if taxonomy is project-type, you should use this one


<ul id="menu2" class="menuTab">
<li class="active"><a href="#season1">Season One</a></li>

<?php if ( $post->post_type == 'pathtv' && has_term('season-2', 'project-type', $post->ID) ) : ?>
<li><a href="#season2">Season Two</a></li>
<?php endif ?>
</ul>

<div id="season1" class="contentTab">
<h3>Season One</h3>

<?php
query_posts( array(
'post_type' => 'pathtv',
'project-type' => 'season-1',
'posts_per_page' => '20'
));
?>

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post() ?>
<li>
<a href="<?php the_permalink() ?>">
<?php the_title() ?>
<?php $site = get_post_custom_values( 'projLink'); ?>
<?php if ( !empty($site[0]) ): ?> - Episode <?= $site[0] ?> <?php endif ?>
</a>
</li>
<?php endwhile ?>
<?php endif; wp_reset_query() ?>
</div>

<div id="season2" class="contentTab">
<h3>Season Two</h3>
<?php
query_posts(array(
'post_type' => 'pathtv',
'project-type' => 'season-2'
));
?>

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post() ?>
<li>
<a href="<?php the_permalink() ?>">
<?php the_title() ?>
<?php $site = get_post_custom_values( 'projLink'); ?>
<?php if ( !empty($site[0]) ): ?> - Episode <?= $site[0] ?> <?php endif ?>
</a>
</li>
<?php endwhile ?>
<?php endif; wp_reset_query() ?>
</div>

<script type="text/javascript">
// <![CDATA[
(function($)
{
$(document).ready(function()
{
$('#menu2').tabify();
});
})(jQuery);
// ]]>
</script>

2015-02-18

Sébastien | French WordpressDesigner answers:

not
if($post->post_type == 'pathtv' && is_category == 'season-2')
but
if($post->post_type == 'pathtv' && is_category('season-2'))


Sébastien | French WordpressDesigner comments:

You had just one mistake in the syntax of your code.
Try my code and tell me if it's ok


eni360 comments:

I have tried this. not working.

2015-02-19

Romel Apuya answers:

Can you show how you registered your post type pathtv?
or is it via plugin??


eni360 comments:

It is not through a plugin. It was done through normal custom post.


Romel Apuya comments:

can you post the code on adding custom post?