我使用下面的代码来显示边栏中最喜欢的帖子的链接,但我想控制链接的数量,只显示3个链接,如果超出了这一点,我希望链接查看所有的最爱显示。
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
}
} 我怎样才能做到这一点?
请帮帮忙
发布于 2013-12-19 15:30:23
我在其他地方找到了va_get_dashboard_favorites函数,它看起来是相同的。最简单的方法是限制va_get_dashboard_favorites() WP_Query。例如:
方法1
这段代码是在wp-content/themes/vantage-new/includes/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循环中即可。例如:
<?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的更新更新)
根据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 {
?>发布于 2013-12-19 15:33:27
我相当肯定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>https://stackoverflow.com/questions/20685406
复制相似问题