为了账号安全,请及时绑定邮箱和手机立即绑定

Wordpress 侧边栏上的子列表中的缩略图图像

Wordpress 侧边栏上的子列表中的缩略图图像

PHP
守候你守候我 2021-08-28 14:39:13
我正在尝试在 WordPress 侧边栏列表的子列表中获取缩略图。我不知道如何集成缩略图的代码。请帮我。这是我的代码如下:function wpb_list_child_pages(){  global $post;  if(is_page() && $post->post_parent)    $childpages=wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->post_parent.'&echo=0');  else    $childpages=wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->ID.'&echo=0');  if($childpages)    $string='<ul class="list-unstyled">'.$childpages.'</ul>';  return $string;}add_shortcode('wpb_childpages', 'wpb_list_child_pages');
查看完整描述

2 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

您可以创建自定义查询,并且在该查询中您可以根据条件更改参数。在循环内,您可以the_post_thumbnail()用来显示帖子的特色图片。


function wpb_list_child_pages() {

    global $post;

    ob_start();


    $qargs = array(

        'posts_per_page' => 10,

        'post_type'      => 'page',

        'orderby'        => 'menu_order',

    );


    if ( is_page() && $post->post_parent ) {

        $qargs['post_parent'] = $post->post_parent;

    } else {

        $qargs['post_parent'] = $post->ID;

    }


    $the_query = new WP_Query( $qargs );

    ?>

    <?php if ( $the_query->have_posts() ) : ?>

        <ul class="list-unstyled">

        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

            <li>

                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

                <?php if ( has_post_thumbnail( ) ) : ?>

                    <?php the_post_thumbnail( 'post-thumbnail' ); ?>

                <?php endif; ?>

            </li>

        <?php endwhile; ?>

        <?php wp_reset_postdata(); ?>

        </ul>

    <?php endif; ?>

    <?php

    $output = ob_get_contents();

    ob_end_clean();

    return $output;

}


add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );


查看完整回答
反对 回复 2021-08-28
?
翻翻过去那场雪

TA贡献2065条经验 获得超14个赞

最简单的方法是使用函数。编写 WP_Query 以获取子列表中的缩略图图像。它将作为 wp_list_pages 工作。


function wpb_list_child_pages() {


global $post;


if ( is_page() && $post->post_parent )

$child_pages_query_args = array(

    'post_type'   => 'page',

    'post_parent' => $post->post_parent ,

    'orderby'     => 'menu_order'

);

else

$child_pages_query_args = array(

    'post_type'   => 'page',

    'post_parent' => $post->ID,

    'orderby'     => 'menu_order'

);


$child_pages = new WP_Query( $child_pages_query_args );


if ( $child_pages->have_posts() ) :

?>

<ul class="child_page_row">

<?php 

while ( $child_pages->have_posts() ) : $child_pages->the_post();

    ?>

    <li>

        <a href="<?php the_permalink(); ?>">

        <?php if(has_post_thumbnail()): ?>

            <div class="child_page_thumb">

                <?php the_post_thumbnail(array(240, 240)); ?>

            </div>

            <?php endif; ?>

                <div class="child_page_name">

                <?php the_title(); ?>

                </div>

        </a>

    </li>

<?php

endwhile; 

?>

</ul>

<?php    

endif;


wp_reset_postdata();

}


add_shortcode('wpb_childpages', 'wpb_list_child_pages');


查看完整回答
反对 回复 2021-08-28
  • 2 回答
  • 0 关注
  • 122 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信