Wordpress / PHP:获取循环外变量的值

时间:2014-01-09 02:47:17

标签: php wordpress variables loops post

在我的WordPress模板(我正在开发)中,我有header.php和名为header-head.php的模板部分(在header.php中调用)。在index.php,我有其他模板parte,名为content.php(在index.php中调用)。我正在尝试获取我创建的分类法(命名感觉)的第一篇文章的信息,并返回header-head.php中的值。

content.php代码:

$terms = get_the_terms($post->ID, 'feeling');
foreach ($terms as $term) {
    //Always check if it's an error before continuing. get_term_link() can be finicky sometimes
    $term_link = get_term_link( $term, 'feeling' );
    if( is_wp_error( $term_link ) )
        continue;
    //We successfully got a link. Print it out.
    $term_return = '<a href="' . $term_link . '">Feeling ' . $term->name . '</a>';
}

if ( has_term( '', 'feeling' ) AND $count == 0 ) {
    $latest_feeling = "<p class='feeling-text'>" . $term_return . "</p>";
    $GLOBALS['feeling_post'] = $latest_feeling;
}

header-head.php代码:

$feeling_return = $GLOBALS['feeling_post'];
echo $feeling_return;

您可能已经注意到变量$ count,它会计算要检查第一篇帖子的帖子数量。我在index.php文件中完成了它,然后使用$ GLOBALS获取了content.php文件中的值,但现在我没有得到...

index.php循环代码:

<?php if ( have_posts() ) : ?>
    <?php $count = 0; ?>

        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>
        <?php $GLOBALS['count_post'] = $count++; ?>

            <?php
                /* Include the Post-Format-specific template for the content.
                 */
                get_template_part( 'partials/content', get_post_format() );
            ?>

        <?php endwhile; ?>

        <?php sensibus_paging_nav(); ?>

    <?php else : ?>

        <?php get_template_part( 'partials/content', 'none' ); ?>

    <?php endif; ?>

有人能帮助我吗?我做错了什么?

0 个答案:

没有答案