0

In my site, I created a custom post type called: "Pictures"

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'Pictures',
    array(
      'labels' => array(
        'name' => __( 'Pictures' ),
        'singular_name' => __( 'Pictures' )
      ),
      'public' => true,
      'has_archive' => true,
    )
  );
}

In my site, I want to have two pages: Index and Pictures

->Index: It had posts only of text ->Pictures: It had posts only of pictures

In Dashboard -> Posts. All posts that I did go straight to the index.

I wonder how I can access the Dashboard-> Pictures-> Add New . And each post I do, fall into the "Pictures" page.

How I can do this?

  • you already have that with your post type archive, but note that the first argument of register post type must be pictures, no capital letters or spaces can be in the post type slug. – Milo May 17 '16 at 3:55
  • I use a plugin called custom post type UI to create cpt, honestly saves me a lot of time & effort wordpress.org/plugins/custom-post-type-ui – decoder88 Mar 9 at 9:15
0

You need to add:

'supports'  =>  array('title', 'editor','page-attributes','thumbnail')

add this to your array after 'singular name' => 'pictures'.

The thumbnail adds the featured image option to your post type.

You can access the post type archive: yoursite/archive-pictures, this will use your themes archive.php file if it exists.

Or you can create a custom post type archive template with a custom loop: archive-pictures.php.

Or you can create a custom page template and apply it to a page.

Both the post-type archive and custom page template would contain similar code: a custom post type loop like this:

$args = array( 'post_type' => 'pictures', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
  the_title();
  echo '<div class="entry-content">';
  the_content();
  the_post_thumbnail("medium");
  echo '</div>';
endwhile;
  • Thanks so much. With this code and some changes, I got what I wanted! – Zkk May 18 '16 at 3:03
1

Hello maybe this can help, i use this kind of query to retrieve cpt query and display contents:

   <?php // querying Database
        $args = array( 'post_type' => 'Pictures', 'posts_per_page' => 6);
        $the_query = new WP_Query( $args ); 
    ?>
    <?php // creating the loog
     if ( $the_query->have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post();
    ?>

- output goes here, like the_content() or what you need -  

  <?php wp_reset_postdata(); ?>
    <?php endwhile; ?>
    <?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' );  ?></p>
    <?php endif; ?>

more... your CPT register name must be all lowercase, and at the end of your plugin file you have to add something like this to be able to query your cpt

// Querying custom post type
add_action( 'pre_get_posts', 'add_pictures_post_types_to_query' );

function add_pictures_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'pictures' ) );
    return $query;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.