-5

I'm new to php and I'm trying to figure out why the following error is occurring:

Notice: Undefined variable: post in functions.php on line 59

Here is my functions.php code:

<?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();
}
?>

closed as off-topic by Pieter Goosen, fuxia Apr 5 '16 at 7:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?" – Pieter Goosen, fuxia
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    It i hard to see which line is 59. Can you edit to point to it? – Mark Kaplun Apr 5 '16 at 7:05
  • Please only post the relevant code, not the entire file, and also point to the specific line. You should also properly indent your code properly. It is messy and hard to read – Pieter Goosen Apr 5 '16 at 7:05
0

You have made a mistake in the function wpb_related_pages. Your code should look as below:

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);

Hope this helps!

Not the answer you're looking for? Browse other questions tagged or ask your own question.