【问题标题】:PHP not showing one variable on WordPress pagePHP没有在WordPress页面上显示一个变量
【发布时间】:2019-10-20 23:41:28
【问题描述】:

我正在尝试在图像叠加层中添加文本。该页面显示正确帖子的所有其他变量。但是状态变量仅显示所有图像上的第一个文本,例如新到货

变量

$status = 'NEW'; $status2='SOLD'; $status3='HOT DEAL';

显示代码

<div class="image-overlay"><?php $status = get_field('status'); if ($status='NEW') echo 'NEW ARRIVAL'; else { the_field('status');} ?></div>

我尝试过其他代码,例如,

echo ('$status')

<?php
unset($bed); unset($type); unset($salerent); unset($pricemin); unset($pricemax); unset($status);
$salerent = 'sale';
$salerent2 = 'rent';
$type = 'villa'; $type2='condo'; $type3='house'; $type4='land';
$status = 'NEW'; $status2='SOLD'; $status3='HOT DEAL';
$bed = 'all';
$minprice='0';
$maxprice='999999999';
if ($bed=='all') {$bed1=1;$bed2=2;$bed3=3;$bed4=4;$bed5=5;$bed6=6;$bed7=7;$bed8=8;$bed9=9;$bed10=10;}
if ($salerent=='rent'){
$p0text="Price (per month)";
$p1text="0 - 10,000";
$p2text="10,000 - 25,000";
$p3text="25,000 - 40,000";
$p4text="40,000 - 80,000";
$p5text="80,000+";  
}
else{
$p0text="Price";
$p1text="0 - 3 mill";
$p2text="3 - 6 mill";
$p3text="6 - 10 mill";
$p4text="10 - 20 mill";
$p5text="20+ mill"; 
}
?>
<?php wp_head(); ?>
</head>
<body>
<?php include( get_template_directory() . '/menu.php'); ?>  
    <!-- Main-->
    <main>
    <!-- Main Container-->
    <div class='container '>


<div class="row" style="vertical-align:bottom;">
    <div class="col-lg-12 col-md-12 text-center"><h1 class="hometitle"><span style="font-size:1.1em; color:red; position:absolute; margin-left:-4px; margin-top:-19px;  transform: rotate(25deg);">/</span>star property<br /><span class="mainsub"> Villas, Condos &amp; Houses for Sale &amp; Rent in Hua Hin</span></h1></div>
    </div>

    <div class="row">
    <div class="col-lg-12 col-md-12 center-on-small-only text-center">
    <div class="row searchbox">
    <div class="col-lg-12">
    <form id="searchurl" method="POST" action="http:// .com/search-result/?">
    <span class="filter">Search:</span>
    <select class="custom-select mysearch" id="salerent" name="salerent">
    <option value="sale" <?php if($salerent=='sale'){echo "selected";}?>>Sale</option>
    <option value="rent" <?php if($salerent=='rent'){echo "selected";}?>>Rent</option>
    </select>

    <select class="custom-select mysearch" id="type" name="type">
    <option value="villa" <?php if ($type=='villa'){echo "selected";}?>>Villa</option>
    <option value="condo" <?php if ($type=='condo'){echo "selected";}?>>Condo</option>
    <option value="house"<?php if ($type=='house'){echo "selected";}?>>House</option>
    <option value="house"<?php if ($type=='land'){echo "selected";}?>>Land</option>
    <option value="house"<?php if ($type=='commercial'){echo "selected";}?>>Commercial</option>
    </select>

    <select class="custom-select mysearch" id="bed" name="bed">
    <option value="all">Bedrooms</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    </select>

    <select class="custom-select mysearch" id="pricerange" name="pricerange">
    <option id="p0" value="all"><?php echo $p0text;?></option>
    <option id="p1" value="1"><?php echo $p1text;?></option>
    <option id="p2" value="2"><?php echo $p2text;?></option>
    <option id="p3" value="3"><?php echo $p3text;?></option>
    <option id="p4" value="4"><?php echo $p4text;?></option>
    <option id="p5" value="5"><?php echo $p5text;?></option>
    </select>
<input class="col-lg-1 col-md-1 mysearch" type="text" id="searchid" name="searchid" placeholder="#ID/Project" />
<button type="submit" onclick="document.getElementById('searchurl').submit();" id="submitbutton" class="btn btn-primary btn-md">SEARCH</button>
    </form>
    </div>
    </div>
    </div>  
    </div>



<div class="row zero col-lg-12 col-md-12 text-center">
<h2 style="width:100%; margin:0.8em 0 0.8em 0;">New Listings:<br /><span class="sub"> Properties for Sale &amp; Rent in Hua Hin</span></h2>
        </div>  
<div class="row" style="padding:0; margin:0;">

<?php
$args = array(
    'posts_per_page'    => 6,
    'post__in' => array(22094, 16782, 20898, 19838, 22150, 1442, 1794),
    'post_type'     => 'post',
    'no_paging' => true,
    'order' => DESC,
    'cat'           => 2,
);
// query
$the_query = new Wp_Query( $args ); ?>

 <ul id="starlistings" class="col-lg-12 col-md-12 row" >
<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li class="col-lg-6 col-md-6" style="float:left; ">
<div class="col-lg-12 col-md-12 listingframe" >
<div class="row" style="background-color:#77c0ff" "padding-top: 20px; padding-bottom: 20px">
<div class="text-center col-lg-12 col-md-12"><a class="listingslink" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
</div>
<div class="row" style="padding-top:0.8em;">
<div class="text-center col-lg-12 col-md-12" class="zero"><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image_1'); ?>" class="w-100" alt="<?php the_field('image_1_alt'); ?>" title="<?php the_field('area_project'); ?> <?php $type = get_field('type'); if ($type=='Villa') echo 'House'; else { the_field('type');} ?> for <?php the_field('sale_or_rent'); ?> in Hua Hin">**<div class="image-overlay"><?php $status = get_field('status'); if ($status='NEW') echo 'NEW ARRIVAL'; else { the_field('status');} ?></div>**</a>

</div>

</div>
</div>
<div class="text-center listingsdivider col-lg-12 col-md-12">
    <span>
<span class="listingsicons" style="padding:5px 0.5em 5px 0;">ID #<?php the_field('property_id'); ?></span>
    <span class="listingsicons" style="padding:5px 0.5em 5px 0;"><?php $price = get_field('Price'); echo number_format($price, 0, '', ','); ?> baht<?php $saleorrent = get_field('sale_or_rent'); if($saleorrent=='Rent'){echo ' ';} ?></span>
<span class="listingsicons"  style="padding:5px 0.5em 5px 0.5em;"><img src="/wp-content/themes/startheme/images/house.png" alt="house" style="height:12px; width:auto; margin:-4px 8px 0 0; padding:0; opacity:0.7" /><?php the_field('living_space'); ?> SQM</span>
<span class="listingsicons" style="padding:5px 0.5em 5px 0.5em;"><img src="/wp-content/themes/startheme/images/beds.png" alt="bedrooms" style="height:12px; width:auto; margin:-2px 4px 0 -3px; padding:0; opacity:0.6" /><?php the_field('bedrooms'); ?> Beds</span>
    <span style="padding:5px 0 5px 0.5em; "><img src="/wp-content/themes/startheme/images/bathrooms.png" alt="bathrooms" style="height:15px; width:auto; margin:-2px 6px 0 2px; padding:0; opacity:0.6;" /><?php the_field('bathrooms'); ?> Baths</span>
    </span>

<div style="background-color: white; padding-top: 10px; padding-bottom: 5px"><span><?php $excerpt = wp_trim_words( get_field('property_description' ), $num_words = 10, $more = '<a href="'.get_permalink().'">... more</a>' ); echo $excerpt; ?></span></div>
</div>
     </li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>

【问题讨论】:

  • 我强烈建议您使用数组而不是按名称松散分组的单个变量... I.E 而不是 $p0text 和他所有的朋友都使用 $ptext[0], $ptext[1]等等,所以你所有的 $ptext 都在内部分组。您需要发现阵列才能让您的生活更快乐。尤其是你的 $bed1 和 $bed2 等应该是一个 $bed 数组。

标签: php mysql echo


【解决方案1】:

您的部分代码是...

 if ($status='NEW') echo 'NEW ARRIVAL';

现在您说它始终显示“NEW ARRIVAL”,这意味着您的 IF 始终为 TRUE。

你正在做的是分配 $status = "NEW" 这将永远是真的。

Single = 是赋值

Double == 正在检查等价性。

所以你的陈述应该是……

 if ($status =='NEW') echo 'NEW ARRIVAL';

【讨论】:

  • 嗨,谢谢,是的,它总是显示 NEW ARRIVAL,但是双 == 图像叠加中没有显示任何内容。这让我发疯了......
  • 再次查看您的代码,我会问什么是 var_dump(the_field('status')); 的结果它看起来像空白、空或从未设置,因此请检查您设置它的位置并检查您想要使用它的值。
  • 这更像是一个仅供参考......你为什么不把它弄乱一点。如果你看到一些空的东西,你可以简单地添加一些文本来测试它。比如“哦,它确实到了”或类似的东西,所以你知道 100% 的逻辑是有效的。
  • 我已经像这样设置了状态变量: unset($bed);未设置($类型);未设置($salent);未设置($pricemin);未设置($pricemax);未设置($状态); $salerent = '销售'; $salent2 = '租金'; $type = '别墅'; $type2='公寓'; $type3='房子'; $type4='土地'; $status = '新';但是 var_dump 仍然显示 NULL
  • 您需要关闭它并创建一个关于为什么使用 get_field('status') 从数据库查询中检索字段状态返回 NULL 的新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 2013-03-24
相关资源
最近更新 更多