View wp-posts in dropdown
The following snippet will allow you to view WP-Posts in dropdown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<form action="<? bloginfo('url'); ?>" method="get"> <select name="page_id" id="page_id"> <?php global $post; $args = array( 'numberposts' => -1); $posts = get_posts($args); foreach( $posts as $post ) : setup_postdata($post); ?> <option value="<? echo $post->ID; ?>"><?php the_title(); ?></option> <?php endforeach; ?> </select> <input type="submit" name="submit" value="view" /> </form> |
Source: Snipplr
Prev/Next wordpress loop links
Add the following snippet to previous/next loop links
1 |
<div class="navigation"><p><?php posts_nav_link(); ?></p></div> |
Source: WordPress Codex
Query a custom taxonomy
The following snippet will allow you to query a custom taxonomy, using the taxquery parameter
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 |
<?php // Example query to select all posts belonging to a custom taxonomy. $args = array( 'tax_query' => array( array( 'taxonomy' => 'TAXONOMY_NAME', 'field' => 'slug', 'terms' => 'TAXONOMY_SLUG' ) ) ); $the_query = new WP_Query( $args ); // The Loop while ( $the_query->have_posts() ) : $the_query->the_post(); echo '<li>'; the_title(); echo '</li>'; endwhile; // Reset Post Data wp_reset_postdata(); ?> |
Source: Snipplr
Customize Default Avatar
The following snippet will allow you to customize default avatar
1 2 3 4 5 6 7 8 9 10 11 |
function newgravatar ($avatar_defaults) { $myavatar = get_bloginfo('template_directory') . '/images/tweaker.jpg'; $avatar_defaults[$myavatar] = "Tweaker"; return $avatar_defaults; } add_filter( 'avatar_defaults', 'newgravatar' ); |
Current theme link for WordPress theme
The following snippet will link to the current theme/template url for use in WordPress template
1 |
<?php bloginfo('template_url'); ?> |
WordPress Theme Options Page
Using the snippet will creates a theme options page for your WordPress theme
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
<?php /* This should be saved as theme-options.php and * you'll need to use the following in your functions.php: * require_once ( get_stylesheet_directory() . '/theme-options.php' ); */ add_action( 'admin_init', 'theme_options_init' ); add_action( 'admin_menu', 'theme_options_add_page' ); /** * Init plugin options to white list our options */ function theme_options_init(){ register_setting( 'sample_options', 'sample_theme_options', 'theme_options_validate' ); } /** * Load up the menu page */ function theme_options_add_page() { add_theme_page( __( 'Theme Options', 'sampletheme' ), __( 'Theme Options', 'sampletheme' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' ); } /** * Create arrays for our select and radio options */ $select_options = array( '0' => array( 'value' => '0', 'label' => __( 'Zero', 'sampletheme' ) ), '1' => array( 'value' => '1', 'label' => __( 'One', 'sampletheme' ) ), '2' => array( 'value' => '2', 'label' => __( 'Two', 'sampletheme' ) ), '3' => array( 'value' => '3', 'label' => __( 'Three', 'sampletheme' ) ), '4' => array( 'value' => '4', 'label' => __( 'Four', 'sampletheme' ) ), '5' => array( 'value' => '3', 'label' => __( 'Five', 'sampletheme' ) ) ); $radio_options = array( 'yes' => array( 'value' => 'yes', 'label' => __( 'Yes', 'sampletheme' ) ), 'no' => array( 'value' => 'no', 'label' => __( 'No', 'sampletheme' ) ), 'maybe' => array( 'value' => 'maybe', 'label' => __( 'Maybe', 'sampletheme' ) ) ); /** * Create the options page */ function theme_options_do_page() { global $select_options, $radio_options; if ( ! isset( $_REQUEST['settings-updated'] ) ) $_REQUEST['settings-updated'] = false; ?> <div class="wrap"> <?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'sampletheme' ) . "</h2>"; ?> <?php if ( false !== $_REQUEST['settings-updated'] ) : ?> <div class="updated fade"><p><strong><?php _e( 'Options saved', 'sampletheme' ); ?></strong></p></div> <?php endif; ?> <form method="post" action="options.php"> <?php settings_fields( 'sample_options' ); ?> <?php $options = get_option( 'sample_theme_options' ); ?> <table class="form-table"> <?php /** * A sample checkbox option */ ?> <tr valign="top"><th scope="row"><?php _e( 'A checkbox', 'sampletheme' ); ?></th> <td> <input id="sample_theme_options[option1]" name="sample_theme_options[option1]" type="checkbox" value="1" <?php checked( '1', $options['option1'] ); ?> /> <label class="description" for="sample_theme_options[option1]"><?php _e( 'Sample checkbox', 'sampletheme' ); ?></label> </td> </tr> <?php /** * A sample text input option */ ?> <tr valign="top"><th scope="row"><?php _e( 'Some text', 'sampletheme' ); ?></th> <td> <input id="sample_theme_options[sometext]" type="text" name="sample_theme_options[sometext]" value="<?php esc_attr_e( $options['sometext'] ); ?>" /> <label class="description" for="sample_theme_options[sometext]"><?php _e( 'Sample text input', 'sampletheme' ); ?></label> </td> </tr> <?php /** * A sample select input option */ ?> <tr valign="top"><th scope="row"><?php _e( 'Select input', 'sampletheme' ); ?></th> <td> <select name="sample_theme_options[selectinput]"> <?php $selected = $options['selectinput']; $p = ''; $r = ''; foreach ( $select_options as $option ) { $label = $option['label']; if ( $selected == $option['value'] ) // Make default first in list $p = "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>"; else $r .= "\n\t<option style=\"padding-right: 10px;\" value='" . esc_attr( $option['value'] ) . "'>$label</option>"; } echo $p . $r; ?> </select> <label class="description" for="sample_theme_options[selectinput]"><?php _e( 'Sample select input', 'sampletheme' ); ?></label> </td> </tr> <?php /** * A sample of radio buttons */ ?> <tr valign="top"><th scope="row"><?php _e( 'Radio buttons', 'sampletheme' ); ?></th> <td> <fieldset><legend class="screen-reader-text"><span><?php _e( 'Radio buttons', 'sampletheme' ); ?></span></legend> <?php if ( ! isset( $checked ) ) $checked = ''; foreach ( $radio_options as $option ) { $radio_setting = $options['radioinput']; if ( '' != $radio_setting ) { if ( $options['radioinput'] == $option['value'] ) { $checked = "checked=\"checked\""; } else { $checked = ''; } } ?> <label class="description"><input type="radio" name="sample_theme_options[radioinput]" value="<?php esc_attr_e( $option['value'] ); ?>" <?php echo $checked; ?> /> <?php echo $option['label']; ?></label><br /> <?php } ?> </fieldset> </td> </tr> <?php /** * A sample textarea option */ ?> <tr valign="top"><th scope="row"><?php _e( 'A textbox', 'sampletheme' ); ?></th> <td> <textarea id="sample_theme_options[sometextarea]" cols="50" rows="10" name="sample_theme_options[sometextarea]"><?php echo esc_textarea( $options['sometextarea'] ); ?></textarea> <label class="description" for="sample_theme_options[sometextarea]"><?php _e( 'Sample text box', 'sampletheme' ); ?></label> </td> </tr> </table> <p class="submit"> <input type="submit" value="<?php _e( 'Save Options', 'sampletheme' ); ?>" /> </p> </form> </div> <?php } /** * Sanitize and validate input. Accepts an array, return a sanitized array. */ function theme_options_validate( $input ) { global $select_options, $radio_options; // Our checkbox value is either 0 or 1 if ( ! isset( $input['option1'] ) ) $input['option1'] = null; $input['option1'] = ( $input['option1'] == 1 ? 1 : 0 ); // Say our text option must be safe text with no HTML tags $input['sometext'] = wp_filter_nohtml_kses( $input['sometext'] ); // Our select option must actually be in our array of select options if ( ! array_key_exists( $input['selectinput'], $select_options ) ) $input['selectinput'] = null; // Our radio option must actually be in our array of radio options if ( ! isset( $input['radioinput'] ) ) $input['radioinput'] = null; if ( ! array_key_exists( $input['radioinput'], $radio_options ) ) $input['radioinput'] = null; // Say our textarea option must be safe text with the allowed tags for posts $input['sometextarea'] = wp_filter_post_kses( $input['sometextarea'] ); return $input; } |
Source: Themeshaper
Display your latest Google+ WordPress blog
The following snippet will display your latest Google+ WordPress blog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php include_once(ABSPATH.WPINC.'/rss.php'); $googleplus = fetch_feed("http://plusfeed.appspot.com/103329092193061943712"); // Replace 103329092193061943712 by your own ID echo '<a href="'; echo $googleplus->items[0]['link']; echo '">'; echo $googleplus->items[0]['summary']; echo ''; ?> |