2013-12-19 34 views
0

我正在使用下面的代碼來顯示側邊欄中收藏的帖子的鏈接,但我想控制鏈接的數量只顯示3個鏈接,如果超過,我想鏈接查看所有收藏夾以顯示。控制Wordpress中顯示的鏈接數

PHP:

<?php 
$favorites = va_get_dashboard_favorites($dashboard_user->ID, (bool) $is_own_dashboard); 

if ($favorites->post_count > 0) { 
while ($favorites->have_posts()) : $favorites->the_post(); 

$post_status = $is_own_dashboard ? 'post-status' : ''; 
?> 
<article id="post-<?php the_ID(); ?>" <?php post_class($post_status); ?>> 
    <a href="<?php echo get_permalink($ID); ?>">My link to a post or page</a><?php echo get_the_title($ID); ?> 
</article> 
<?php 
endwhile; 
} else { 
?> 

<?php 
} 
}   

我怎樣才能做到這一點?

請幫

回答

1

我發現va_get_dashboard_favorites功能的其他地方看起來是一樣的。最簡單的方法是限制va_get_dashboard_favorites()WP_Query。例如

方法1

此代碼是在可溼性粉劑內容/主題/有利-新/發現包括/ dashboard.php順便說

function va_get_dashboard_favorites($user_id, $self = false) { 

    $favorites = new WP_Query(array(
     'connected_type' => 'va_favorites', 
     'connected_items' => $user_id, 
     'nopaging'  => true, 
     'posts_per_page' => 3, // limiting posts to 3 
    )); 

    return $favorites; 

} 

然後添加一個視圖 - 所有鏈接,只需將它添加到iunder while循環中即可。例如

<?php 
    $favorites = va_get_dashboard_favorites($dashboard_user->ID, (bool) $is_own_dashboard); 

    if ($favorites->post_count > 0) { 
     while ($favorites->have_posts()) : $favorites->the_post(); 

     $post_status = $is_own_dashboard ? 'post-status' : ''; 
?> 
    <article id="post-<?php the_ID(); ?>" <?php post_class($post_status); ?>> 
    <?php get_template_part('content-listing', get_post_status()); ?> 
    </article> 
<?php 
     endwhile; 
?> 
    <br /><a href="#yourview-all-link-here">View all</a> 
<?php 
    } else { 
?> 

方法2(已更新爲每個操作的更新)

按OP的評論,更新的代碼只顯示3個鏈接

<?php 
    $favorites = va_get_dashboard_favorites($dashboard_user->ID, (bool) $is_own_dashboard); 

    $counter = 0; 
    $max = 3; 
    if ($favorites->post_count > 0) { 
     while ($favorites->have_posts() and ($counter < $max)) : $favorites->the_post(); 

      $post_status = $is_own_dashboard ? 'post-status' : ''; 
?> 
    <article id="post-<?php the_ID(); ?>" <?php post_class($post_status); ?>> 
    <?php get_template_part('content-listing', get_post_status()); ?> 
    </article> 
<?php 
      $counter++; 
     endwhile; 
?> 
    <br /><a href="#yourview-all-link-here">View all</a> 
<?php 
    } else { 
?> 
+0

我改變了代碼升技。我不想限制帖子的數量,但只是顯示的鏈接數量 – user2798091

+0

OP更新了代碼,檢查方法#2。 – Braunson

+0

謝謝..但你提供的代碼限制了帖子,而不是用'

  • ' – user2798091

    0

    我相當肯定那WP_Query可以在這種情況下工作。

    <?php $posts = WP_Query(array(
        'posts_per_page' => 3 
        //any other options can go in this array 
    )); ?> 
    // Your code goes here 
    <?php wp_reset_query(); ?> //This makes sure your query doesn't affect other areas of the page 
    <a href="linktomorefavorites/">View More</a> 
    
    +0

    謝謝,但添加此代碼給我調用未定義的函數WP_Query()錯誤 – user2798091