dtv7174 2014-10-18 17:53
浏览 48

在同一站点(WP)中的两种不同情况下使用外部循环

In my theme I have a blog page and a homepage (without blog loop). Now I would like to add some blog posts on my homepage (like most recent).

I am using the blog page as a template for the homepage ( I copied and renamed the original), wich works good. But I can't make any changes to it without changing the original blog page (the loop is called in an external php).

I Have no idea were to start.

This is the piece on the homepage calling the blog loop:

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

        <div id="main">
            <div class="central-wrapper clearfix">
                <div id="center" class="fullwidth">

                    <div id="content">
                        <?php if($gallery_select && $gallery_position == 1) : ?>
                            <?php codeus_gallery($gallery_select, $gallery_size, $gallery_style);  ?>
                        <?php endif; ?>

                        <div class="inner">
                            <?php the_content(); ?>
                            <?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<div class="bloghome">
<?php codeus_blog_list(); ?>
</div>
                            <?php if($quickfinder_position == 2) : ?>
                                <?php codeus_quickfinder($quickfinder_select); ?>
                            <?php endif; ?>

                            <?php if($portfolio_position == 2) : ?>
                                <?php codeus_portfolio(implode(',',$portfolio_select), $portfolio_size, $portfolio_count, $portfolio_filter, $portfolio_title); ?>
                            <?php endif; ?>

                            <?php if($gallery_select && $gallery_position == 3) : ?>
                                <?php codeus_gallery($gallery_select, $gallery_size, $gallery_style); ?>
                            <?php endif; ?>

                        </div>
                    </div><!-- #content -->
                </div><!-- #center -->
            </div><!-- .central-wrapper -->
        </div><!-- #main -->

    <?php endif; ?>

</div><!-- wrap end -->

And this is the piece in the external php called blog.php

<?php if($blog_loop->have_posts()) : ?>
<div class="blog_list">
    <ul class="styled">
        <?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
            <li class="clearfix">
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(),      'codeus_post_list_image'); ?>
                    <div class="comment-info">
                        <a href="<?php echo get_permalink($post->ID); ?>" class="date-day"><?php echo get_the_date('d'); ?></a>
                        <div class="date-month"><?php echo get_the_date('M'); ?></div>
                    </div>
                    <div class="post-info">
                        <h3><a href="<?php echo get_permalink($post->ID); ?>"><?php the_title(); ?></a></h3>
                        <?php if($image_url[0]) : ?>
                            <div class="post-image">
                                <div class="image wrap-box shadow middle">
                                    <div class="shadow-left"></div><div class="shadow-right"> </div>
                                    <a href="<?php echo get_permalink($post->ID); ?>"><img src="   <?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" /></a>
                                </div>
                            </div>
                        <?php endif; ?>
                        <div class="text clearfix"><?php the_excerpt(); ?></div>
                        <?php codeus_author_info(get_the_ID()); ?>
                    </div>
                </div>
            </li>
            <?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
        <?php endwhile; ?>
        </ul>
        <?php codeus_pagination($page_num, $pages_count); ?>
    </div>
<?php endif; ?>

Remember, I don't want to make changes in the original blog.php, because the normal blog page needs to work as it is now. I would like to make the blog work on the homepage also, but just with a maximum of 3 posts showing, perhaps of a certain category.

Help would be so welcome!!

  • 写回答

1条回答 默认 最新

  • dqjo4340 2014-10-18 18:40
    关注

    I think I found a solution, I edited the homepage template like this:

         <!-- wrap start --><div class="content-wrap">
    
        <?php if(have_posts()) : the_post(); ?>
    
            <div id="main">
                <div class="central-wrapper clearfix">
                    <div id="center" class="fullwidth">
    
                        <div id="content">
    
    
                            <div class="inner">
                                <?php the_content(); ?>
                                <?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
    <div class="bloghome">                              
    <?php
    
    
    $page_num = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $items_per_page = 2;
    $blog_loop = new WP_Query(array(
        'post_type' => 'post',
        'posts_per_page' => $items_per_page,
        'paged' => $page_num
    ));
    $pages_count = ceil($blog_loop->found_posts/$items_per_page);
    global $post;
    $portfolio_posttemp = $post;
    ?>
    <?php if($blog_loop->have_posts()) : ?>
    <div class="blog_list">
        <ul class="styled">
            <?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
                <li class="clearfix">
                    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'codeus_post_list_image'); ?>
                        <div class="comment-info">
                            <a href="<?php echo get_permalink($post->ID); ?>" class="date-day"><?php echo get_the_date('d'); ?></a>
                            <div class="date-month"><?php echo get_the_date('M'); ?></div>
                        </div>
                        <div class="post-info">
                            <h3><a href="<?php echo get_permalink($post->ID); ?>"><?php the_title(); ?></a></h3>
                            <?php if($image_url[0]) : ?>
                                <div class="post-image">
                                    <div class="image wrap-box shadow middle">
                                        <div class="shadow-left"></div><div class="shadow-right"></div>
                                        <a href="<?php echo get_permalink($post->ID); ?>"><img src="<?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" /></a>
                                    </div>
                                </div>
                            <?php endif; ?>
                            <div class="text clearfix"><?php the_excerpt(); ?></div>
                            <?php codeus_author_info(get_the_ID()); ?>
                        </div>
                    </div>
                </li>
                <?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
            <?php endwhile; ?>
        </ul>
    
    </div>
    <?php endif; ?>
    </div>
    
    
    
                            </div>
                        </div><!-- #content -->
                    </div><!-- #center -->
                </div><!-- .central-wrapper -->
            </div><!-- #main -->
    
        <?php endif; ?>
    
    </div><!-- wrap end -->
    
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本