【发布时间】:2016-03-04 20:16:29
【问题描述】:
我在使用 wordpress 时遇到问题。我正在在线编辑 wordpress 页面并在其中使用数组,所以当我在我的页面上写这个时,
<section id="recent">
<h1>Recently Added</h1>
<ul class="row-fluid">
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="span3">
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li><!-- /span3 -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!-- /row-fluid -->
</section><!-- /recent -->
结果,页面显示的是我想要的,
‘产品’, ‘库存’ => 1, ‘posts_per_page’ => 4, ‘orderby’ =>'date','order' =>'DESC'); $loop = new WP_Query($args); while ( $loop->have_posts() ) : $loop->the_post();全球$产品; ?> '; ?> get_price_html(); ?>
post, $product ); ?>
请帮助我了解发生了什么,以及如何解决它。 至少我知道在这部分代码中,
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'orderby' =>'date','order' => 'DESC' );
第一个'>'符号,关闭php标签
【问题讨论】:
-
除非你有错别字而且是
?>。大于符号不是有效的 php 关闭标记。它所在的文件的名称是什么? -
数组定义中的
=>是文件中第一个关闭的>,这表明它没有被发送到PHP 解释器。如果您在浏览器中查看页面源代码,您将在那里看到 所有 PHP 代码。您是通过网络服务器查看此文件,通过http://localhost,还是尝试在浏览器中打开文件,如file:///path/to/filename? -
我在在线主机上编辑它,所以可能接近localhost,使用wordpress编辑器编辑它
-
使用wordpress编辑器编辑是什么意思?您想说您将
php代码放在wordpress 后端的页面中吗?因为无论您尝试什么,这都行不通。您的代码看起来不错,但应该在.php文件中,而不是在 wordpress 页面中。也不要使用wp_reset_query(),而是使用wp_reset_postdata()。