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

我正在尝试在 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');


守候你守候我
浏览 123回答 2
2回答

拉丁的传说

您可以创建自定义查询,并且在该查询中您可以根据条件更改参数。在循环内,您可以the_post_thumbnail()用来显示帖子的特色图片。function wpb_list_child_pages() {&nbsp; &nbsp; global $post;&nbsp; &nbsp; ob_start();&nbsp; &nbsp; $qargs = array(&nbsp; &nbsp; &nbsp; &nbsp; 'posts_per_page' => 10,&nbsp; &nbsp; &nbsp; &nbsp; 'post_type'&nbsp; &nbsp; &nbsp; => 'page',&nbsp; &nbsp; &nbsp; &nbsp; 'orderby'&nbsp; &nbsp; &nbsp; &nbsp; => 'menu_order',&nbsp; &nbsp; );&nbsp; &nbsp; if ( is_page() && $post->post_parent ) {&nbsp; &nbsp; &nbsp; &nbsp; $qargs['post_parent'] = $post->post_parent;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $qargs['post_parent'] = $post->ID;&nbsp; &nbsp; }&nbsp; &nbsp; $the_query = new WP_Query( $qargs );&nbsp; &nbsp; ?>&nbsp; &nbsp; <?php if ( $the_query->have_posts() ) : ?>&nbsp; &nbsp; &nbsp; &nbsp; <ul class="list-unstyled">&nbsp; &nbsp; &nbsp; &nbsp; <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if ( has_post_thumbnail( ) ) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php the_post_thumbnail( 'post-thumbnail' ); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; <?php endwhile; ?>&nbsp; &nbsp; &nbsp; &nbsp; <?php wp_reset_postdata(); ?>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; <?php&nbsp; &nbsp; $output = ob_get_contents();&nbsp; &nbsp; ob_end_clean();&nbsp; &nbsp; return $output;}add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );

翻翻过去那场雪

最简单的方法是使用函数。编写 WP_Query 以获取子列表中的缩略图图像。它将作为 wp_list_pages 工作。function wpb_list_child_pages() {global $post;if ( is_page() && $post->post_parent )$child_pages_query_args = array(&nbsp; &nbsp; 'post_type'&nbsp; &nbsp;=> 'page',&nbsp; &nbsp; 'post_parent' => $post->post_parent ,&nbsp; &nbsp; 'orderby'&nbsp; &nbsp; &nbsp;=> 'menu_order');else$child_pages_query_args = array(&nbsp; &nbsp; 'post_type'&nbsp; &nbsp;=> 'page',&nbsp; &nbsp; 'post_parent' => $post->ID,&nbsp; &nbsp; 'orderby'&nbsp; &nbsp; &nbsp;=> 'menu_order');$child_pages = new WP_Query( $child_pages_query_args );if ( $child_pages->have_posts() ) :?><ul class="child_page_row"><?php&nbsp;while ( $child_pages->have_posts() ) : $child_pages->the_post();&nbsp; &nbsp; ?>&nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; <a href="<?php the_permalink(); ?>">&nbsp; &nbsp; &nbsp; &nbsp; <?php if(has_post_thumbnail()): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="child_page_thumb">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php the_post_thumbnail(array(240, 240)); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="child_page_name">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php the_title(); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; </li><?phpendwhile;&nbsp;?></ul><?php&nbsp; &nbsp;&nbsp;endif;wp_reset_postdata();}add_shortcode('wpb_childpages', 'wpb_list_child_pages');
打开App,查看更多内容
随时随地看视频慕课网APP