Skip to content

Instantly share code, notes, and snippets.

@AS87-code
Last active November 18, 2017 14:01
Show Gist options
  • Save AS87-code/3107c68fcfa16a507ae5 to your computer and use it in GitHub Desktop.
Save AS87-code/3107c68fcfa16a507ae5 to your computer and use it in GitHub Desktop.
WordPress
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
<?php query_posts( 'meta_key=slide&showposts=3' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="item <?php echo get_post_meta($post->ID, "class_active", true); ?>">
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(); } ?>
<div class="container">
<div class="carousel-caption slogan">
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<p><a class="btn btn-lg btn-primary button" href="#" role="button">Sign up today</a></p>
</div>
<div class="helper"></div>
</div>
</div>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
function dimox_breadcrumbs() {
$showOnHome = 0; // 1 - показывать "хлебные крошки" на главной странице, 0 - не показывать
$delimiter = '»'; // разделить между "крошками"
$home = 'Главная'; // текст ссылка "Главная"
$showCurrent = 1; // 1 - показывать название текущей статьи/страницы, 0 - не показывать
$before = '<span class="current">'; // тег перед текущей "крошкой"
$after = '</span>'; // тег после текущей "крошки"
global $post;
$homeLink = get_bloginfo('url');
if (is_home() || is_front_page()) {
if ($showOnHome == 1) echo '<div id="crumbs"><a href="' . $homeLink . '">' . $home . '</a></div>';
} else {
echo '<div id="crumbs"><a href="' . $homeLink . '">' . $home . '</a> ' . $delimiter . ' ';
if ( is_category() ) {
$thisCat = get_category(get_query_var('cat'), false);
if ($thisCat->parent != 0) echo get_category_parents($thisCat->parent, TRUE, ' ' . $delimiter . ' ');
echo $before . 'Архив рубрики "' . single_cat_title('', false) . '"' . $after;
} elseif ( is_search() ) {
echo $before . 'Результаты поиска по запросу "' . get_search_query() . '"' . $after;
} elseif ( is_day() ) {
echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
echo $before . get_the_time('d') . $after;
} elseif ( is_month() ) {
echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
echo $before . get_the_time('F') . $after;
} elseif ( is_year() ) {
echo $before . get_the_time('Y') . $after;
} elseif ( is_single() && !is_attachment() ) {
if ( get_post_type() != 'post' ) {
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>';
if ($showCurrent == 1) echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
} else {
$cat = get_the_category(); $cat = $cat[0];
$cats = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
if ($showCurrent == 0) $cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
echo $cats;
if ($showCurrent == 1) echo $before . get_the_title() . $after;
}
} elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
$post_type = get_post_type_object(get_post_type());
echo $before . $post_type->labels->singular_name . $after;
} elseif ( is_attachment() ) {
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID); $cat = $cat[0];
echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a>';
if ($showCurrent == 1) echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
} elseif ( is_page() && !$post->post_parent ) {
if ($showCurrent == 1) echo $before . get_the_title() . $after;
} elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
for ($i = 0; $i < count($breadcrumbs); $i++) {
echo $breadcrumbs[$i];
if ($i != count($breadcrumbs)-1) echo ' ' . $delimiter . ' ';
}
if ($showCurrent == 1) echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
} elseif ( is_tag() ) {
echo $before . 'Записи с тегом "' . single_tag_title('', false) . '"' . $after;
} elseif ( is_author() ) {
global $author;
$userdata = get_userdata($author);
echo $before . 'Статьи автора ' . $userdata->display_name . $after;
} elseif ( is_404() ) {
echo $before . 'Error 404' . $after;
}
if ( get_query_var('paged') ) {
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
echo __('Page') . ' ' . get_query_var('paged');
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
}
echo '</div>';
}
}
// end dimox_breadcrumbs()
<?php if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs(); ?>
<?php
wp_reset_query();
global $withcomments;
$withcomments = 1;
comments_template( '', true );
Активация миниатюр
add_theme_support('post-thumbnails');
***
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_generator');
show_admin_bar(false);
***
Settings theme
require_once ( get_stylesheet_directory() . '/theme-options.php' );
register_nav_menus( array(
'header_menu' => 'Header Menu',
'footer_menu' => 'Footer Menu',
) );
***
Register Css
function bootstrap_styles_with_the_lot() {
wp_register_style( 'bootstrap', get_template_directory_uri() . '/libs/bootstrap/css/bootstrap.min.css', array(), '', 'all' );
wp_enqueue_style( 'bootstrap' );
}
add_action( 'wp_enqueue_scripts', 'bootstrap_styles_with_the_lot', 1);
*** Register JS
function bootstrap_scripts() {
wp_register_script( 'bootstrap', get_template_directory_uri() . '/libs/bootstrap/js/bootstrap.min.js', array('jquery'), '', true );
wp_enqueue_script( 'bootstrap' );
}
add_action( 'wp_enqueue_scripts', 'bootstrap_scripts', 2 );
// Переменные и умная длина цитаты.
function print_excerpt($length) { // Максимальная длина цитаты. Длина задается в символах
global $post;
$text = $post->post_excerpt;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
}
$text = strip_shortcodes($text); // опционально, рекомендуется
$text = strip_tags($text); // используйте' $text = strip_tags($text,'<p><a>'); ' если хотите оставить некоторые теги
$text = substr($text,0,$length);
$excerpt = reverse_strrchr($text, '.', 1);
if( $excerpt ) {
echo apply_filters('the_excerpt',$excerpt);
} else {
echo apply_filters('the_excerpt',$text);
}
}
function reverse_strrchr($haystack, $needle, $trail) {
return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}
==
<?php if (function_exists('print_excerpt')) print_excerpt(120); ?>
function wp_sidebars() {
/* В боковой колонке - первый сайдбар */
register_sidebar(
array(
'id' => 'left', // уникальный id
'name' => 'Left sidebare', // название сайдбара
'description' => 'Перетащите сюда виджеты, чтобы добавить их в сайдбар.', // описание
'before_widget' => '<div id="%1$s" class="side widget %2$s">', // по умолчанию виджеты выводятся <li>-списком
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">', // по умолчанию заголовки виджетов в <h2>
'after_title' => '</h3>'
)
);
/* В подвале - второй сайдбар */
register_sidebar(
array(
'id' => 'footer',
'name' => 'Footer sidebare',
'description' => 'Перетащите сюда виджеты, чтобы добавить их в футер.',
'before_widget' => '<div id="%1$s" class="foot widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);
}
add_action( 'widgets_init', 'wp_sidebars' );
---
<aside>
<?php if ( is_active_sidebar( 'left' ) ) : ?>
<?php dynamic_sidebar( 'left' ); ?>
<?php endif; ?>
</aside>
<?php
$idObj = get_category_by_slug('s_about');
$id = $idObj->term_id;
echo get_cat_name($id);
?>
<?php echo get_post_meta($post->ID, 'year', true); ?>
<?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> ';
}
}
?>
<?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> ';
}
}
?>
<?php if ( have_posts() ) : query_posts('p=1',category_name);
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php the_excerpt(); ?> ---> <a href="<?php echo get_permalink(); ?>"> [ Читать далее → ]</a>
<?php the_post_thumbnail(array(100, 100)); ?>
<?php comments_popup_link('дефолт значение (0)', 'что если 1 (1)', '%') ?> - вывод кол-во коменнтов
<?php the_category(', ');?> -- вывод рубрик
<?php echo get_permalink(); ?> -- постоянная ссылка
<? endwhile; endif; wp_reset_query(); ?>
<?php $the_query = new WP_Query( 'tag=метка' ); ?>
<?php while ($the_query->have_posts() ) : $the_query->the_post(); ?>
//тут выводим то что нам нужно, например, список ссылок
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
<?php endwhile; ?>
<?php wp_reset_postdata();?>
//примеры https://wpmag.ru/2013/wp_query/
jQuery(document).ready( function ($) {
$('img').each( function () {
$(this).removeAttr( 'width' );
$(this).removeAttr( 'height' );
});
});
img {
max-width: 100%;
height: auto;
width: auto\9; /* ie8 */
}
После импорта базы данных можно выполнить следующую MySQL-команду:
UPDATE wp_options SET option_value = 'http://mysite.ru' WHERE option_value = 'http://mysite.local';
or
В файле wp-config.php добавить строки:
define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');
codex.wordpress.org/Changing_The_Site_URL
необходимо открыть в редакторе файл wp-config.php и установить соответствующие настройки для соединения с новой базой данных
***
Сайт ненадолго закрыт на техническое обслуживание. Зайдите через минуту.
.maintenance >
<?php $upgrading = time();
***
<?php get_header (); ?>
<?php get_footer (); ?>
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<?php echo get_bloginfo('name'); ?> // Site info
<?php echo get_bloginfo('description'); ?> // Site info
<?php echo get_post_meta($post->ID, "Price", true); ?> // Вывод дополнительного поля (класс, ссылка и т.д.)
<?php echo get_template_directory_uri ();?> // Puths for links
<?php echo get_cat_name(5) ?> // Get category title
<?php echo category_description(5); ?> // Get category description
// Get content for p=30 -> (tag=*, cat=*)
<?php if ( have_posts() ) : query_posts('p=30');
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php the_excerpt(); ?>
<?php if (function_exists('print_excerpt')) print_excerpt(120); ?>
<?php get_search_form(); ?> -> search-form.php
<?php get_template_part( 'template-parts/content', 'page' );
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(); } ?>
<?php endwhile; endif; wp_reset_query(); ?>
if ( is_singular( 'attachment' ) ) {
the_post_navigation
//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/
<?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');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment