繁体   English   中英

WordPress自定义页面,帖子不显示

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM