父类别的简单输出

时间:2018-02-13 14:07:27

标签: php wordpress

我在WP的模板页面中使用此代码。

<div id="content" role="main">
<?php

$query = array(
'post_type'      => 'post',
'category_name'  => 'jewellery-design',
'orderby'        => 'date',
'order'          => 'DESC'
);

$featured_home = new WP_Query( $query );

if( $featured_home->have_posts() ) {
?>


<div class="row">
    <?php while ( $featured_home->have_posts() ) : $featured_home->the_post();?>
     <span class="cat"><?php the_category(); ?> 
    <div class="col-3">
        <a href="<?php the_permalink(); ?>">
            <div class="featured-home-img" <?php
            if ( $thumbnail_id = get_post_thumbnail_id() ) {
                if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
                    printf( ' style="background-image: url(%s);"', $image_src[0] );     
                }?>>

                <div class="blog-info-content">

                    <h3><?php the_title(); ?></h3>
                    <div class="obsah"><?php the_content(); ?></div>   </span>
                                    <p class="postmetadata">
Posted in <?php the_category(', ') ?> 
<strong>|</strong>
<?php edit_post_link('Edit','','<strong>|</strong>'); ?>  

            </span>
                </div>
            </div>
        </a>
    </div>
<?php
endwhile;
?>
</div>
<?php
}
wp_reset_postdata();
?>    
      </div>

我得到这样的输出: enter image description here

但是,我希望输出如下: enter image description here

有人能告诉我怎么办?或者我可以使用一些WordPress插件让我更容易吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

我可能会考虑先将它们分组,然后在已排序的组中重新循环。我在Wordpress上有点生疏,所以我基本上设置了它,但所有的函数/方法需要return值,而不是echo,所以你需要调整它:

<?php
# Create base function
function getItemsByCategoryType($category)
{
    $query = array(
        'post_type'      => 'post',
        'category_name'  => $category,
        'orderby'        => 'date',
        'order'          => 'DESC'
    );
    # Get query
    $featured_home = new WP_Query($query);
    # Just stop if empty
    if(!$featured_home->have_posts())
        return false;
    # Set a default
    $array = array();
    while($featured_home->have_posts()){
        $featured_home->the_post();
        $thumbnail_id =  get_post_thumbnail_id();
        $image_src    =  ($thumbnail_id)? wp_get_attachment_image_src($thumbnail_id,'normal-bg')) : false;
        # You are looking to store the category title as the key
        # That will isolate each group
        $array[the_category()][] = array(
            'thumb_id' => $thumbnail_id,
            'image' => $image_src,
            'permlink' => the_permalink(),
            'title' => the_title(),
            'content' => the_content(),
            'categories' => the_category(', '),
            'edit_mode' => edit_post_link('Edit','','<strong>|</strong>')
        );
    }
    wp_reset_postdata();
    # Send back stored data as an array
    return $array;
}

# To use, get the data from your function    
$Items = getItemsByCategoryType('jewellery-design') ?>

<div id="content" role="main">
    <?php if(!empty($Items)): ?>
    <div class="row">
        <?php foreach($Items as $categories => $rows): ?>
        <span class="cat"><?php echo $categories ?>
            <?php foreach($rows as $row): ?>
            <div class="col-3">
                <a href="<?php echo $row['permlink'] ?>">
                    <div class="featured-home-img"<?php if(!empty($row['image'][0])) echo ' style="background-image: url('.$row['image'][0].');"' ?>>
                        <div class="blog-info-content">
                            <h3><?php echo $row['title'] ?></h3>
                            <div class="obsah">
                                <?php echo $row['content'] ?>
                            </div>
                            <p class="postmetadata">Posted in <?php echo $row['categories'] ?><strong>|</strong><?php $row['edit_mode'] ?></p>
                        </div>
                    </div>
                </a>
            </div>
            <?php endforeach ?>
        </span>
        <?php endforeach ?>
    </div>
    <?php endif ?>
</div>
  

注意:重申一下,我不能快速使用WP,所以你需要了解我提出的建议并修复它,特别是在返回值时允许WP功能为echo内容。

修改

你想要一个数组出来的功能类似于:

Array (
    [ Fibre Collection ] => Array (
        [ 0 ] => Array (
            [ title ] => FIBRE_BRACELET
            [ content ] => etc...,
            etc...
        )
    ),
    [ Tetragon Collection ] => Array (
        [ 0 ] => Array (
            [ title ] => Test2
            [ content ] => etc...,
            etc...
        ),
        [ 1 ] => Array (
            [ title ] => TETRAGÓN_BRACELET
            [ content ] => etc...,
            etc...
        )
    )
)

答案 1 :(得分:0)

我找到了我需要的输出解决方案。如果这有助于任何人:)

$cat_args=array(
  'orderby' => 'name',
  'order' => 'ASC',
  'parent' => 2
   );
$categories=get_categories($cat_args);
  foreach($categories as $category) {
    $args=array(
      'showposts' => -1,
      'category__in' => array($category->term_id),
      'caller_get_posts'=>1
    );
    $posts=get_posts($args);
      if ($posts) {
        echo '<div class="test"><p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        foreach($posts as $post) {
          setup_postdata($post); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <p><a href="<?php the_permalink() ?>" <?php the_content(); ?></a></p>
          <?php
        } // foreach($posts
      echo '</div>';}  // if ($posts
    } // foreach($categories
?>

'parent'=&gt; 2表示您希望从中获取帖子的父类别的ID。

度过愉快的一天。