Skip to content

Instantly share code, notes, and snippets.

@IGGY-MODEL
Created April 15, 2015 19:32
Show Gist options
  • Select an option

  • Save IGGY-MODEL/53cf84435a03fc809350 to your computer and use it in GitHub Desktop.

Select an option

Save IGGY-MODEL/53cf84435a03fc809350 to your computer and use it in GitHub Desktop.
WordPress
//////// WordPress Get Custom Field
<?php echo get_post_meta($post->ID, 'year', true); ?>
/////// WordPress query_posts
<?php if ( have_posts() ) : query_posts('p=1');
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php the_post_thumbnail(array(100, 100)); ?>
<? endwhile; endif; wp_reset_query(); ?>
/////// WordPress get tags
<?php
$tags = get_tags();
if ($tags) {
foreach ($tags as $tag) {
echo '<p>Tag: <a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a> </p> ';
}
}
?>
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
foreach($tags as $tag) {
echo '<p>' . $title . '<a href="' . get_term_link( $tag, 'post_tag' ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a> has ' . $tag->count . ' post(s). </p> ';
}
}
?>
/////// WordPress Get Category name By label
<?php
$idObj = get_category_by_slug('s_about');
$id = $idObj->term_id;
echo get_cat_name($id);
?>
////////Wordpress - Условия вывода single.php
<?php
$post = $wp_query->post;
if (in_category('cat_label_1')) {
include(TEMPLATEPATH.'/single-cat_label_1.php');
} elseif (in_category('cat_label_2')) {
include(TEMPLATEPATH.'/single-cat_label_2.php');
}
?>
/////// WordPress Category Home
<?php
if ( have_posts() ) : // если имеются записи в блоге.
query_posts('cat=3'); // указываем ID рубрик, которые необходимо вывести.
while (have_posts()) : the_post(); // запускаем цикл обхода материалов блога
?>
<?php the_post_thumbnail(array(100, 100)); ?>
<? endwhile; // завершаем цикл.
endif;
/* Сбрасываем настройки цикла. Если ниже по коду будет идти еще один цикл, чтобы не было сбоя. */
wp_reset_query();
?>
/////// Wordpress Menu Raw
//functions.php
register_nav_menus(array(
'top_mnu' => 'Top Menu',
));
//Template
wp_nav_menu(array(
'theme_location' => 'top_mnu'
));
//////// WordPress Magic Field Multiple Images Resize and Crop
<?php
$images = getFieldOrder('img');
$i = 0;
foreach($images as $image) {
$i++;
$nuevos = array ("w" => 300, "h" => 200, "zc" => 1, "q" => 100);
$image_thumb = get_image('img', 1, $i, 0, NULL, $nuevos);
$image_link = get_image('img', 1, $i, 0, NULL); ?>
<?php echo $image_thumb; ?><br/>
<?php echo $image_link; ?><br/><br/>
<? }; ?>
//////// Wordpress Comments Outside Loop
<?php
wp_reset_query();
global $withcomments;
$withcomments = 1;
comments_template( '', true );
/////// Wordpress Theme Options ///////////////////////////////////////////
//In Template
<?php
$options = get_option('sample_theme_options');
echo $options['phone1'];
?>
//in functions.php
require_once ( get_stylesheet_directory() . '/theme-options.php' );
//theme-options.php file:
<?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]" class="regular-text" 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>
<tr valign="top"><th scope="row"><?php _e( 'Телефон компании', 'sampletheme' ); ?></th>
<td>
<input id="sample_theme_options[phone1]" class="regular-text" type="text" name="sample_theme_options[phone1]" value="<?php esc_attr_e( $options['phone1'] ); ?>" />
<label class="description" for="sample_theme_options[phone1]"><?php _e( 'Введите номер телефона', '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]" class="large-text" 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" class="button-primary" 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;
}
// adapted from http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
////////////////END Wordpress Theme Options //////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment