首页 文章

在其他模板/页面上显示单个帖子

提问于
浏览
2

在网站上工作,我需要有一个单独的模板/页面来显示这一个帖子 .

在主页(索引)上,我遍历我的类别并显示其中的一部分 - 事件 - 新闻 - 参考 .

像这样:

<?php
            $query = new WP_Query( array( 'category_name' => 'Events' ) );

            if ($query->have_posts())
            {
                while ($query->have_posts()) : $query->the_post();
                        $haveEvents = true;
                        if ($eventCount < 3) {
                            $eventCount++;
                            ?>
                            <div class="event-tile">
                                <div class="event-tile-content">
                                    <h3><?php the_title(); ?></h3>
                                    <h4><?php the_time('F jS, Y'); ?></h4>
                                    <a href="<?php the_permalink(); ?>" class="event-tile-read-more">Lees meer</a>
                                </div>
                            </div>
                            <?php
                    }
                endwhile;
            }

每个部分都有一个读取所有和读取特定项目按钮:
enter image description here

enter image description here

我已经找到了如何在名为:page-posts.php的单独页面上显示类别中的所有帖子

在页面posts.php我只是检查我在哪个页面,并根据哪个页面,进行查询以显示指定类别中的帖子

$query;
        if (is_page('events')) {
            $query = new WP_Query( array( 'category_name' => 'Events' ) );
        }
        elseif (is_page('news')) {
            $query = new WP_Query( array( 'category_name' => 'News' ) );
        }

        if ($query->have_posts())
        {
etc..

现在我无法弄清楚(甚至在谷歌上花了几个小时之后),如何在主页上显示主页上的单个帖子(index.php)......

在我使用的动作按钮上:

<a href="<?php the_permalink(); ?>" class="event-tile-read-more">Lees meer</a>

这让我回到主屏幕 . (刚开始使用Wordpress)

2 回答

  • 1

    在Wordpress主题中, single.php 用于显示帖子 . 如果它不存在则下拉到使用 index.php

    尝试将 page-posts.php 移动到 single.php

  • 0

    也许最好使用archive-events.php . 您可以为自定义帖子进行存档 .

    您可以这样做:事件是您的自定义后期类型 . 然后登陆事件存档页面 .

    据我所知,你的问题是帖子是全球帖子(来自你家) . 所以也许你可以setup_postdata($ post),或者使用home_url('url')函数 .

相关问题