WordPress Post Pages With Custom HTML includes depending on Category

I’ve been trying to get this to work all day. I want add some custom HTML depending on what category the post is in WordPress from a separate directory to keep things clean. I can’t seem to load the html file.

The relevant code is:

        <div class="container">
    <?php while ( have_posts() ) : the_post(); ?>
        <div class="row clearfix">  
            <?php if (function_exists('breadcrumbs')) breadcrumbs(); ?>
            <div class="title clearfix">
                <a href="javascript:window.history.back();" class="back-list"></a>
            <?php global $post; $category = get_the_category($post->ID); setPostViews($post->ID);  ?>
                <h1><?php echo $category[0]->name;?></h1>
            </div>
            <div class="detailBlock clearfix">
                <h1><?php the_title()?></h1>
                <div class="detail-inner">
                <?php $featured_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full', true);
                    if($featured_img) {
                        echo "<div class='detail-img'><img src='$featured_img[0]' alt='".get_the_title()."'></div>";
                        echo $category[0]->ID ;
                    }
                    the_content(); 

                ?>
                <?php 
                /* TEST CODE */
                 global $post; $category = get_the_category($post->ID);
                 if($category[0] == '6'){
                 include( get_template_directory_uri() '/call-to-actions/cta-weight-loss-fem.html');
                 }
                            ?>

                </div>


        </div>
        <?php endwhile;?>
    </div>

Related posts

1 comment

  1. Change:

    if($category[0] == '6'){
    

    to

    if($category[0]->ID == '6'){
    

    and that should work.

    Note: you don’t need the line above to get the category as you already have it from before. A better idea would be to store the value as a variable.

Comments are closed.