doupu9251 2016-12-25 09:24
浏览 15
已采纳

无法在WP主页中查看自定义帖子类型的帖子

I am trying to view custom post type posts in WordPress and stuck with the code below.

$args = array(
    'type'      => 'theslider',
    'orderby'   => 'date',
    'order'     => 'DESC',
    'cat'       => 23
);

$lasts = new WP_Query( $args );

if( $lasts->have_posts() ):

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

        the_title( sprintf('<a href="%s"><div class="pt10 title">', esc_url( get_permalink() ) ),'</div></a>' ); 

    endwhile;

endif;

wp_reset_postdata();

This code works with the default post type. And the Custom Post Type function works well in the admin panel.

How can I view custom post type posts on WordPress home page.

  • 写回答

1条回答 默认 最新

  • dqcuq4138 2016-12-25 09:50
    关注

    Got it. Posting here for future reference.

    <?php 
    $args = array(
        'post_type'=> 'theslider',
        'orderby'   => 'date',
        'order'     => 'DESC'
    );              
    
    $the_query = new WP_Query( $args );
    if($the_query->have_posts() ) : 
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
    
            the_title();
    
        endwhile; 
    
    else: 
    
        echo '<p>Nothing Here.</p>';
    
    endif; 
    
    wp_reset_postdata(); 
    
    ?>
    

    That's it. it will get and view the custom post type posts :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据