【问题标题】:Wordpress Custom Page, posts not displayingWordpress 自定义页面,帖子未显示
【发布时间】:2016-08-26 06:41:01
【问题描述】:

您好,我正在尝试创建一个包含我的一些博客帖子的自定义页面 (custom-page.php),但是当我测试循环时,我没有看到任何帖子。它仅将我的自定义页面模板的名称作为 h2 返回。

我已经发布了两篇文章,但它们没有出现。

我有一个front-page.php,用户第一次访问我的网站时会登陆,我没有更改阅读选项卡下的任何设置。

我已经阅读了 Wordpress 法典,到目前为止找不到任何解决方案。

<?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();
?>

【问题讨论】:

  • 您需要在哪个页面下显示您的自定义模板。在登陆页面或博客页面中???
  • 也不是,这只是一个包含我的一些帖子的sn-p的页面
  • 希望您必须在管理面板中设置页面才能正确显示:)

标签: php wordpress loops


【解决方案1】:

请遵循此代码。

$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();
?>

【讨论】:

  • 这对我有用,谢谢!现在我需要找到那个文档!
【解决方案2】:

从 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();
?>

【讨论】:

    【解决方案3】:
            <?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;
    
    
                }
            ?>
    

    【讨论】:

      【解决方案4】:

      正如我们所提到的,WP_Query 是 WordPress 数据库使用的 PHP 类。这个特殊的类可以做几件事,但主要是用来从数据库中提取帖子。

      <?php
       
      // The Query
      $the_query = new WP_Query( $args );
       
      // The Loop
      if ( $the_query->have_posts() ) {
          echo '<ul>';
          while ( $the_query->have_posts() ) {
              $the_query->the_post();
              echo '<li>' . get_the_title() . '</li>';
          }
          echo '</ul>';
      } else {
          // no posts found
      }
      /* Restore original Post Data */
      // wp_reset_postdata();
      

      这里有两个您可能希望在其中使用 WP_Query 的主要场景。第一个是找出 WordPress 当前正在处理的请求类型。 $is_* 属性旨在保存此信息:使用条件标签在此处进行交互。这是插件编写者更常见的情况(第二种通常适用于主题编写者)。

      第二个是在循环期间。 WP_Query 为 The Loop 中的常见任务提供了许多功能。首先,调用 $wp_query->have_posts() 的 have_posts() 来查看是否有要显示的帖子。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-10
        • 1970-01-01
        • 1970-01-01
        • 2019-05-08
        • 1970-01-01
        • 2015-01-03
        • 1970-01-01
        相关资源
        最近更新 更多