Skip to content

Instantly share code, notes, and snippets.

@jpen365
jpen365 / setup_postdata-and-get_posts.php
Created November 26, 2016 16:09
Use setup_postdata() to use template tags when using get_posts() to query the database
<?php
$args = array(
'numberposts' = '4',
'category_name' = 'slider'
);
$slider_posts = get_posts( $args );
if ( $slider_posts ) {
foreach ( $slider_posts as $post ) :
@jpen365
jpen365 / new-WP_Query.php
Created November 26, 2016 16:08
A template for creating a new instance of the WP_Query class
<?php
$args = array(
// Define the arguments for the new query
);
$myQuery = new WP_Query( $args );
@jpen365
jpen365 / echo $args and content.php
Created November 16, 2016 04:02
Define widget output
<?php
echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
<div class="row">
<div class="col-lg-6"><?php
foreach( $cat_col_one as $cat_one ) {
echo $cat_one;
} ?>
</div>
@jpen365
jpen365 / foreach( $categories as $category).php
Created November 16, 2016 04:01
Iterate through all categories and break them into two columns
<?php
foreach( $categories as $category ) {
$cat_count ++;
$category_link = sprintf(
'<li class="list-unstyled"><a href="%1$s" alt="%2$s">%3$s</a></li>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
if ($cat_count % 2 != 0 ) {
@jpen365
jpen365 / widget() variables.php
Created November 16, 2016 04:00
Variables for the category list sidebar widget.
<?php
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$cat_count = 0;
$cat_col_one = [];
$cat_col_two = [];
?>
@jpen365
jpen365 / widget() function.php
Created November 16, 2016 03:59
Empty widget() method of WP_Widget class
<?php
function widget( $args, $instance ) {
// All widget output will go here
}
?>
@jpen365
jpen365 / add_action to widgets_init.php
Created November 16, 2016 03:43
Add the new widget using the widgets_init hook
<?php
function jpen_register_example_widget() {
register_widget( 'jpen_Example_Widget' );
}
add_action( 'widgets_init', 'jpen_register_example_widget' );
?>
@jpen365
jpen365 / WP_Widget::update().php
Created November 16, 2016 03:42
Update widget settings information with update()
<?php
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
return $instance;
}
?>
@jpen365
jpen365 / WP_Widget::form().php
Created November 16, 2016 03:37
Display widget setting fields in the admin area.
<?php
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : ''; ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
</p><?php
}
@jpen365
jpen365 / WP_Widget::widget().php
Created November 16, 2016 02:56
Define widget output with widget()
<?php
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$blog_title = get_bloginfo( 'name' );
$tagline = get_bloginfo( 'description' );
echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
<p><strong>Site Name:</strong> <?php echo $blog_title ?></p>