douba2705 2016-06-27 11:23
浏览 25
已采纳

将get_posts循环与另一个数组相结合,并按日期随机排序

I have a page I am building in WordPress which includes tweets by the Twitter API and then a standard get_posts loop.

At the moment they are displaying one after the other but I wish to create a masonry style grid which includes both.

Is there a way to output both arrays in between each other by date so the items are broken up instead of it outputting tweets first then the posts?

Here is my code.

$twitter = new TwitterAPIExchange($settings);

$string = json_decode($twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest(),$assoc = TRUE);

echo '<div class="funny-grid">';
echo '<div class="grid-sizer"></div>';
foreach($string as $items)
    {
    $string = $items['text'];
    $regex = "@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@";

    echo '<div class="grid-item tweet ">';
        echo '<i class="fa fa-lg fa-twitter inverse" aria-hidden="true"></i>';
        echo '<div class="tweet-text">' . preg_replace($regex, ' ', $string) . '</div>' ."<br />";
        echo '<div class="tweet-user">' . '@' . '<a href="http://www.twitter.com/' . $items['user']['screen_name'] . '">' . $items['user']['screen_name'] . '</a>' . '</div>';
        echo '<div class="tweet-date">' . $items['created_at'] . '</div>' . "<br />";

    echo '</div>';
    }

    //counts the amount of items in the array.
    /*echo "Number of items:" . count($string);*/
?>
<!--<div style="clear: both;"></div>-->

            <?php 
        query_posts('post_type=funny_wall &posts_per_page=8');
        if(have_posts()):while(have_Posts()):the_post();
         $img = wp_get_attachment_url(get_post_thumbnail_id($post->ID), "full");?>
            <div class="grid-item image">
                <div class="as">
                <img src="<?php echo $img ; ?>">
                    <a href="<?php the_permalink();?>">
                    </a>            
                </div>                                  
            </div>  

            <?php
            endwhile;
            endif;
            wp_reset_query();
            ?>

        </div>
        </div>
  • 写回答

1条回答 默认 最新

  • dr2898 2016-06-27 12:13
    关注

    Instead of echoing the results directly, you could put them into an array, and then use shuffle() to randomise before outputting it in one go.

    e.g.: tweets

    $grid_items[] =  '<div class="grid-item tweet ">
        <i class="fa fa-lg fa-twitter inverse" aria-hidden="true"></i>
        <div class="tweet-text">' . preg_replace($regex, ' ', $string) . '</div>' .'<br />
        <div class="tweet-user">' . '@' . '<a href="http://www.twitter.com/' . $items['user']['screen_name'] . '">' . $items['user']['screen_name'] . '</a>' . '</div>
        <div class="tweet-date">' . $items['created_at'] . '</div>' . '<br />
        </div>';
    

    posts:

    $grid_items[] = "<div class='grid-item image'>
                <div class='as'>
                <img src=".$img.">
                    <a href=".the_permalink().">
                    </a>            
                </div>                                  
            </div>";
    

    then use:

    shuffle($grid_items);
    foreach($grid_items as $grid_item){
        echo $grid_item;
    }
    

    when you want to output them.

    http://php.net/manual/en/function.shuffle.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?