【问题标题】:Blog posts won't open on my custom WordPress theme博客文章无法在我的自定义 WordPress 主题上打开
【发布时间】:2018-09-05 00:10:49
【问题描述】:

这可能是一个愚蠢的问题,但我就是想不通。我正在开发通过转换 html 网站创建的自定义 WordPress 主题。但是当我使用 WP_Query 时,它不会加载我的博客文章。是的,它会按循环显示博客文章列表,但是当我单击每个标题或文章的“阅读更多”链接时,它只会将地址栏中的 url 更改为文章的链接,并且然后简单地刷新希望页面。打不开,真的好累。这是我的 index.php 中的查询循环

<?php
    $categories = get_categories(); 

    foreach ( $categories as $category ) {


    //define args for query
    $args = array(
    'cat' => $category->term_id,
    'post_type' => 'post',
    'posts_per_page' => '30',
);
//The main query

$query = new WP_Query( $args );

if ( $query->have_posts() ) { ?>

    <section class="<?php echo $category->name; ?> listing">
        <h2>Latest in <?php echo $category->name; ?>:</h2>

        <?php while ( $query->have_posts() ) {
$query->the_post();
            ?>

            <article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
                <?php if ( has_post_thumbnail() ) { ?>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
                <?php } ?>

                <h3 class="entry-title">
                    <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                    </a>
                </h3>

                <?php the_excerpt(); ?>

            </article>

<?php } // end while ?>

    </section>

<?php } // end if

// Use reset to restore original query.
wp_reset_postdata();
    }
?>

                </div><!-- blog-main-content-->


                <?php get_sidebar(); ?>

                                <?php get_footer(); ?>

请问我该怎么办

【问题讨论】:

  • 您的主题中有single.php 文件可用作帖子的模板吗?您在哪个文件中有此代码?
  • 不,代码在我的 index.php 中。我还没有帖子的模板文件。我必须创建一个吗?

标签: wordpress


【解决方案1】:

根据您上面的评论,您的帖子页面缺少模板文件。在您当前的配置中,帖子循环将显示在您网站的所有页面上,因为您只有一个 index.php 文件。您应该创建一个single.php 模板来显示您的单个博客文章。

这是 WordPress 模板层次结构供参考:https://developer.wordpress.org/themes/basics/template-hierarchy/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 2015-10-05
    相关资源
    最近更新 更多