WordPress的:循环交替的帖子类型和输出特色的图像 [英] Wordpress: looping through alternating post types and outputting featured image

查看:157
本文介绍了WordPress的:循环交替的帖子类型和输出特色的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个职位类型,我想交替输出,但没有使用多个循环,所以我发现

然而,这是不理想的,因为我需要输出the_post_thumbnail,我发现我无法使用此方法( echo $ smallPosts-> posts [$ i] - > post_thumbnail; 什么都不做)。另外我读了 post_content the_content(); 不一样 - 后者是我想要使用的。

关于如何循环交替的帖子类型和对输出进行更多的控制的任何建议,所以我可以使用the_post_thumnail等?

$ b $
$ b $ p
pre $
$ b

<?php $ args = array(
'post_type'=>'small_post',
'posts_per_page'=> 3
);
$ smallPosts = new WP_Query($ args);

$ args = array(
'post_type'=>'full_post',
'posts_per_page'=> 3
);
$ fullPosts = new WP_Query($ args); ($ smallPosts-> post_count> $ i)


$ b $($ i = 0; $ i <3; $ i ++)

echo $ smallPosts-> posts [$ i] - > post_title;
echo'< br />';
echo $ smallPosts-> posts [$ i] - > post_content;
echo'< br />';
$ b $ if($ fullPosts-> post_count> $ i)
echo $ fullPosts-> posts [$ i] - > post_title;
echo'< br />';
echo $ fullPosts-> posts [$ i] - > post_content;
echo'< br />';
}

?>


解决方案

这是我的解决方案,发布的时间可以交替,我可以使用the_thumbnail();和我需要的其他功能。此外,我使用if语句来添加类,因为不同的帖子类型需要以不同的样式进行设置。

 < ul> 


$ b $ args = array(
'posts_per_page'=> 10,
'post_type'=>(array('small_post', 'full_post')),
);
query_posts($ args); ?>

<?php if(have_posts()):?>

<?php while(have_posts()):the_post(); ?>
<?php if(get_post_type(get_the_ID())=='small_post'){?>
< li class =article small-poststyle =>
<?php if(has_post_thumbnail()):?>
< a href =<?php the_permalink();?>>
<?php the_post_thumbnail(''); ?>
< / a>
<?php endif;?>
< a href =<?php the_permalink();?>>
< h3>
<?php the_title(); ?>
< / h3>
< / a>
< p><?php the_excerpt(); ?>< / p为H.
< / li>
<?php}?>

<?php if(get_post_type(get_the_ID())=='full_post'){?>
< li class =article full-poststyle =>
<?php if(has_post_thumbnail()):?>
< a href =<?php the_permalink();?>>
<?php the_post_thumbnail(''); ?>
< / a>
<?php endif;?>
< a href =<?php the_permalink();?>>
< h3>
<?php the_title(); ?>
< / h3>
< / a>
< p><?php the_excerpt(); ?>< / p为H.
< / li>
<?php}?>
<?php endwhile; ?>


<?php wp_reset_postdata(); ?>

<?php else:?>
< p><?php _e('抱歉,没有帖子符合您的标准。 ?>< / p为H.
<?php endif; ?>

< / ul>


I've 2 post types and I want to output them alternately but without using many loops so I found this solution which does that.

However, it is not ideal as I need to output the_post_thumbnail which I find I am unable to do using this method (echo $smallPosts->posts[$i]->post_thumbnail; does nothing). Additonally I've read post_content is not the same as the_content(); - with the latter what I want to use.

Any suggestions on how I can loop through the alternating post types and have more control over the output so I can use the_post_thumnail etc.?

Below is my code that does work but just doesn't quite do what I require.

    <?php $args = array(
        'post_type' => 'small_post',
        'posts_per_page' => 3
      );
    $smallPosts = new WP_Query($args);

    $args = array(
        'post_type' => 'full_post',
        'posts_per_page' => 3
      );
    $fullPosts = new WP_Query($args);



     for ($i = 0; $i < 3; $i++) {
        if ($smallPosts->post_count > $i)

            echo $smallPosts->posts[$i]->post_title;
            echo '<br />';
            echo $smallPosts->posts[$i]->post_content;
            echo '<br />';

        if ($fullPosts->post_count > $i) 
            echo $fullPosts->posts[$i]->post_title;
            echo '<br />';
            echo $fullPosts->posts[$i]->post_content;
            echo '<br />';
       }    

    ?>

解决方案

This is my solution which outputs both post types and using the time published they can be alternated and I can use the_thumbnail( ); and other functions I need. Additionally I've used if statements to add classes as the different post types need to be styled differently.

                 <ul>                       
                    <?php

                        $args = array( 
                            'posts_per_page' => 10, 
                            'post_type' => (array('small_post','full_post')),
                            );
                        query_posts($args); ?>

                        <?php if ( have_posts() ) : ?>

                        <?php while ( have_posts() ) : the_post();  ?>
                            <?php if ( get_post_type( get_the_ID() ) == 'small_post' ) { ?>
                            <li class="article small-post" style="">
                                <?php if(has_post_thumbnail()) :?>
                                    <a href="<?php the_permalink(); ?>">
                                        <?php the_post_thumbnail(''); ?>
                                    </a>
                                <?php endif;?>
                                <a href="<?php the_permalink(); ?>">
                                    <h3>
                                        <?php the_title(); ?>
                                    </h3>
                                </a>
                                <p><?php the_excerpt(); ?></p> 
                            </li>
                        <?php } ?>      

                        <?php if ( get_post_type( get_the_ID() ) == 'full_post' ) { ?>
                            <li class="article full-post" style="">
                                <?php if(has_post_thumbnail()) :?>
                                    <a href="<?php the_permalink(); ?>">
                                        <?php the_post_thumbnail(''); ?>
                                    </a>
                                <?php endif;?>
                                <a href="<?php the_permalink(); ?>">
                                    <h3>
                                        <?php the_title(); ?>
                                    </h3>
                                </a>
                                <p><?php the_excerpt(); ?></p> 
                            </li>
                        <?php } ?>                      
                        <?php endwhile; ?>


            <?php wp_reset_postdata(); ?>

            <?php else : ?>
            <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
            <?php endif; ?>

           </ul>

这篇关于WordPress的:循环交替的帖子类型和输出特色的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆