我已创建搜索。php来自二十一主题搜索模板。搜索模板工作正常,但当未找到任何结果时,我遇到了一个问题
我收到了这个PHP通知:通知:尝试获取非对象wp includes/post模板的属性。php第29行
帖子模板第29行如下所示:
function get_the_ID() {
return get_post()->ID;<-this is the source of the problem
}
我假设这是因为
get_post()->ID
未设置(当未显示结果时),因为我在搜索模板中输入了以下代码:
<?php if ( !isset ($post->ID)) {
echo \'not set\';<- this will echo "not set" if NO results are found and
PHP notice will be displayed:(
}
else{
echo \'set\';<- this will echo "set" if results ARE found and
PHP notice will NOT be displayed
}
?>
所以我的问题是如何设置
$post->ID
(如果未设置)以删除PHP通知
非常感谢您的帮助,谢谢!!
搜索php代码:
<?php
get_header(); ?>
<?php get_sidebar(); ?>
<?php get_sidebar(\'secondary\'); ?>
<?php if ( !isset ($post->ID )) {
echo \'not set\';
}
else{
echo \'set\';
}
?>
<div id="blogwrapper">
<div id="blog">
<h1 class="page-title"><?php printf( __( \'Search Results for: %s\' ,\'your-override\' ), \'<span>\' . get_search_query() . \'</span>\' ); ?></h1>
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<div <?php post_class();?> id="post-<?php the_ID(); ?>">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="entry">
<div class="featured_img">
<?php the_post_thumbnail();
echo \'<div class="featured_caption">\' . get_post(get_post_thumbnail_id())->post_excerpt . \'</div>\';?>
</div><!--/featured_img-->
<?php // let\'s enable more link on pages...
global $more;
$more = 0;
?>
<?php the_content(); ?>
<div class="clear"></div>
<div class="custom_fields"><?php the_meta(); ?></div><br/>
<p class="postmetadata">
<?php _e(\'Filed under:\'); ?> <?php the_category(\', \') ?> <?php _e(\'by\'); ?> <?php the_author(); ?><br/><?php the_tags(\'Tags:\', \', \', \'<br />\'); ?>
<?php _e(\'Posted on: \'); ?><?php the_time(\'l, F jS, Y\'); ?><br/>
<?php comments_popup_link(\'No Comments »\', \'1 Comment »\', \'% Comments »\'); ?> <?php edit_post_link(\'Edit\', \' | \', \'\'); ?>
</p>
</div>
</div>
<?php endwhile; ?>
<div class="navigation">
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, $paged ),
\'total\' => $wp_query->max_num_pages
) );
?>
</div>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( \'Nothing Found\'); ?></h1>
</header>
<div class="entry-content">
<p><?php _e( \'Sorry, but nothing matched your search criteria. Please try again with some different keywords.\'); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
</div>
</div><!--/blogwrapper-->
<?php get_footer(); ?>
SO网友:Dejo Dekic
看来我的问题不是搜索。php,但我的页脚。php此代码导致错误<?php post_comments_feed_link( $link_text = \'Comments RSS\', $post_id = \'post_id\', $feed = \'rss2\' ); ?>
无论如何,我的页脚代码如下所示:
<div id="footer">
<?php wp_footer(); ?>
<p>
<strong>Copyright 2013 <?php bloginfo(\'name\'); ?> | All Rights Reserved.</strong>
Designed by Dex
</p>
<p><a href="<?php bloginfo(\'rss2_url\'); ?>">Latest Stories RSS</a> |
<?php post_comments_feed_link( $link_text = \'Comments RSS\', $post_id = \'post_id\', $feed = \'rss2\' ); ?></p>
</div>
</div><!--/wrapper-->
我使用@Svetlana Silina对其进行了修改,回答如下:
<div id="footer">
<?php wp_footer(); ?>
<p>
<strong>Copyright 2013 <?php bloginfo(\'name\'); ?> | All Rights Reserved.</strong>
Designed by Dex
</p>
<p><a href="<?php bloginfo(\'rss2_url\'); ?>">Latest Stories RSS</a> |
<?php if(get_post()){
?>
<?php post_comments_feed_link( $link_text = \'Comments RSS\', $post_id = \'post_id\', $feed = \'rss2\' ); ?></p>
<?php } else if(!get_post()){
?>
<?php post_comments_feed_link( $link_text = \'Comments RSS\', $post_id = \'1\', $feed = \'rss2\' ); ?></p>
<?php } ?>
</div>
现在一切都很好,没有PHP通知,即使没有显示结果。生活又好了:=)