【问题标题】:Display (Wordpress) Child Pages with Title, Thumbnail and Excerpt显示 (Wordpress) 带有标题、缩略图和摘录的子页面
【发布时间】:2016-10-25 05:55:49
【问题描述】:

提前为一个问题道歉,对于专家来说,我相信这是相对明显的。我查看了 WordPress Codex 以获取适当的信息(the_post_thumbnail、the_excerpt 等),但我对 .php 的了解还不够,无法正确实现它。还在学习!

我正在尝试在标准 (WP) 页面中显示子页面,包括它们的标题、缩略图和摘录。除了 THUMBNAIL 和 EXCEPT 之外,我可以通过以下方式让一切正常工作:

    <div class="child-pages">
    <?php
        $pageChild = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC' ) );
            foreach( $pageChild as $page ) { 
    ?>
        <!-- loop: child page -->
        <div class="child">
            <header class="entry-header">
                <a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo '<h3>'.$page->post_title.'</h3>'; ?></a>
            </header><!-- .entry-header -->
            <img src="<?php echo the_post_thumbnail_url( $page->ID ); ?>">
            <?php echo $page->the_excerpt; ?>
        </div>
    <?php } ?>
    </div>

到目前为止,我可以看到正确子页面的链接/标题,并且顺序正确,但看不到缩略图或摘录。显然,我没有正确调用缩略图或摘录。有人可以纠正我吗?

我也试过这几行,由二十六主题支持:

            <a href="<?php echo get_permalink(); ?>"><?php twentysixteen_post_thumbnail(); ?></a>
            <?php the_excerpt(); ?>

任何帮助将不胜感激!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    缩略图不显示是因为函数the_post_thumbnail_url();显示的是当前帖子的缩略图,参数是指定图片的大小,不是帖子的大小。

    $post-&gt;the_excerpt 不一定要填写。如果您查看添加/编辑帖子屏幕,您会注意到两个文本字段,一个用于编辑帖子内容,一个用于摘录。摘录是可选的,因此函数the_excerpt() 显示该字段的内容,但当它为空时,它将显示$post-&gt;the_content 的前X 个字符。

    关于您的第二次尝试:the_excerpt()get_permalink()twentysixteen_post_thumbnail() 函数不起作用,因为您没有正确设置帖子。

    最简单的方法是将setup_postdata() 添加到您的代码中:

    $pageChild = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC' ) );
    foreach( $pageChild as $page ) { 
        setup_postdata( $page ); 
        // now you can use all the fancy functions like `the_excerpt()`
        // add your output here
    }
    

    或者您可以按照推荐的方式进行操作,使用 WP_Query 对象:

    <?php
    $args = array(
        'post_parent' => $post->ID,
        'orderby' => 'menu_order', 
        'order' => 'ASC' 
        );
    
    $the_query = new WP_Query( $args ); ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
        <!-- pagination here -->
    
        <!-- the loop -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <h2><?php the_title(); ?></h2>
        <?php endwhile; ?>
        <!-- end of the loop -->
    
        <!-- pagination here -->
    
        <?php wp_reset_postdata(); ?>
    
    <?php else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    

    【讨论】:

    • 谢谢杰拉德!非常感谢!我确实选择了“简单”的答案,添加了“setup_postdata($page);”行然后添加花哨的东西。我保证我确实尝试了 WP_Query 技术,但无法让它显示子页面 - 它只显示了我的示例博客文章。我只是刚刚了解 WP_Query 标签,今天第一次使用它来识别我的自定义帖子类型的存档页面并将它们显示为目录中的列表。
    猜你喜欢
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多