taylor_man
taylor_man

Reputation: 39

How to Only Show Certain Posts in Widget?

I am working on a site and I'm having trouble customizing one of the widgets that came with the template. It's a real estate site, and on the main page there is a widget that grabs post's thumbnails and select info. I've broken the widget in two, so one side shows the posts as it normally does and the other side is to show posts that have the field "hot deal" marked as true. See here

How can I make it so only hot deals shows up on the Specials side? Right now, it's showing in both. I'm not too versed in PHP, but I usually manage to get through it.

I think this is the php in question

<?php 
if(has_post_thumbnail()) {
  if ($instance["thumb"]) {
    echo '<figure class="featured-thumbnail thumbnail">';

    if ($instance['thumb_as_link') {
      echo '<a href="' . the_permalink() . '">';
      if ($hotdeal) {
        echo '<div class="hot-deal"></div>';
      }
    }

    if($instance['thumb_w']!=="" || $instance['thumb_h']!==""){
      $thumb_w = $instance['thumb_w'];
      $thumb_h = $instance['thumb_h'];
      echo '<img src="<?php echo $image; ?>" width="<?php echo $thumb_w ?>" height="<?php echo $thumb_h ?>" alt="' . the_title() . '" />';
    } else {
      echo the_post_thumbnail();
    }

    if ($instance['thumb_as_link']) {
      echo '</a>';
    }

    if (is_front_page() ) { 
      if ($area) {
        echo '<div class="area"><?php echo $area; ?></div>';
      }
    }

    echo '</figure>';
  }
}     

Is there an if statement I could write to achieve the results I'm looking for?

Upvotes: 0

Views: 55

Answers (1)

James
James

Reputation: 1769

Following the suggestion of our friend @TimLewis , I tried to solve your problem and I tried also to organize your code to make it more readable.

See:

if(has_post_thumbnail()) {
    if ($instance["thumb"]){
        echo('<figure class="featured-thumbnail thumbnail">');
    }
    if ($instance['thumb_as_link']) {
        echo('<a href="'.the_permalink().'">');
        if ($hotdeal) {
            echo ('class="hot-deal"');
        }
    }
    if($instance['thumb_w']!=="" || $instance['thumb_h']!=="") {
        $thumb_w = $instance['thumb_w'];
        $thumb_h = $instance['thumb_h'];
        echo ('<img src="'. $image." width=".$thumb_w." height=".$thumb_h."     alt=".the_title()."/>");
    } else {
        the_post_thumbnail();
    }
    if ( $instance['thumb_as_link'] ){
        echo ('</a>');
    }
    if ( is_front_page() ) {
        if ($area) {
            echo('<div class="area">'.$area.'</div>');
        }
    }
    echo ('</figure>');
}

If I didn't make any mistake (what is probable since your code was "a mess"), your error is in this part:

 if ($instance['thumb_as_link']) {
    echo('<a href="'.the_permalink().'">');
    if ($hotdeal) {
        echo ('class="hot-deal"');
    }
}

Don't use the div, just assign the class hot-deal to your link and then use the CSS make the magic happen on your page.

I hope this helps

Upvotes: 1

Related Questions