/ Wordpress / Attachment - практическое применение

Attachment — практическое применение

HIT

28.01.2016

2184

Файлы attachment — это все что мы загружаем на сайт: изображения, документы, аудио и т.д.

Файлы attachment в поиске

Поместить в файл functions

// attachment в выводе поиска

function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', array('attachment','post','page'));
$query->set('post_status', array('publish','inherit'));
}
return $query;
}
add_action('pre_get_posts','SearchFilter');

Структурирование медиафайлов

Все в functions.php

Регистрируем что-то (не знаю для чего)

function wptp_register_attachments_tax() {
}
add_action( 'init', 'wptp_register_attachments_tax', 0 );

Регистрируем таксономию gallery-category

register_taxonomy( 'gallery-category', 'attachment',
    array(
        'labels' =>  array(
            'name'              => 'Gallery Categories',
            'singular_name'     => 'Gallery Category',
            'search_items'      => 'Search Gallery Categories',
            'all_items'         => 'All Gallery Categories',
            'edit_item'         => 'Edit Gallery Categories',
            'update_item'       => 'Update Gallery Category',
            'add_new_item'      => 'Add New Gallery Category',
            'new_item_name'     => 'New Gallery Category Name',
            'menu_name'         => 'Gallery Category',
        ),
        'hierarchical' => true,
        'sort' => true,
        'show_admin_column' => true
    )
);

Это для шаблона вывода таксономии gallery-category

// добавление вложений в основной запрос с помощью фильтра parse_query и функции wptutsplus_add_attachments_to_tax_query() 

function wptutsplus_add_attachments_to_tax_query() {

    global $wp_query;

    // When inside a custom taxonomy archive include attachments
    if ( is_tax( array( 'document-category', 'gallery-category' ) ) ) {
        $wp_query->query_vars['post_type'] =  array( 'attachment' );
        $wp_query->query_vars['post_status'] =  array( null );

        return $wp_query;
    }
}
add_action('parse_query', 'wptutsplus_add_attachments_to_tax_query');

Шаблон вывода таксономии gallery-category

Создаем файл с именем taxonomy-gallery-category.php

<?php get_header(); ?>

<div align="center" class="wrapper">
<div class="wrapper-2">
 
<div id="main-content">
<div id="content">
 
<div class="pre-post-content"> 
 <?php $queried_object = get_queried_object();
 echo '<h1 id="title">Gallery - ' . $queried_object->name . '</h1>'; ?>
<div id="path"><?php if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs(); ?></div>
 
</div>

 <?php if ( have_posts() ) : ?>
 
 <section class="gallery <?php echo $queried_object->name; ?>">
 
 <?php // Start the Loop.
 while ( have_posts() ) : the_post(); 
 
 // define attributes for image display
 $imgattr = array(
 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
 ); ?>
 
<div class="gallery-image"> 
 
<a href ="<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full', false );
echo $src[0]; ?>">
 
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail', $imgattr ); ?>
 
</a>
 
<a href ="<?php echo get_attachment_link(); ?>"><?php the_title(); ?></a>
 
</div>
 
 <?php endwhile; ?>
 
 </section>
 
 <?php else :

 // If no content, include the "No posts found" template.
 get_template_part( 'content', 'none' );

 endif; ?>
 
 
<?php include(TEMPLATEPATH."/tax-navigate.php");?> 
 

</div> <!-- end content -->
</div> <!-- end main-content -->
 

</div>
</div> 

<?php get_footer(); ?>

Поделиться в соц. сетях:

  • Комментарии
  • Вложения

Добавить комментарий

Пока нет комментариев. Будь первым!

Attachment — практическое применение
Количество товара
Рекомендации для васКоличество товараOpttour.ru
Спасибо! Наш менеджер свяжется с Вами в течении 5 минут.