duanba8173 2013-11-07 21:49
浏览 48
已采纳

Wordpress外循环按类别和时间排序

Need help with some Wordpress outside the blog(Im not a blogger..)

So I need to get post from my Wordpress blog. I have manage to get the post, but need to sort by category and by time. here is part of my code:

                          <div class="8u skel-cell-mainContent" id="newsfeed">

                            <!-- Main Content -->
                                <?php if ($_GET['id']) {
                                    $id = $_GET['id'];
                                    $post = get_post($id);
                                    echo "<section>";
                                    echo "<a href='javascript:history.back();'>&#8592;Tilbake</a>";
                                    echo "<header style='font-size: 30px;'>";
                                    echo "<h2>".$post->post_title."</h2>";
                                    echo "</header>";
                                    echo "<p>".$post->post_content;
                                    echo "</section>";

                                } else {
                                    while (have_posts()):

                                        the_post();
                                        echo "<section>";
                                        echo "<header style='font-size: 30px;'> ";
                                        echo the_title('<h2>', '</h2>');

                                        echo "</header>";
                                        the_excerpt('<h3>', '</h3>');
                                        $id = get_the_ID();
                                        echo "<p><a href='nyheter.php?id=".$id."#newsfeed'>Les mer...</a></p>";
                                        echo "</section>";

                                    endwhile;
                                } ?>
                        </div>
                        <div class="4u">

                            <!-- Right Sidebar -->
                            <section>
                                <header>
                                    <h2> <?php the_widget( WP_Widget_Archives, 'title=Arkiv' ); ?> </h2>
                                </header>
                            </section>
                            <section>
                                <header>
                                    <h2> <?php the_widget( 'WP_Widget_Categories', 'dropdown=1&count=1', 'title=Kategori' ); ?> </h2>
                                </header>
                            </section>



                        </div>
  • 写回答

1条回答 默认 最新

  • dongzhucha3999 2013-11-07 22:11
    关注

    I would start a new loop, as it's much easier to get post data using the functions made for the Loop ( the_content(), the_title(), etc.)

    Try this as a staring point:

    <?php
        $new_loop = new WP_Query( array(
        'post_type' => 'post',
            'orderby' => 'category'
        ) );
    ?>
    
    <?php if ( $new_loop->have_posts() ) : while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
    
    <section>
    <a href='javascript:history.back();'>&#8592;Tilbake</a>
    <header style='font-size: 30px;'>
    <h2><?php the_title();?></h2>
    </header>
    <p><?php the_content();?></p>
    </section>
    
    <?php endwhile; else: ?>
    <?php endif; ?>
    <?php wp_reset_query(); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3