php - Wordpress 查询帖子将每个项目包装在一个 div 而不是一个 li 中,并显示当前页面的后代

标签 php wordpress loops

我正在使用以下内容来包装帖子列表,因为我想在 div 中显示它们。

尽管使用

    global $post;
    $currentPage = $post->ID;

    'post_parent'      => $currentPage,

这在其他地方运行良好。我无法让页面只显示此页面的子代和孙代。

<?php
    global $post;
    $currentPage = $post->ID;
    // Get posts (tweak args as needed)
    $args = array(
        'post_parent'      => $currentPage,
        'post_type'        => 'page',
        'orderby'          => 'menu_order',
        'order'            => 'ASC'
    );
    $posts = get_pages( $args );
?>

<?php foreach (array_chunk($posts, 1, true) as $posts) :  ?>
    <div class="column small-4 medium-4 large-4">
        <?php foreach( $posts as $post ) : setup_postdata($post); ?>
            <a href="<?php the_permalink(); ?>">
                <?php the_title(); ?>
            </a>
        <?php endforeach; ?>
    </div>
<?php endforeach; ?>

我在自定义模板中使用代码。

我也试过

                <?php
                global $post;
                $currentPage = $post->ID;

                $args=array(
                  'child_of' => $currentPage,
                  'post_type' => 'page'
                );
                $my_query = null;
                $my_query = new WP_Query($args);
                echo $currentPage;
                if( $my_query->have_posts() ) {
                    while ($my_query->have_posts()) : $my_query->the_post(); ?>
                        <?php $img = wp_get_attachment_image_src( get_post_meta($post->ID, 'image_or_video', true)); ?>
                        <?php $alt_text_for_logo = get_post_meta($post->ID, 'article_name', true); ?>
                        <?php $short_description = get_post_meta($post->ID, 'article_short_description', true); ?>
                        <div class="column small-12 medium-6 large-4 box">
                            <div>
                                <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" itemprop="url">
                                    <?php if( $img ): ?>
                                        <img src="<?php echo $img[0]; ?>" alt="<?php echo $alt_text_for_logo; ?>" />
                                    <?php endif; ?>
                                    <span><?php the_title(); ?></span>
                                </a>
                            </div>
                        </div>
                   <?php endwhile; } ?>
                <?php wp_reset_query();?>

但这列出了我想要的页面,后面似乎是来自网站根目录的其他 10 个随机页面,尽管它没有列出该网站的每个页面。

最佳答案

最初将此解决方案作为评论发布,因为我不确定这是唯一必要的更改。原来是这样,所以这是解决方案:

<?php
    global $post;
    $currentPage = $post->ID;
    // Get posts (tweak args as needed)
    $args = array(
        'child_of'         => $currentPage,
        'post_type'        => 'page',
        'orderby'          => 'menu_order',
        'order'            => 'ASC'
    );
    $posts = get_pages( $args );
?>

<?php foreach (array_chunk($posts, 1, true) as $posts) :  ?>
    <div class="column small-4 medium-4 large-4">
        <?php foreach( $posts as $post ) : setup_postdata($post); ?>
            <a href="<?php the_permalink(); ?>">
                <?php the_title(); ?>
            </a>
        <?php endforeach; ?>
    </div>
<?php endforeach; ?>

get_pages 函数似乎没有将 post_parent 作为有效参数。所以你需要使用 child_of 来代替。

Reference to Codex

关于php - Wordpress 查询帖子将每个项目包装在一个 div 而不是一个 li 中,并显示当前页面的后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39894020/

相关文章:

php - 如何轻松存储用户帐户详细信息?

php - PHP 是否在函数结束后立即释放局部变量?

php - 当 SSH 进入但不是通过 cron 时,运行 PHP 文件可以正常工作

jquery - 为什么页面元素、动画、视差加载如此不稳定?

html - 选择图像链接

java - 我的密码程序的 while 循环和字符串比较

php - 非法字符串偏移 codeigniter

javascript - 如果没有为该帖子设置特色图像,想要更改帖子 <h1> 标题的颜色

javascript - 为什么 jquery 的 .each() 回调将 indexInArray 而不是 valueOfElement 作为第一个参数?

javascript - 回调函数中的参数