【发布时间】:2011-02-03 19:34:26
【问题描述】:
对于我对这样一个关键概念缺乏理解,我深表歉意,但它最终会深入人心!
这是我的 index.php 页面上的代码。我希望每个投资组合“项目”链接到它的相应 single.php 页面,该页面将显示更多信息/图像。
<?php
$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 12));
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$screenshot_url = $custom["screenshot_url"][0];
$website_url = $custom["website_url"][0];
?>
<div class="item">
<a href="<?=$website_url?>"><?php the_post_thumbnail(); ?> </a>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
<p><a href="<?php echo get_permalink(ID); ?>">This is a link</a></p>
<p><a href="<?php the_permalink(); ?>">Read more</a></p>
</div>
<?php endwhile; ?>
这是我的 single.php 页面上的代码。我认为我的循环线是错误的,因为无论我在主页上单击什么项目,它都会拉入同一个条目,但我不知道它们应该是什么。
<?php $loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 1));
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$screenshot_url = $custom["screenshot_url"][0];
$website_url = $custom["website_url"][0];
?>
<div class="left">
<a href="<?=$website_url?>"> </a>
<h3><?php the_title(); ?></h3>
<p><?php the_content(); ?></p>
</div>
<?php endwhile; ?>
对于这个 Wordpress 新手的任何提示将不胜感激!注意:我知道基本的 HTML 和 CSS,但 PHP 对我来说是一个新概念。
【问题讨论】:
-
我看到您正在尝试显示投资组合中的单个项目,对吗?在这种情况下,您不需要在 single.php 中进行循环,因为您没有循环任何内容。如果没有更广泛的背景,我无法理解您正在尝试做什么,例如此代码来自何处以及您如何尝试使其与众不同。如果您查看默认主题,您将看到循环中的单个帖子如何链接到其 single.php 页面。尝试复制这种行为。
-
啊……循环。好的。我会看看那个。非常感谢!
-
是的,第一部分中的循环看起来像这样,但对于 single.php 页面,您不需要它。取出 while 循环,使其只显示:
标签: wordpress