doumei4964 2015-02-27 02:29
浏览 44

如何在嵌套循环中获取父循环的帖子ID

I'm trying to figure out how to get the current post ID from my main wp_query loop to work in the nested loop..

I've added my loops below with most HTML removed to make it cleaner to see.

I need to replace the "16" where it says "$currentID = 16;" in the nested loop with the actual current post ID from the main loop.

<?php $related_query = new WP_Query( 'post_type=post&posts_per_page=-1' ); ?>
        <?php if( $related_query->have_posts() ): ?>
            <?php while ( $related_query->have_posts() ) : $related_query->the_post(); ?>
                <?php the_ID(); ?>
                <?php the_time('F j, Y'); ?>
                <?php the_category(); ?>
                <?php echo get_edit_post_link(); ?>
                <?php echo get_post_meta(get_the_ID(), 'cf_meta-desc', true); ?>
                <?php echo get_post_meta(get_the_ID(), 'cf_xray', true); ?>
                <?php the_tags(); ?>
                <ul>
                    <h4>Recommended Articles</h4>
                    <?php 
                    $related_cfs = get_post_meta( get_the_ID(), 'cf_related' );
                    foreach($related_cfs as $related_cf) {
                    echo '<li>';
                    echo '<span class="related-links__id"><a href="#post-' .$related_cf. '">' .$related_cf. '</a></span>';
                    echo '<span class="related-links__title"><a target="_blank" href="' .get_permalink($related_cf). '">' .get_the_title($related_cf). '</a></span>';
                    echo '<span class="related-links__edit"><a target="_blank" href="' .get_edit_post_link($related_cf). '">edit</a></span>';
                    echo '</li>';
                    } ?>
                </ul>


    <?php global $post;$backup=$post; //saves main query data before calling nested query ?>


                <!-- BEGIN NESTED LOOP -->
                <?php $referral_query = new WP_Query( 'meta_key=cf_related&posts_per_page=-1' ); ?>

                <ol>
                    <h4 class="referring-links__header">Linkbacks (<?php
                        $meta_key = 'cf_related';
                        $currentID = 16;
                        $sql = "SELECT count(DISTINCT pm.post_id)
                        FROM $wpdb->postmeta pm
                        JOIN $wpdb->posts p ON (p.ID = pm.post_id)
                        WHERE pm.meta_key = '$meta_key'
                        AND pm.meta_value = '$currentID'
                        ";
                        $count = $wpdb->get_var($sql);
                        echo "$count";
                        ?>)
                    </h4>

                    <?php while ( $referral_query->have_posts() ) : $referral_query->the_post(); ?>

                    <?php 
                        $currentID = 16;
                        $arrayCFrelated = get_post_custom_values('cf_related');
                        if (in_array($currentID, $arrayCFrelated))  
                    { ?>

                    <li>
                        <?php the_ID(); ?>
                        <?php the_title(); ?>
                        <?php echo get_edit_post_link(); ?>
                    </li>

                    <?php } ?>

                    <?php endwhile; ?>
                </ol>
                <!-- END NESTED LOOP -->


    <?php $post=$backup; //brings back main query data before called nested query ?>

                <?php echo get_post_meta(get_the_ID(), 'cf_img-feature', true); ?>

            <?php endwhile; ?>
        <?php else: ?>
            <p class="center">Nothing found.</p>
        <?php endif; ?>
    <?php wp_reset_query(); ?>
  • 写回答

1条回答 默认 最新

  • douzhi9395 2015-02-27 02:39
    关注

    The code you posted is very hard to read because it is all nested and distributed between opening and closing php tags.

    First of all - You should consider to get familiar with functions and objects as an alternative to nest everything within the same loop. This will also help other developers who work with you to understand your code.

    As for your problem. Try using other types of loops to get indices within the loop. For example:

    for ($i1=0; $i1 < count($yourarray); $i1++) {
       // ...
       echo "index: $i1 <br />";
       echo "value: {$yourarray[$i1]}";
    }
    

    or

    foreach ($array AS $idx => $value) {
       // ...
       echo "index: $idx <br />";
       echo "value: $value";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端