【问题标题】:Wordpress Looping Category IDWordpress 循环类别 ID
【发布时间】:2014-12-09 21:41:18
【问题描述】:

我需要帮助在 categories id 9 中循环这个模板。这是我到目前为止所做的:

$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();

【问题讨论】:

  • 你有什么问题?您希望代码输出什么?
  • 嗨,程序员先生,我需要循环该模板类别 id 9。
  • 如何在类别中插入循环?
  • 我需要它是类别。请帮帮我
  • 你到底想循环什么?类别 id 9 或类别 9 名称的帖子?你需要做一个 wp_query codex.wordpress.org/Class_Reference/WP_Query

标签: php wordpress loops templates categories


【解决方案1】:

既然你提到这是一个模板,你应该review the WP template hierarchy。这将允许您指定在特定情况下使用哪些模板,例如查看特定类别时。就像重命名你的模板category-9.php一样简单:当你访问example.com/blog/?cat=9时,这个模板就会被使用

如果您正在讨论在 another 模板中显示分类帖子(例如,在主页上),那么您需要先查询一些帖子,然后才能真正循环它们;您需要在打开 if ( have_posts() ) 之前执行此操作。您可以为查询设置不同的参数,以更改您收到的帖子。 One of these available parameters is cat,可让您指定从哪个类别获取帖子。你会这样使用它:

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

我强烈推荐你read up on how WP_Query works

【讨论】:

  • 代码放在哪里?在这里 if ( have_posts() ) : if ( is_page() ) { $grid_portfolio = new GridPortfolio( get_the_ID() );或者在这里 while ( have_posts() ) : the_post(); $project = new PortfolioProject(get_the_ID()); $featured = $project->get_featured_media(); if ( ! $featured ) 继续; // 我们没有关于这个项目的媒体,没有什么可展示的。 $thumbnail = FLUXUS_IMAGES_URI 。 '/no-portfolio-thumbnail.png'; $thumbnail_2x = '';
  • 你能用上面的代码给我创建循环类别吗?请:(
猜你喜欢
  • 1970-01-01
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 2018-09-25
  • 1970-01-01
相关资源
最近更新 更多