简体   繁体   中英

can't view custom post type posts in WP home page

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.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM