Skip to content

Instantly share code, notes, and snippets.

@pixelbart
Last active November 9, 2015 09:27
Show Gist options
  • Save pixelbart/137fffce582120640801 to your computer and use it in GitHub Desktop.
Save pixelbart/137fffce582120640801 to your computer and use it in GitHub Desktop.
Wordpress Plugin: products
<?php
/*
Plugin Name: Products
Description: Products for your back- and frontend.
Version: 1.0
Author: Your Name
Author URI: http://yoururl.com
*/
defined( 'ABSPATH' ) or die( 'Plugin file cannot be accessed directly.' );
/*******************************************************
Scripte and CSS
*******************************************************/
// Include your scripts for the frontend
function _scripts()
{
wp_enqueue_style( 'my-css', plugins_url('style.css',__FILE__), array(), 'screen' ); // css in the plugin folder
wp_enqueue_script('my-js', plugins_url('script.js',__FILE__), array('jquery'), '1.0', true); // js in in the plugin folder
}
add_action( 'wp_enqueue_scripts', '_scripts'); // add your script
/*******************************************************
Custom Post Type - Products
*******************************************************/
add_action( 'init', 'products_custom_post_type', 0 );
public function products_custom_post_type()
{
// Labels for the backend /wp-admin
$labels = array(
'name' => _x( 'Products', 'Post Type General Name', 'products' ),
'singular_name' => _x( 'Product', 'Post Type Singular Name', 'products' ),
'menu_name' => __( 'Products', 'products' ),
'all_items' => __( 'All Products', 'products' ),
'view_item' => __( 'Show Product', 'products' ),
'add_new_item' => __( 'Add Product', 'products' ),
'add_new' => __( 'Add', 'products' ),
'edit_item' => __( 'Edit', 'products' ),
'update_item' => __( 'Update', 'products' ),
'search_items' => __( 'Search Products', 'products' ),
'not_found' => __( 'No Products found.', 'products' ),
'not_found_in_trash' => __( 'No Products found in trash.', 'products' ),
);
// Options
$args = array(
'label' => __( 'Products', 'products' ),
'description' => __( 'Products for your portfolio', 'products' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'revisions'),
'taxonomies' => array('category'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
// Register post type products
register_post_type( 'products', $args );
}
/*******************************************************
ShortCode - Products
*******************************************************/
// add shortcode [products]
add_shortcode( 'products', 'products_shortcode' ) );
function products_shortcode( $atts )
{
$args = array( 'post_type' => 'products', 'posts_per_page' => 9 );
$the_query = new WP_Query( $args );
// You can change the HTML if and how you like
if ( $the_query->have_posts() ) : ?>
<div class="products">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="product">
<div class="product-image">
<?php the_post_thumbnail(); ?>
</div>
<div class="product-text">
<?php the_title(); the_content(); ?>
</div>
</div>
<?php wp_reset_postdata(); endwhile; ?>
</div>
<?php
else :
echo '<p>'._e( 'No products found.', 'products' ).'</p>';
endif;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment