【问题标题】:Wordpress post object getting page title rather than object titleWordpress 帖子对象获取页面标题而不是对象标题
【发布时间】:2019-07-24 07:44:53
【问题描述】:

我有一个 ACF 转发器,它重复发布对象。我将 postdata 更改为 post 对象而不是页面,以便我可以获得标题和缩略图。这对第一个非常有效,但是后续对象会提取正确的缩略图,但标题是从页面标题中提取的。

代码如下:

<?php if( have_rows('service_repeater') ): ?>
<?php while ( have_rows('service_repeater') ) : the_row(); ?>

    <?php $post_object = get_sub_field('service'); ?>

    <?php if( $post_object ): ?>
        <?php $post = $post_object; ?>
        <?php setup_postdata( $post ); ?>
        <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
        <a href="<?php the_permalink(); ?>" class="service">
            <div class="background" style="background-image: url('<?php echo $url; ?>');"></div>
            <div class="content">
                <h3><?php the_title(); ?></h3>
                <p><?php the_field('read_more_text'); ?></p>
            </div>
        </a>
        <?php unset($post_object, $post); ?>
        <?php wp_reset_postdata(); ?>
    <?php endif; ?>

<?php endwhile; ?>

任何帮助将不胜感激!

【问题讨论】:

  • 好的,所以我删除了&lt;?php unset($post_object, $post); ?&gt;,它运行良好。这会在以后给我带来变量方面的问题吗??

标签: wordpress repeater advanced-custom-fields postdata


【解决方案1】:

好的,所以我删除了&lt;?php unset($post_object, $post); ?&gt;,它现在可以工作了。我担心这会给我以后的变量带来麻烦。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2016-02-20
    • 2010-12-04
    • 2014-04-21
    • 1970-01-01
    • 2018-10-12
    • 2021-11-01
    相关资源
    最近更新 更多