Skip to content

Instantly share code, notes, and snippets.

@ramiabraham
Created August 17, 2016 01:55
Show Gist options
  • Save ramiabraham/786cb8e9162196e6436fb2ea11eb73ac to your computer and use it in GitHub Desktop.
Save ramiabraham/786cb8e9162196e6436fb2ea11eb73ac to your computer and use it in GitHub Desktop.
query
<?php
/*
Plugin Name: Orange Cats 4U
Plugin URI: https://catfacts.com
Description: Orange cats. For you!
Version: 1.0
Author: Helena Bonham Carter
Author URI: https://dennys.com
Copyright: ok
Text Domain: orange-cats
Domain Path: /languages
*/
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
// Register Custom Post Type
// Register Custom Post Type
function oc_cpt() {
$labels = array(
'name' => _x( 'Orange cats', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Orange cat', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Orange cats', 'text_domain' ),
'name_admin_bar' => __( 'Orange cats', 'text_domain' ),
'archives' => __( 'Orange cat Archives', 'text_domain' ),
'parent_item_colon' => __( 'Parent Orange cat:', 'text_domain' ),
'all_items' => __( 'All Orange cats', 'text_domain' ),
'add_new_item' => __( 'Add New Orange cat', 'text_domain' ),
'add_new' => __( 'Add New Orange cat', 'text_domain' ),
'new_item' => __( 'New Orange cat', 'text_domain' ),
'edit_item' => __( 'Edit cat', 'text_domain' ),
'update_item' => __( 'Update Orange cat', 'text_domain' ),
'view_item' => __( 'View Orange cat', 'text_domain' ),
'search_items' => __( 'Search Orange cats', 'text_domain' ),
'not_found' => __( 'Not found meow', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash meow', 'text_domain' ),
'featured_image' => __( 'Featured Image meow', 'text_domain' ),
'set_featured_image' => __( 'Set featured image meow', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into Orange cat', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this Orange cat', 'text_domain' ),
'items_list' => __( 'Orange cats list', 'text_domain' ),
'items_list_navigation' => __( 'Orange cats list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter Orange cat list', 'text_domain' ),
);
$args = array(
'label' => __( 'Orange cat', 'text_domain' ),
'description' => __( 'Orange cats', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-smiley',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'orange_cats', $args );
}
add_action( 'init', 'oc_cpt', 0 );
// Shortcode to show stuff as an example.
// Copy the queries to where they're needed if shortcode is not desired.
function oc_show_cat_taxes() {
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
'post_type' => 'orange_cats'
) );
echo 'Shows all taxonomies for the given post type:';
echo '<br />';
foreach ( $categories as $category ) {
printf( '<a href="%1$s">%2$s</a><br />',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
}
add_shortcode( 'oc_shortcode', 'oc_show_cat_taxes' );
function oc_show_cats() {
// WP_Query arguments
$args = array (
'post_type' => array( 'orange_cats' ),
'post_status' => array( 'publish' ),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<a href="' . get_permalink() . '"">' . get_the_title() . '</a>';
echo get_the_post_thumbnail();
}
} else {
// nope
echo 'no cats omggg';
}
// Restore original Post Data
wp_reset_postdata();
}
add_shortcode( 'oc_shortcode_cats', 'oc_show_cats' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment