php - 自定义帖子类型的存档页面

标签 php wordpress

我创建了一个名为 news 的自定义帖子类型并添加了某些新闻。现在我想在存档页面中显示它。我在函数文件中添加了 'has_archive' => 'true'。
我的存档页面代码是:

<?php
        $args = array('post_type'   => 'news',
                      'post_status' => 'publish');

        $news=wp_get_recent_posts($args);
    ?>
        <div class="container  mc_tb_p">
                <h1>NEWS/RELEASES</h1>
                    <?php
                        foreach ($news as $row) 
                            {
                             $id       = $row['ID'];
                             $ntitle   = $row['post_title'];
                             $ncontent = $row['post_content'];
                             $ncontent = strip_tags($ncontent);
                              if (strlen($ncontent) > 100) 
                                  {
                                    $stringCut = substr($ncontent, 0, 200).'... <a href="'.get_permalink($id).'">Read More</a>';
                                  }
                              else{
                                    $stringCut = $row['post_content'];
                                  }
                            $ndate     = $row['post_date'];
                            $trim      = new DateTime($ndate);
                            $trimdate      = $trim->format('d-m-Y');
                            // var_dump($trimdate);
                    ?>
                <div class="news_releases">
                    <a href="<?php echo get_permalink($id)?>"><h3><?php echo $ntitle?></h3></a>
                        <h5><i>Published On:&nbsp;<?php echo $trimdate?></i></h5>
                            <p><?php echo $stringCut;?></p>
                </div>
                        <?php
                        }
                        ?>

        </div>

现在,当我提供我的 url:https//sitename/news ......它打开了第二个新闻的单页而不是其他任何东西时,我已经尝试了一切,但似乎没有任何效果。请帮忙

最佳答案

你有两个选择,
1. 在当前事件的主题文件夹中创建一个名为“archive-{post_type}.php”的文件,并在该文件中使用以下代码,

<?php if ( have_posts() ) : 
            while ( have_posts() ) : the_post(); ?>    
    <!-- do stuff ... -->
    <?php   endwhile; 
        endif; ?>

引用:https://codex.wordpress.org/Post_Type_Templates

或者

2.创建自定义模板文件并使用以下代码,
<?php
    $loop = new WP_Query( array( 'post_type' => 'posttypename', 'paged' => $paged ) );
    if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="pindex">
                <?php if ( has_post_thumbnail() ) { ?>
                    <div class="pimage">
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                    </div>
                <?php } ?>
            </div>
        <?php endwhile;
        if (  $loop->max_num_pages > 1 ) : ?>
            <div id="nav-below" class="navigation">
                <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Previous', 'domain' ) ); ?></div>
                <div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">&rarr;</span>', 'domain' ) ); ?></div>
            </div>
        <?php endif;
    endif;
    wp_reset_postdata();
?>

*posttypename - ypu 帖子类型名称

关于php - 自定义帖子类型的存档页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44626650/

相关文章:

php - 从 URL Wordpress 中删除目录名称

wordpress - 反向代理 heroku 应用程序到 WordPress 博客

php - 处理不可避免的警告的正确方法

php - Where cell is not equal to.. MySQL查询(!=)

wordpress - CIDR 表示法中我的 IP 地址是什么?

php - 迁移数据,从 latin1 字符集到 UTF-8

php - 在 Mac 上使用 Xampp 设置 Wordpress

javascript - 从 PHP 调用 javascript 函数 - 引号问题

php - 根据其他 2 个单元格值的结果更新单元格值

PHP_Doctrine 2.0 _连接管理