dongzheng7165 2014-12-09 21:41
浏览 46

Wordpress循环类别ID

I need help looping this template in categories id 9. This is what I have done so far:

$columns     = false;
$rows        = false;
$image_sizes = array();

if ( have_posts() ) :

    if ( is_page() ) {

        $grid_portfolio = new GridPortfolio( get_the_ID() );
    // Sets: $orientation, $aspect_ratio, $grid_size, $image_sizes, $columns, $rows
        extract( $grid_portfolio->get_options() );

    } else {

        $default_grid_portfolio = it_find_page_by_template( 'template-portfolio-grid.php', array( 'post_status' => 'publish' ) );

        if ( $default_grid_portfolio ) {
            $grid_portfolio = new GridPortfolio( $default_grid_portfolio[0]->ID );
            extract( $grid_portfolio->get_options() );
        }

        /**
         * We are on a Project Type page,
         * thus we need to check if there is any overriden options.
         *
         * Sets: $orientation, $aspect_ratio, $grid_size, $image_sizes, $columns, $rows
         */
        extract( fluxus_project_type_grid_options( get_queried_object()->term_id ) );

    }
    if ( ! is_numeric( $columns ) ) {
        $columns = 4;
    }

    if ( ! is_numeric( $rows ) ) {
        $rows = 3;
    }

    $max_size = $orientation == 'horizontal' ? $rows : $columns;

    fluxus_add_html_class( 'layout-portfolio-grid layout-portfolio-grid-' . $orientation );

    if ( $orientation != 'vertical' ) {
        fluxus_add_html_class( 'horizontal-page' );
    }
    get_header();

    fluxus_query_portfolio( array(
        'fluxus-project-type' => get_query_var( 'fluxus-project-type' )
    ) );

    ?>
    <div id="main" class="site">

        <div class="portfolio-grid" data-aspect-ratio="<?php esc_attr_e( $aspect_ratio ); ?>" data-orientation="<?php esc_attr_e( $orientation ); ?>" data-columns="<?php echo $columns; ?>" data-rows="<?php echo $rows; ?>"><?php

            while ( have_posts() ) :

                the_post();
    $project = new PortfolioProject( get_the_ID() );
                $featured = $project->get_featured_media();

                if ( ! $featured ) continue; // We have no media on this project, nothing to show.

                $thumbnail = FLUXUS_IMAGES_URI . '/no-portfolio-thumbnail.png';
                $thumbnail_2x = '';

                $thumbnail_data = $featured->get_thumbnail( 'fluxus-thumbnail' );
                $thumbnail_data_2x = $featured->get_thumbnail( 'fluxus-thumbnail-2x', 'fluxus-thumbnail' );

                if ( $thumbnail_data ) {
                    $thumbnail = $thumbnail_data['src'];
                }

                if ( $thumbnail_data_2x ) {
                    $thumbnail_2x = $thumbnail_data_2x['src'];
                    if ( $thumbnail_2x == $thumbnail ) {
                        $thumbnail_2x = '';
                    }
                }
    if ( isset( $image_sizes[get_the_ID()] ) ) {
                    $size = $image_sizes[get_the_ID()];
                    $size = $size > $max_size ? $max_size : $size;
                } else {
                    $size = 1;
                } ?>
                <article class="grid-project size-<?php echo $size; ?>" data-size="<?php echo $size; ?>" data-id="<?php echo esc_attr( get_the_ID() ); ?>">
                    <a href="<?php the_permalink(); ?>" class="preview" style="background-image: url(<?php echo esc_url( $thumbnail ); ?>);" data-hd-image="<?php echo esc_url( $thumbnail_2x ); ?>">
                        <span class="hover-box">
                            <span class="inner"><?php
                                if ( $project->meta_subtitle ) : ?>
                                    <i><?php echo $project->meta_subtitle; ?></i><?php
                                endif; ?>
                                <b><?php the_title(); ?></b>
                                <?php if ( post_password_required() ) : ?>
                                    <span class="password-required">
                                        <?php _e( 'Password required', 'fluxus' ); ?>
                                    </span>
                                <?php endif; ?>
                            </span>
                        </span>
                        <?php
                        /**
                         * Add <img /> tag, so that they can be found
                         * by search engines and social sharing widgets.
                         */ ?>
                        <img class="hide" src="<?php echo esc_url( ! empty( $thumbnail_2x ) ? $thumbnail_2x : $thumbnail ); ?>" alt="<?php esc_attr_e( get_the_title() ); ?>" />
                    </a>
                </article><?php
            endwhile; ?>
        </div>
    </div>
<?php
else:
    get_header();
endif;
wp_reset_query();
get_footer();
  • 写回答

1条回答 默认 最新

  • douping7105 2014-12-10 08:21
    关注

    Since you mentioned that this was to go in a template, you should review the WP template hierarchy. This would allow you to specify which templates to use under particular circumstances, such as when viewing a particular category. It's as simple as renaming your template category-9.php: this template would then be used when you visited example.com/blog/?cat=9

    If you're talking about displaying categpry posts in another template (for eaxmple, on the home page), then you need to query some posts before you can actually loop over them; you need to do this before your opening if ( have_posts() ). You can set different parameters for query that change which posts you receive. One of these available parameters is cat, which lets you specify which category to fetch posts from. You'd use it like so:

    $args = (
        'cat' => 9
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        while ($query->have_posts()) {) {
        // The rest of the code here
    

    I'd strongly recommend you read up on how WP_Query works

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题