2016-01-20 94 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