首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >控制Wordpress中显示的链接数

控制Wordpress中显示的链接数
EN

Stack Overflow用户
提问于 2013-12-19 15:24:02
回答 2查看 45关注 0票数 0

我使用下面的代码来显示边栏中最喜欢的帖子的链接,但我想控制链接的数量,只显示3个链接,如果超出了这一点,我希望链接查看所有的最爱显示。

PHP:

代码语言:javascript
复制
<?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
}
 }          

我怎样才能做到这一点?

请帮帮忙

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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中找到的,顺便说一句

代码语言:javascript
复制
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循环中即可。例如:

代码语言:javascript
复制
<?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个链接

代码语言:javascript
复制
<?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 {
?>
票数 1
EN

Stack Overflow用户

发布于 2013-12-19 15:33:27

我相当肯定WP_Query会在这种情况下工作。

代码语言:javascript
复制
<?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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20685406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档