Skip to content

Instantly share code, notes, and snippets.

@gicolek
Created October 31, 2012 23:00
Show Gist options
  • Save gicolek/3990512 to your computer and use it in GitHub Desktop.
Save gicolek/3990512 to your computer and use it in GitHub Desktop.
Custom Post Type Featured Images
// w functions.php
// upewniamy się, że szablon pozwala na featured image
add_theme_support( 'post-thumbnails' );
// dodajemy automatycznie cięty obrazek o rozmiaracah 209 na 203 o nazwie thumb-course
add_image_size( 'thumb-course', 209, 203, true );
// dodajemy posty podczas jednej z pierwszych akcji WordPressa
add_action( 'init', 'raf_cpt_init' );
/**
* Adds Courses Post Type
*
* @hook init
*/
function raf_cpt_init() {
$courses_labels = array(
'name' => _x( 'Courses', 'post type general name' ),
'singular_name' => _x( 'Course', 'post type singular name' ),
'all_items' => __( 'All Courses' ),
'add_new' => _x( 'Add new Course', 'Courses' ),
'add_new_item' => __( 'Add new Course' ),
'edit_item' => __( 'Edit Course' ),
'new_item' => __( 'New Course' ),
'view_item' => __( 'View Course' ),
'search_items' => __( 'Search in Courses' ),
'not_found' => __( 'No Courses found' ),
'not_found_in_trash' => __( 'No Courses found in trash' ),
'parent_item_colon' => ''
);
$args = array(
'labels' => $courses_labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'author', 'custom-fields', 'thumbnail', 'excerpt' ),
// thumbnail pozwala na ustawienie featured image
'has_archive' => 'courses',
'rewrite' => array(
'slug' => 'course',
),
);
// rejestruje post o nazwie raf_course i podanej tablic argumentów
register_post_type( 'raf_course', $args );
}
// w pliku który wypisuje pętlę, np. front-page.php
<?php
$args = array(
'post_type' => 'raf_course',
'posts_per_page' => -1,
);
$course_query = new WP_Query( $args );
?>
<?php if ( $course_query->have_posts() ): ?>
<?php while ( $course_query->have_posts() ) : $course_query->the_post(); ?>
// wypisz coś
<?php endwhile; ?>
<?php endif; ?>
// poniższe musi być użyte przy własnym obiekcie klasy WP_Query
<?php wp_reset_postdata(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment