user2798091
user2798091

Reputation: 606

Controlling the number of links shown in Wordpress

I am using the following code to show links of favourite posts in the sidebar but I want to controll the number of links to show only 3 links and if this is exceeded, i want the link to View All favourites to show.

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
}
 }          

How can I achieve this?

Please help

Upvotes: 0

Views: 53

Answers (2)

Braunson
Braunson

Reputation: 717

I found the va_get_dashboard_favorites function elsewhere which looks to be the same. The easiest way would be to limit the va_get_dashboard_favorites() WP_Query. e.g.

METHOD 1

This code was found in wp-content/themes/vantage-new/includes/dashboard.php by the way:

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;

}

Then to add a view-all link, just add it in iunder the while loop. e.g.

<?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 {
?>

METHOD 2 (Updated as per OP's update)

As per OP's comments, the updated code to just show 3 links

<?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 {
?>

Upvotes: 1

user3119733
user3119733

Reputation: 1

I'm fairly sure that WP_Query would work in this situation.

<?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>

Upvotes: 0

Related Questions