【问题标题】:How to call meta functions in Wordpress properly如何在 Wordpress 中正确调用元函数
【发布时间】:2012-08-22 17:41:25
【问题描述】:

我正在为我的 Wordpress 网站开发一个作者徽章,并且我正在学习如何调用元函数(需要在循环中)。我调用的元函数与作者生物相关,如用户名、姓氏等

这是一个代码示例:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    About <?php the_author(); ?>, the author of this blog
    <?php userphoto_the_author_thumbnail() ?>
    <?php get_the_author_meta( 'description' ); ?>

<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

我将此示例添加到我的 author.php 文件中并且它可以工作,但是它多次显示相同的内容(因此出现了循环)。如果我想在 Wordpress 中调用元函数而不是像这样多次回显它们,我该怎么做?

我确定我做错了,有一个正确的方法来实现它。

如果您选择回复,请详细说明,因为我对 PHP 编码的了解达到了我今天了解 echo 是什么的程度

【问题讨论】:

    标签: php wordpress function call meta


    【解决方案1】:

    由于您已将此添加到您的 author.php 文件中,因此所有帖子都将由一位作者发布。所以我猜你只想显示这个内容一次。

    像这样改变你的代码就可以了:

    <?php $show_author_data = TRUE;?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php if(!$show_author_data){ ?>
      About <?php the_author(); ?>, the author of this blog
      <?php userphoto_the_author_thumbnail() ?>
      <?php get_the_author_meta( 'description' ); ?>
    <?php $show_author_data = FALSE; } ?>
    
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    

    这只会为第一遍设置$show_author_data 标志。

    【讨论】:

    • 我将您的代码添加到我的 author.php 文件中,但它返回 Parse error: syntax error, unexpected $end in 。你知道是什么问题吗?
    • 编辑了我的代码。对不起。我没有包括没有改变的部分。
    猜你喜欢
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多