【发布时间】:2014-02-13 23:59:33
【问题描述】:
考虑:
<?php
/*
Portfolio 2 Columns
*/
?>
<?php
get_header();
?>
<div id="primary">
<div id="content" role="main">
<?php
$terms = get_terms("tagportfolio");
$count = count($terms);
echo '<ul id="portfolio-filter">';
echo '<li><a href="#all" title="">All</a></li>';
if ( $count > 0 ){
foreach ( $terms as $term ) {
$termname = strtolower($term->name);
$termname = str_replace(' ', '-', $termname);
echo '<li><a href="#' . $termname .
'" title="" rel="' . $termname .
'">' . $term->name .
'</a></li>';
}
}
echo "</ul>";
?>
<?php
$loop = new WP_Query(array('post_type' => 'project', 'posts_per_page' => -1));
$count =0;
?>
<div id="portfolio-wrapper">
<ul id="portfolio-list">
<?php
if ( $loop ) :
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php
$post = '';
$terms = get_the_terms($post->ID, 'tagportfolio' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term )
{
$links[] = $term->name;
}
$links = str_replace(' ', '-', $links);
$tax = join( " ", $links );
else :
$tax = '';
endif;
?>
<?php
$infos = get_post_custom_values('_url');
?>
<li class="portfolio-item <?php echo strtolower($tax); ?> all">
<div class="thumb"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail('project-large'); ?></a></div>
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p class="excerpt"><a href="<?php the_permalink() ?>"><?php echo get_the_excerpt(); ?></a></p>
<p class="links"><a href="<?php echo $infos[0]; ?>" target="_blank">Live Preview →</a> <a href="<?php the_permalink() ?>">More Details →</a></p>
</li>
<?php
endwhile; else:
?>
<li class="error-not-found">Sorry, no portfolio entries for while.</li>
<?php
endif;
?>
</ul>
<div class="clearboth"></div>
</div> <!-- end #portfolio-wrapper-->
</div><!-- #content -->
</div><!-- #primary -->
当上面的 WordPress 代码调试为真时,我会收到这些通知:
注意:未定义的变量:第 50 行的 C:..php 中的帖子
注意:第 50 行试图在 C:....php 中获取非对象的属性
第 50 行是:$terms = get_the_terms($post->ID, 'tagportfolio');
如何解决此问题并消除通知?
【问题讨论】:
-
您在代码中的哪个位置初始化了 $post 并且它是否具有
ID属性? -
删除 if($terms && !is_wp_error($terms)) 中的空格以读取 !is_wp_error
-
我已经编辑了代码并添加了这个:$post = '';初始化 $post 现在没有第一个通知,但第二个仍然存在:注意:尝试在 C:.. 中获取非对象的属性第 50 行