简体   繁体   English

如何仅在窗口小部件中显示某些帖子?

[英]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. 我将小部件分为两部分,因此一侧按通常方式显示帖子,而另一侧则显示将“特价”字段标记为true的帖子。 See here 这里

How can I make it so only hot deals shows up on the Specials side? 我怎样才能使它所以只有hot deals显示出来的特价一边? Right now, it's showing in both. 现在,两者都显示出来。 I'm not too versed in PHP, but I usually manage to get through it. 我不太精通PHP,但通常可以通过它来解决。

I think this is the php in question 我认为这是有问题的PHP

<?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? 是否可以编写if语句来实现所需的结果?

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. 按照我们的朋友@TimLewis的建议,我尝试解决您的问题,并尝试组织您的代码以使其更具可读性。

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. 不要使用div,只需将hot-deal类分配给您的链接,然后使用CSS即可在页面上实现神奇效果。

I hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM