Last active
November 6, 2018 20:49
-
-
Save sxidsvit/44ba8fcc8b464e5f9e7b to your computer and use it in GitHub Desktop.
фрагменты кода для создания и настройки темы, включая вывод постов, сайдбаров и т.д
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------------------- style.css ------ создаем новую тему ---------------// | |
/* | |
Theme Name: MyTheme | |
*/ | |
// ----------------------- functions.php -------------------------------------// | |
<?php | |
remove_action('wp_head', 'rsd_link'); // удаляем лишний вывод | |
remove_action('wp_head', 'wlwmanifest_link'); // удаляем лишний вывод | |
remove_action('wp_head', 'wp_generator'); // удаляем лишний вывод | |
show_admin_bar(false); // убираем отображение админпанели над шапкой | |
// пользовательские функции | |
function category_name_id_by_slug($categoryslug) { | |
$idObj = get_category_by_slug($categoryslug); $id = $idObj->term_id; echo get_cat_name($id); | |
return $id; | |
}; | |
function category_id_by_slug($categoryslug) { | |
$idObj = get_category_by_slug($categoryslug); $id = $idObj->term_id; | |
return $id; | |
} | |
// подключаем дополнительный сайдбар | |
function logo_widget_init() { | |
register_sidebar( array( | |
'name' => 'Логотип SVG', | |
'id' => 'logo', | |
'before_widget' => '', | |
'after_widget' => '', | |
'before_title' => '<span class="hidden">', | |
'after_title' => '</span>', | |
) ); | |
} | |
add_action( 'widgets_init', 'logo_widget_init' ); | |
// подключение опций темы | |
require_once ( get_stylesheet_directory() . '/theme-options.php' ); | |
// добавляем в тему поддержку миниатюр (thumbnails) | |
add_theme_support('post-thumbnails'); | |
// ----------------------- heder.php index.php footer.php --------- фрагменты кода -------------------------// | |
// подключаем шапку и подвал | |
<?php get_header(); ?> | |
<?php get_footer(); ?> | |
// заголовок страницы | |
<title><?php echo get_bloginfo('name'); ?> / <?php echo get_bloginfo('description'); ?></title> | |
//подулючаем стили и библиотеки js | |
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/libs/bootstrap/bootstrap-grid.min.css" /> | |
<script src="<?php echo get_template_directory_uri(); ?>/libs/jquery/jquery-2.1.3.min.js"></script> | |
//подключаем динамический сайдбар | |
<?php dynamic_sidebar('logo'); ?> | |
// получаем id категории по ее ярлыку (слагу) [не требуется знание ID поста] | |
<?php $idObj = get_category_by_slug('s_about'); | |
$id = $idObj->term_id; | |
echo get_cat_name($id); ?> | |
<?php echo category_description($id); ?> | |
// информация о поcте по его id | |
<?php if ( have_posts() ) : query_posts('p=50'); | |
while (have_posts()) : the_post(); ?> | |
<?php if ( has_post_thumbnail() ) : ?> | |
<a class="popup" href="<?php | |
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' ); | |
echo $large_image_url[0];?>"> | |
<?php the_post_thumbnail(array(220, 220)); ?></a> | |
<?php endif; ?> | |
<?php the_title(); ?> | |
<?php the_content(); ?> | |
<? endwhile; endif; wp_reset_query(); ?> | |
// ======================================== Посты из одной категории =============================================== // | |
// по слагу (ярлыку) узнаем id категории и потом по id выводим посты | |
// мета-данные (произвольные поля -> resume_years) позволяют вывести дополнительную информацию | |
<h3><?php | |
$idObj = get_category_by_slug('c_work'); | |
$id = $idObj->term_id; | |
echo get_cat_name($id); ?> | |
<?php if ( have_posts() ) : query_posts('cat=' . $id); | |
while (have_posts()) : the_post(); ?> | |
<div class="resume_item"> | |
<div class="year"><?php echo get_post_meta($post->ID, 'resume_years', true); ?></div> | |
<div class="resume_description"><?php echo get_post_meta($post->ID, 'resume_place', true); ?> | |
<strong><?php the_title(); ?></strong></div> | |
<?php the_content(); ?> | |
</div> | |
<? endwhile; endif; wp_reset_query(); ?> | |
/* или другой вариант, применимый в Pinegrow (Custom WP Query name) => изменяем запрос */ | |
<?php $cat_posts_args = array('cat' => $id) ?> | |
<?php $cat_posts = new WP_Query( $cat_posts_args ); ?> | |
<?php if ( $cat_posts->have_posts() ) : ?> | |
<?php while ( $cat_posts->have_posts() ) : $cat_posts->the_post(); ?> | |
<div class="resume_item"> | |
<div class="year"> | |
<?php echo get_post_meta( get_the_ID(), 'resume_years', true ); ?> | |
</div> | |
<div class="resume_description"> | |
<?php echo get_post_meta( get_the_ID(), 'resume_place', true ); ?> | |
<strong><?php the_title(); ?></strong> | |
</div> | |
<?php the_content(); ?> | |
</div> | |
<?php endwhile; ?> | |
<?php wp_reset_postdata(); ?> | |
<?php else : ?> | |
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> | |
<?php endif; ?> | |
// =================================== Фильтрация постов по тегам ====================================================== // | |
// используем тэги для фильтрации постов | |
<?php if ( have_posts() ) : query_posts('cat=' . $id); | |
while (have_posts()) : the_post(); ?> | |
<?php $tags = wp_get_post_tags($post->ID); | |
if ($tags) { foreach($tags as $tag) { | |
echo ' ' . $tag->name;} | |
} ?> | |
<?php the_post_thumbnail(array(400, 300)); ?> | |
<?php the_title(); ?> | |
<?php the_excerpt(); ?> | |
><?php the_title(); ?> | |
<?php the_content(); ?> | |
<img src="<?php | |
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' ); | |
echo $large_image_url[0]; | |
?>" alt="<?php the_title(); ?>" /> | |
<? endwhile; endif; wp_reset_query(); ?> | |
// ============= Вывод значений опций - настроек темы - из конфигурационной страницы (файла) ====================== // | |
<h3>Адрес:</h3> | |
<p><?php $options = get_option('sample_theme_options'); echo $options['addresstext']; ?></p> | |
// ==================== Отформатированный вывод содержимого массива для его анализа ----- есть еще print_r() ========== // | |
<pre> | |
<?php var_dump($settings_options['header_text'])?> | |
</pre> | |
// ----------------------- Полезные плагины -------------------------// | |
BackupBuddy - создание резервной копии | |
WPBackItUp Backup & Restore - создание резервной копии | |
Shortcodes Ultimate 4.9.9 - вставка шорткодов | |
Disable Google Fonts - Disable enqueuing of Open Sans and other fonts used by WordPress from Google | |
Не ужели работает синхронизация? - Да, работает !!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment