簡體   English   中英

Wordpress Understrap在頁面上顯示帖子

[英]Wordpress Understrap display posts on a page

我正在學習如何使用wordpress API。 我是該框架的新手,因此,我決定安裝Understrap以使用Bootstrap 4框架並創建一個簡單的產品組合網站。 經過一番谷歌搜索之后,我開始嘗試該代碼,但是這個wordpress主題的許多方面對我來說還不清楚。 我想在頁面上顯示一些帖子,並使用bootstrap類標記來設置它們的顯示樣式。 是否有任何有效的教程,或者有人可以向我建議我需要對模板主題文件進行的正確修改?

我試圖用此代碼創建一個名為postpage.php的頁面,但是它不會被wordpress識別為頁面的模板模型。 碼:

<?php

$args = array(
'posts_per_page' => 6,
'offset' => 0,
'category' => 'portfolio',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '', 'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post', 'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);

$myposts = get_posts( $args );

foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php
endforeach;
wp_reset_postdata();
?>

首先,您需要通過將以下代碼添加到文件頂部來指定這是頁面模板:

<?php /* Template Name: Example Template */ ?>

然后它將顯示在您的頁面模板下拉列表中。 有關頁面模板的更多信息,請參見

為了添加Boostrap類,您需要在Bootstrap容器中包裝foreach語句,然后將ul更改為bootstrap列:

 <div class="container"> <div class="row"> <?php foreach ( $myposts as $post ) : setup_postdata($post ); ?> <div class="col-sm-4"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </div> <?php endforeach; wp_reset_postdata(); ?> </div> </div> 

如果要使用自定義布局,則需要制作一個自定義模板,然后在其中添加一個頁面以使用您的自定義模板。 您的自定義模板代碼將如下所示

 <?php /* Template Name: Your custom templete */ get_header(); ?><?php $the_query = new WP_Query(array( 'category_name' => 'popular', 'posts_per_page' => '6', 'order' => 'DESC', // Show only the published posts ));?> <?php if( $the_query->have_posts() ): ?> <?php while( $the_query->have_posts() ) : $the_query->the_post();?> <div class="story-info"> <a class="category-name arts texunset" href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"> <span class="daycolor" style="background:<?php the_field('colorpost'); ?>;">&nbsp;</span> <span> <?php the_title(); ?> </span> </a> <div class="date"> <?php the_time('F jS, Y') ?> &nbsp;|&nbsp; <i class="fa fa-signal"></i> </div> </div> <hr> <?php endwhile; ?> <?php endif; ?> <?php get_footer();?> 

暫無
暫無

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

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