Skip to content

Instantly share code, notes, and snippets.

@captprice26
Last active October 26, 2016 06:31
Show Gist options
  • Save captprice26/98cf96bb31f3bd9245739d5818fee3cd to your computer and use it in GitHub Desktop.
Save captprice26/98cf96bb31f3bd9245739d5818fee3cd to your computer and use it in GitHub Desktop.
Press Release plugin with shortcode and parameters "[press_release]" and "[press_release_list]" add these inside the shortcode to limit the custom post type "posts=your number"
<?php
/*
Plugin Name: Press Release (Custom Post Type)
*/
function my_custom_posttypes() {
$labels = array(
'name' => 'Press Releases',
'singular_name' => 'Press Release',
'menu_name' => 'Press Release',
'name_admin_bar' => 'Press Releases',
'add_new' => 'Add New',
'add_new_item' => 'Add New Press Release',
'new_item' => 'New Press Release',
'edit_item' => 'Edit Press Release',
'view_item' => 'View Press Release',
'all_items' => 'All Press Releases',
'search_items' => 'Search Press Releases',
'parent_item_colon' => 'Parent Press Releases:',
'not_found' => 'No press releases found.',
'not_found_in_trash' => 'No press releases found in Trash.',
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-pressthis',
'query_var' => true,
'rewrite' => array( 'slug' => 'press_release' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' )
);
register_post_type( 'press_release', $args );
}
add_action( 'init', 'my_custom_posttypes' );
// Flush rewrite rules to add "review" as a permalink slug
function my_rewrite_flush() {
my_custom_posttypes();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'my_rewrite_flush' );
// Custom Taxonomies
function press_taxonomy() {
register_taxonomy(
'press_taxonomies', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'press_release', //post type name
array(
'hierarchical' => true,
'label' => 'Press Release Categories', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'press_release', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'press_taxonomy');
// Press Release Shortcode
function press_shortcode( $atts ){
ob_start();
$atts = shortcode_atts(
array(
'posts' => '-1',
),
$atts,
'press_release'
);
$pressloop = new WP_Query( array (
'posts_per_page' => $atts['posts'],
'post_type' => 'press_release'
) );
echo '<div id="press-releases">';
while ( $pressloop->have_posts() ) : $pressloop->the_post();
echo '<div class="press-content">';
echo '<div class="press-featured">';
the_post_thumbnail('medium');
echo '</div>';
echo '<h2 class="entry-title">' . get_the_title() . '</h2>';
echo '<div class="entry-content">';
the_excerpt();
echo '</div>';
echo '<a class="press-btn" target="_blank" href="' . get_permalink() . '">Read More</a>';
echo '</div>';
endwhile;
echo '</div>';
$out = ob_get_clean();
return $out;
}
add_shortcode( 'press_release', 'press_shortcode' );
// Press Release Shortcode List
function press_shortcode_list( $atts ){
ob_start();
$atts = shortcode_atts(
array(
'posts' => '-1',
),
$atts,
'press_release_list'
);
$pressloop = new WP_Query( array (
'posts_per_page' => $atts['posts'],
'post_type' => 'press_release'
) );
echo '<ul id="press-releases-list">';
while ( $pressloop->have_posts() ) : $pressloop->the_post();
echo '<li><a class="press-btn" target="_blank" href="' . $page_link . '">' . get_permalink() . '</a>';
echo '</li>';
endwhile;
echo '</ul>';
// Reset post data
wp_reset_postdata();
$out = ob_get_clean();
return $out;
}
add_shortcode( 'press_release_list', 'press_shortcode_list' );
@captprice26
Copy link
Author

For Wordpress only

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment