【问题标题】:Wordpress relationship loop in <head><head> 中的 Wordpress 关系循环
【发布时间】:2016-09-16 01:47:55
【问题描述】:

我有一个回显大约 10 个项目的页面。我希望它们在悬停时都有不同的背景图像。

我猜我需要在我的&lt;head&gt; 标记中添加我的循环,以便我可以应用背景图片。

这是我的循环:

<?php 
    // get background images for the services page
    $post = get_field('related_services');
        if( $posts ): ?>

            <style>

                <?php foreach( $posts as $post): ?>
                    <?php setup_postdata($post); ?>

                        .sector-box[style]::after {
                            background-image: url("<?php echo get_template_directory_uri(); ?>/images/creative-bg-hover.jpg");   
                            color:#<?php the_field( 'colour' ); ?>;
                        }

                <?php endforeach; ?>

            </style>

        <?php wp_reset_postdata(); ?>
    <?php endif; ?>

我不能让它为每个人都呼应一种风格..

我还需要为每个盒子应用一个 ID 吗?以便它知道将背景图像应用到哪个框?

这是我的html:

<?php 
            $posts = get_field('related_services');
            if( $posts ): ?>

                <div class="container">

                    <?php foreach( $posts as $post): ?>
                    <?php setup_postdata($post); ?>

                        <div class="sector-box reveal" style="background-color:<?php the_field( 'colour' ); ?>;">
                            <div class="sector-title">
                                <a href="<?php the_permalink(); ?>"><?php the_title( '' ); ?></a>
                            </div>
                        </div>
                        <!--Close Single Service box-->

                    <?php endforeach; ?>

            </div> 

            <?php wp_reset_postdata(); ?>
            <?php endif; ?>
            <!--Close Service Sector Boxes-->

【问题讨论】:

    标签: php html css wordpress advanced-custom-fields


    【解决方案1】:

    使用以下内容:

    <?php echo get_permalink($post->ID); ?>
    <?php echo get_the_title( $post->ID ); ?>
    <?php echo get_field('colour', $post->ID ); ?>
    

    和删除功能

    <?php setup_postdata($post); ?>
    <?php wp_reset_postdata(); ?>
    

    【讨论】:

    • OP为什么要“使用以下”和“删除功能”? 好的答案将始终解释所做的事情以及为什么以这种方式完成,不仅适用于 OP,而且适用于 SO 的未来访问者。
    猜你喜欢
    • 2019-09-26
    • 2011-10-10
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    相关资源
    最近更新 更多