我是php新手,我正试图弄清楚为什么会发生以下错误:
Notice: Undefined variable: post in functions.php on line 59
这是我的功能。php代码:
<?php
// related pages
function wpb_related_pages() {
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag)
$tag_ids[] = $individual_tag->term_id;
$args=array(
\'post_type\' => \'page\',
\'tag__in\' => $tag_ids,
\'post__not_in\' => array($post->ID),
\'posts_per_page\'=>4
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
echo \'<div id="relatedpages"><h3>Related Terms</h3><ul>\';
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<li>
<div class="relatedthumb">
<a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(\'thumb\'); ?></a>
</div>
<div class="relatedcontent">
<h4>
<a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
<p>
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 90, \'...\');?>
<a href="<?php the_permalink() ?>"> Read More</a>
</p>
</div>
</li>
<?
}
echo \'</ul></div>\';
} else {
echo "No Related Pages Found:";
}
}
$post = $orig_post;
wp_reset_query();
}
?>
最合适的回答,由SO网友:Mehul Gohil 整理而成
您在函数中出错wpb_related_pages
. 您的代码应如下所示:
Updated Code with change needed:
// related pages
function wpb_related_pages() {
# Global $post variable should be called at the start of the function.
global $post;
$orig_post = $post;
$tags = wp_get_post_tags($post->ID);
希望这有帮助!