secret
anonymous / .htaccess
Created

Latest News Widget Setup

  • Download Gist
.htaccess
ApacheConf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
 
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
 
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
 
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
 
redirect 301 /admin /wp-admin
latest-news.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
<?php
/*
Plugin Name: Latest News
Plugin URI: #
Description: Adds the Latest News and Widget to display the number of Latest News items
Author: #
Version: 1.1
Author URI: #
*/
 
class LatestNews extends WP_Widget
{
// Constructor //
function LatestNews()
{
// Widget Settings
$widget_ops = array(
'classname' => 'latest-news',
'description' => 'Displays the number of Latest News items from the Latest News Custom Post'
);
 
// Create the widget
$this->WP_Widget( 'LatestNews', 'Latest News', $widget_ops );
}
 
function widget( $args, $instance )
{
// outputs the content of the widget
global $wpdb;
extract( $args );
$title = apply_filters( 'widget_title', empty( $instance[ 'title' ] ) ? __( '' ) : $instance[ 'title' ], $instance, $this->id_base );
$include_date = $instance[ 'includedate' ] ? '1' : '0';
$news_item_title = $instance[ 'newsitemtitle' ] ? '1' : '0';
 
if ( !$number = absint( $instance[ 'number' ] ) ) {
$number = 3;
}
 
echo $before_widget;
if ( $title ) : ?>
<h3 class="latest-news-heading"><?php echo $title; ?></h3>
<?php endif; ?>
 
<?php
$args = array(
'post_type' => 'latest-news',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => $number,
'order' => 'DESC',
'orderby' => 'date'
);
 
$loops = new WP_Query( $args );
if ( $loops->have_posts() ) : ?>
 
<ul id="latest-news">
 
<?php while ( $loops->have_posts() ) :
 
$loops->the_post();
$news_content = get_the_content();
$news_content = strip_tags( $news_content );
$news_content = ShortenText( $news_content, 80, '&hellip;' );
?>
 
<li class="lastest-news-item">
<?php if( $news_item_title ) : ?>
<h3 class="latest-news-title"><?php the_title() ?></h3>
<?php endif; ?>
<?php if ( $include_date ) : ?>
<p class="latest-news-date"><span><?php the_time( 'j M Y' ) ?></span></p>
<?php endif; ?>
<div class="latest-news-content">
<p><?php echo $news_content; ?></p>
<p class="read-more"><a href="<?php echo get_permalink(); ?>"title="<?php echo htmlentities( get_the_title() ); ?>">Read more</a></p>
</div>
</li>
 
<?php endwhile; ?>
 
</ul>
<?php if( function_exists( 'wp_paginate' ) ) {
wp_paginate();
} ?>
<?php endif; ?>
<?php
wp_reset_postdata();
echo $after_widget;
 
}
function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance[ 'number' ] = absint( $new_instance[ 'number' ] );
$instance[ 'includedate' ] = !empty ( $new_instance[ 'includedate' ] ) ? 1 : 0;
$instance[ 'newsitemtitle' ] = !empty ( $new_instance[ 'newsitemtitle' ] ) ? 1 : 0;
return $instance;
}
 
function form( $instance )
{
$defaults = array(
'title' => '',
'number' => 3,
'includedate' => true,
'newsitemtitle' => true
);
$instance = wp_parse_args( ( array ) $instance, $defaults );
$title = isset( $instance[ 'title' ] ) ? esc_attr( $instance[ 'title' ] ) : '';
$number = isset( $instance[ 'number' ] ) ? absint( $instance[ 'number' ] ) : 3;
$include_date = isset( $instance[ 'includedate' ] ) ? ( bool ) $instance[ 'includedate' ] : true;
$news_item_title = isset( $instance[ 'newsitemtitle' ] ) ? ( bool ) $instance[ 'newsitemtitle' ] : true;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
 
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of news items to show:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'newstitle' ); ?>" name="<?php echo $this->get_field_name( 'newsitemtitle' ); ?>"<?php checked( $news_item_title ); ?> />
<label for="<?php echo $this->get_field_id( 'newsitemtitle' ); ?>"><?php _e( 'Show News Item Title' ); ?></label></p>
<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'includedate' ); ?>" name="<?php echo $this->get_field_name( 'includedate' ); ?>"<?php checked( $include_date ); ?> />
<label for="<?php echo $this->get_field_id( 'includedate' ); ?>"><?php _e( 'Show Date' ); ?></label></p>
 
<?php
}
}
 
function create_latest_news()
{
$labels = array(
'name' => _x( 'Latest News', 'post type general name' ),
'singular_name' => _x( 'News List', 'post type singular name' ),
'add_new' => _x( 'Add New', 'News' ),
'add_new_item' => __( 'Add New News' ),
'edit_item' => __( 'Edit News' ),
'new_item' => __( 'New News' ),
'view_item' => __( 'View News' ),
'search_items' => __( 'Search News' ),
'not_found' => __( 'Nothing found' ),
'not_found_in_trash' => __( 'Nothing found in Trash' )
);
 
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => 'true', // Activate the archive
'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
'taxonomies' => array( 'category' ) // this is IMPORTANT
);
register_post_type( 'latest-news', $args );
}
 
// End class soup_widget
add_action( 'init', 'create_latest_news' );
add_action( 'widgets_init', create_function( '', 'return register_widget( "LatestNews" );' ) );
 
?>
page-news.php
PHP
1 2 3 4 5 6 7 8 9
<?php get_header(); ?>
<div id="sub-page" class="news main-sub-page">
<?php if( have_posts() ) : while( have_posts() ) : ?>
<?php the_post(); ?>
<h1 class="page-title"><?php the_title(); ?></h1>
<?php endwhile; endif; ?>
<?php dynamic_sidebar( 'first-widget-area' ); ?>
</div>
<?php get_footer(); ?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.