[英]Wordpress Custom Page, posts not displaying
嗨,我正在嘗試創建一個自定義頁面(custom-page.php),其中包含一些博客文章,但是當我測試循環時,我看不到任何文章。 它僅返回我的自定義頁面模板的名稱作為h2。
我已經發表了兩篇文章,但沒有出現。
我有一個front-page.php,用戶首次訪問我的網站時會登陸該頁面,並且在閱讀選項卡下未更改任何設置。
我已經閱讀了Wordpress Codex,到目前為止找不到任何解決方案。
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE
BLOG INDEX PAGE</h1>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
請遵循此代碼。
$newsQuery = newWP_Query('post_type=post','post_status=publish');
if ( $newsQuery->have_posts() ) {
while ($newsQuery->have_posts()) {
$newsQuery->the_post();
echo get_the_title();
echo get_the_excerpt();
}
}
您的完整模板將是這樣。
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE
BLOG INDEX PAGE</h1>
<?php
$newsQuery = new WP_Query('post_type=post','post_status=publish');
if ( $newsQuery->have_posts() ) : while ( $newsQuery->have_posts() ) : $newsQuery->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
從wp admin創建一個名為“ Blog”的頁面,然后在名為page-blog.php的主題文件夾中創建一個文件,並在下面編寫以下代碼。
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1>BLOG INDEX PAGE</h1>
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'id',
'order' => 'desc'
);
$loop = new WP_Query($args);
if($loop->have_posts()) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
?>
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'offset' => 0
);
$the_query1 = new WP_Query( $args );
if (count($the_query1->posts)>0) {
while ( $the_query1->have_posts() ) : $the_query1->the_post();
get_template_part( 'loop-archive-template-location' );
endwhile;
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.