簡體   English   中英

如何在Wordpress中的新聞頁面上顯示帖子?

[英]How to display posts on a news page in Wordpress?

如果我進行自定義帖子,則可以輕松完成,但是這樣做會使原始帖子類型未使用。 我想使用它而不是構建一個新的自定義帖子類型。 我已經創建了page-news.php,但是它無法顯示在此頁面上。 也目前,帖子正在顯示www.somedomain.com/posts_title格式,我希望它是www.somedomain.com/news/posts_title

這是我的page.news.php

<?php 
/*
  Template Name: News
*/
?>
<?php get_header();?>
<div class="mainwrapper">
<!-- slide -->
</div>

<div class="content content-topone">
    <div class="container-fluid">
        <div class="row portfolio">
            <?php $loop = new WP_Query( array( 'post_type' => '', 'posts_per_page' => -1 ) ); ?>
            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="col-lg-3 col-md-4 col-sm-6 col-xs-12 portfoliobox">
                <div class="thumbnail">
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                    <?php 
                    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
                        the_post_thumbnail();
                    } else {
                        $img = '/img/responsive-webness.png"';
                       $attrib = ' class="' . 'img-responsive' . '" alt="' . ' '. '"/>';
                       $begin = '<img src="';
                       $getpath = get_template_directory_uri();
                        echo $begin . $getpath. $img . $attrib;
                      }
                    ?>
                    </a>

                    <p><?php the_title(); ?></p>

                    <p><?php the_content(); ?></p>
                </div><!-- .thumbnail -->
          </div>
          <?php endwhile; wp_reset_query(); ?>
          <div class="clear"></div>
       </div>
    </div>     
</div>
<?php get_footer();?>

更改此:

$loop = new WP_Query( array( 'post_type' => '', 'posts_per_page' => -1 ) );

對此:

$loop = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => -1 ) );

因此,您可以在新聞頁面中顯示本機posts 您需要做的只是循環

if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) {
        $loop->the_post();
        ?><h2><?php the_title(); ?></h2><?php
    }
}

要顯示任何自定義帖子類型,只需更改數組中的post_type 閱讀更多有關Codex的信息

要更改URI格式,請轉到WordPress儀表板,然后從菜單導航至Settings->Permalink ,然后選擇“ Custom Structure單選按鈕,然后在文本框中輸入/%postname%/%category%/ ,然后保存更改。 檢查更多。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM