2016-01-20 89 views
2

我需要改变如何更改代码,当您查看第二次

'orderby' => 'date',  
with 
'orderby' => 'random', 

当您查看网页的第二次 ü可以帮助我的页面?

<?php 
    $type = 'client'; 
    $args = array(
     'post_type' => $type, 
     'post_status' => 'publish', 
     'posts_per_page' => 8, 
     'caller_get_posts'=> 1, 
     'orderby' => 'date', 
     'order' => 'DESC' 
    ); 
    ?> 
+0

集饼干,并为您的cookie存在第二次.. – devpro

+1

你为什么不使用'从第一次“排序依据” =>“random'' ,因为用户第一次看到的结果永远不会第二次出现,那么它有什么用处......? –

回答

2

你可以做到这一点在下列方式: -

<?php 
    session_start(); 
    $_SESSION['pageviews'] = (isset($_SESSION['pageviews'])) ? $_SESSION['pageviews'] + 1 : 1; 
    $type = 'client'; 
    if($_SESSION['pageviews'] == 1){ 
     $order_by_data= 'date'; 
    }else{ 
     $order_by_data= 'random'; 
    } 
    $args = array(
     'post_type' => $type, 
     'post_status' => 'publish', 
     'posts_per_page' => 8, 
     'caller_get_posts'=> 1, 
     'orderby' => $order_by_data, 
     'order' => 'DESC' 
    ); 

    echo $type;echo "<pre/>";print_r($args);echo $_SESSION['pageviews']; 
    ?> 

注: - 基于会话计数器值$args将change.Thanks

+0

显示此为 警告:session_start():无法发送会话cookie - 已在C:\中发送的头文件(输出在C:\ xampp \ htdocs \ site \ wp-includes \ class.wp-styles.php中为127) xampp \ htdocs \ site \ wp-content \ themes \ site \ homepage.php在第56行 – manu

+0

@manu'session_start();'将在<?php'标签后面的页面顶部,而不是在中间或页面结尾。同时删除页面末尾的空白空间。谢谢。 –

+0

谢谢@ A-2-A :) – manu

2
<?php 
    //requires session_start(); 
    $orderBy = "random"; 
    if(!isset($_SESSION['visited'])) { 
     $_SESSION['visited'] = true; 
     $orderBy = "date"; 
    } 


    $type = 'client'; 
    $args = array(
     'post_type' => $type, 
     'post_status' => 'publish', 
     'posts_per_page' => 8, 
     'caller_get_posts'=> 1, 
     'orderby' => $orderBy, 
     'order' => 'DESC' 
    ); 
0

您需要使用Cookie或会话检查用户是否第二次访问您的页面。

<?php 
    session_start(); 
    if(!is_set($_SESSION['userVisit'])) { 
    $type = 'client'; 
    $args = array(
    'post_type' => $type, 
    'post_status' => 'publish', 
    'posts_per_page' => 8, 
    'caller_get_posts'=> 1, 
    'orderby' => 'date', 
    'order' => 'DESC' 
    ); 
    $_SESSION['userVisit']=1; 
    } 
    else { 
    $type = 'client'; 
    $args = array(
    'post_type' => $type, 
    'post_status' => 'publish', 
    'posts_per_page' => 8, 
    'caller_get_posts'=> 1, 
    'orderby' => 'random', 
    'order' => 'DESC' 
    ); 
    } 

我希望我是IST的时间有帮助

+0

这可能会使用重构,因为您不必要地复制代码。 – Repox