Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress列出至少5篇文章的类别_Php_Mysql_Wordpress - Fatal编程技术网

Php Wordpress列出至少5篇文章的类别

Php Wordpress列出至少5篇文章的类别,php,mysql,wordpress,Php,Mysql,Wordpress,我使用以下代码在页面中列出我的类别: <?php $terms = get_terms( array( 'taxonomy' => 'category', 'hide_empty' => true, ) ); $count = count($terms); $categories = array(); if ($count &g

我使用以下代码在页面中列出我的类别:

        <?php
        $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => true,
            ) );
        $count = count($terms);
        $categories = array();
        if ($count > 0) :
            foreach ($terms as $term) {
                $args = array(
                    'post_type'        => 'post',
                    'posts_per_page'   => 1,
                    'show_count'       => 1,
                    'orderby'          => 'rand',
                    'post_status'      => 'publish',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'categorys',
                            'field' => 'slug',
                            'terms' => $term->slug
                            )
                        )
                    );

                $post_from_category = new WP_Query( $args );
                if( $post_from_category->have_posts() ){
                    $post_from_category->the_post();
                }else{}
                $term->slug;
                $term->name; ?>               
                <article id="post-<?php the_ID(); ?>" <?php post_class('thumb-block'); ?>>
                    <a href="<?php echo bloginfo('url'); ?>?categories=<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
                        <header class="entry-header">       
                            <span class="category-title"><?php echo $term->name; ?></span>
                        </header><!-- .entry-header -->
                    </a>
                </article><!-- #post-## -->
            <?php }
        endif; ?>

我的问题是:我怎样才能列出至少只包含5个帖子的类别

        <?php
        $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => true,
            ) );
        $count = count($terms);
        $categories = array();
        if ($count > 0) :
            foreach ($terms as $term) {
                $args = array(
                    'post_type'        => 'post',
                    'posts_per_page'   => 1,
                    'show_count'       => 1,
                    'orderby'          => 'rand',
                    'post_status'      => 'publish',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'categorys',
                            'field' => 'slug',
                            'terms' => $term->slug
                            )
                        )
                    );

                $post_from_category = new WP_Query( $args );
                if( $post_from_category->have_posts() ){
                    $post_from_category->the_post();
                }else{}
                $term->slug;
                $term->name; ?>               
                <article id="post-<?php the_ID(); ?>" <?php post_class('thumb-block'); ?>>
                    <a href="<?php echo bloginfo('url'); ?>?categories=<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
                        <header class="entry-header">       
                            <span class="category-title"><?php echo $term->name; ?></span>
                        </header><!-- .entry-header -->
                    </a>
                </article><!-- #post-## -->
            <?php }
        endif; ?>

多谢各位

foreach
循环中,您只需检查以确保
$term->count
大于或等于5。我还注意到了其他一些事情:

        <?php
        $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => true,
            ) );
        $count = count($terms);
        $categories = array();
        if ($count > 0) :
            foreach ($terms as $term) {
                $args = array(
                    'post_type'        => 'post',
                    'posts_per_page'   => 1,
                    'show_count'       => 1,
                    'orderby'          => 'rand',
                    'post_status'      => 'publish',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'categorys',
                            'field' => 'slug',
                            'terms' => $term->slug
                            )
                        )
                    );

                $post_from_category = new WP_Query( $args );
                if( $post_from_category->have_posts() ){
                    $post_from_category->the_post();
                }else{}
                $term->slug;
                $term->name; ?>               
                <article id="post-<?php the_ID(); ?>" <?php post_class('thumb-block'); ?>>
                    <a href="<?php echo bloginfo('url'); ?>?categories=<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
                        <header class="entry-header">       
                            <span class="category-title"><?php echo $term->name; ?></span>
                        </header><!-- .entry-header -->
                    </a>
                </article><!-- #post-## -->
            <?php }
        endif; ?>
  • 您定义了一个
    $categories
    数组,但似乎不再使用它
  • 如果:endif,尽量不要混用
    if
    if{}
    语句-为了便于维护,请坚持使用语法
  • 您只需检查
    $terms
    是否设置为真实值,而不必检查其计数
  • 您确定您的税务查询设置正确吗?它正在检查分类法“分类法”,这是“分类”的一个打字错误
  • ->have_posts()检查之后,不需要空的
    else{}
    循环
  • 在本文之前,您不需要实例化
    $term
    对象变量
  • 你应该考虑使用而不是
综上所述,这应该让你开始:

        <?php
        $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => true,
            ) );
        $count = count($terms);
        $categories = array();
        if ($count > 0) :
            foreach ($terms as $term) {
                $args = array(
                    'post_type'        => 'post',
                    'posts_per_page'   => 1,
                    'show_count'       => 1,
                    'orderby'          => 'rand',
                    'post_status'      => 'publish',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'categorys',
                            'field' => 'slug',
                            'terms' => $term->slug
                            )
                        )
                    );

                $post_from_category = new WP_Query( $args );
                if( $post_from_category->have_posts() ){
                    $post_from_category->the_post();
                }else{}
                $term->slug;
                $term->name; ?>               
                <article id="post-<?php the_ID(); ?>" <?php post_class('thumb-block'); ?>>
                    <a href="<?php echo bloginfo('url'); ?>?categories=<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
                        <header class="entry-header">       
                            <span class="category-title"><?php echo $term->name; ?></span>
                        </header><!-- .entry-header -->
                    </a>
                </article><!-- #post-## -->
            <?php }
        endif; ?>
<?php
    $term_args = array(
        'taxonomy'   => 'category',
        'hide_empty' => true,
    );

    if( $terms = get_terms( $term_args ) ){
        foreach( $terms as $term ){
            if( $term->count >= 5 ){
                $args = array(
                    'post_type'        => 'post',
                    'posts_per_page'   => 1,
                    'show_count'       => 1,
                    'orderby'          => 'rand',
                    'post_status'      => 'publish',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'categorys',
                            'field' => 'slug',
                            'terms' => $term->slug
                        )
                    )
                );

                $post_from_category = new WP_Query( $args );

                if( $post_from_category->have_posts() ){ $post_from_category->the_post(); ?>               
                    <article id="post-<?php the_ID(); ?>" <?php post_class( 'thumb-block' ); ?>>
                        <a href="<?= home_url( "?categories={$term->slug}" ); ?>" title="<?= $term->name; ?>">
                            <header class="entry-header">
                                <span class="category-title"><?= $term->name; ?></span>
                            </header>
                        </a>
                    </article>
                <?php }
            }
        }
    }
?>