【问题标题】:WordPress Notice: Undefined variable: post in C:WordPress 注意:未定义的变量:在 C 中发布:
【发布时间】: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 行

标签: php wordpress


【解决方案1】:

删除带有$post = ''; 的行。 $post 是一个 global 变量。

然后你尝试访问$post 属性ID

$terms = get_the_terms($post->ID, 'tagportfolio' );

这就是为什么你得到 未定义的变量:在 C 中发布:

【讨论】:

  • 解决办法是什么?
  • 你的问题是什么?
【解决方案2】:

你可以试试下面的方法。

这里是:

global $wp_query;
$post = $wp_query->post;

【讨论】:

  • 解释是什么?
猜你喜欢
  • 2012-09-24
  • 1970-01-01
  • 2021-03-07
相关资源
最近更新 更多