2016-10-27 2 views
-1

Я хочу создать портфолио в wordpress. Он работает правильно, но я хотел бы видеть категории портфеля в h3 под заголовком h2.Array wordpress php

Другой вопрос.

Теперь ссылка будет работать только над заголовком h2, я бы хотел, чтобы все было над подпиской в ​​качестве ссылки.

любые советы по коду приветствуются! Большое спасибо

<?php 
 
     $args = array('post_type' => 'portfolio', 'posts_per_page' => -1); 
 
     $loop = new WP_Query($args); 
 
\t $terms = get_the_terms($post->ID, 'portfolio-categories'); \t \t \t \t \t \t 
 
      if ($terms && ! is_wp_error($terms)) : 
 
    
 
       $links = array(); 
 
    
 
       foreach ($terms as $term) { 
 
        $links[] = $term->name; 
 
       } 
 
    
 
       $tax_links = join(" ", str_replace(' ', '-', $links));   
 
       $tax = strtolower($tax_links); 
 
      else : \t 
 
\t  $tax = ''; \t \t \t \t \t 
 
      endif; 
 
\t  
 
     while ($loop->have_posts()) : $loop->the_post(); 
 
\t \t \t \t 
 
\t \t \t \t echo '<div class="row-masonry">'; 
 
\t \t \t \t echo '<div class="item">'; 
 
\t \t \t \t echo '<div class="well portfolio-item no-gutter">'; 
 
\t \t \t \t echo '<div class="thumbnail no-gutter">'. get_the_post_thumbnail() .'</div>'; 
 
\t \t \t \t echo '<div class="caption">'; 
 
\t \t \t \t echo '<div class="vertical-align">'; 
 
\t \t \t \t $link = get_the_permalink(); 
 
       echo "<a href=$link>"; 
 
       echo '<h2>'. get_the_title() .'</h2>'; 
 
\t \t \t 
 
       I want to see here the portfolio image category in h3 
 
    
 
       echo "</a>"; 
 
\t \t \t \t echo '</div>'; /*close caption*/ 
 
\t \t \t \t echo '</div>'; /*close caption*/ 
 
\t \t \t \t echo '</div>'; 
 
\t \t \t \t 
 
\t \t \t \t echo '</div>'; 
 
\t \t \t \t endwhile; 
 
\t \t \t \t echo '</div>'; 
 
\t \t \t \t echo '</div>'; 
 
\t \t \t \t echo '</div>'; 
 
\t \t \t \t 
 
\t \t ?> 
 
     
 
    
 

 
    
 
    </div><!-- #page -->

ответ

1

Это должно работать для вас. Вы можете google как получить категорию для сообщения. Это даст вам ответ, но это сработает.

<?php 
$args = array('post_type' => 'portfolio', 'posts_per_page' => -1); 
$loop = new WP_Query($args); 
$terms = get_the_terms($post->ID, 'portfolio-categories'); 
if ($terms && ! is_wp_error($terms)) : 

    $links = array(); 

    foreach ($terms as $term) { 
     $links[] = $term->name; 
    } 

    $tax_links = join(" ", str_replace(' ', '-', $links)); 
    $tax = strtolower($tax_links); 
else : 
    $tax = ''; 
endif; 

while ($loop->have_posts()) : $loop->the_post(); 

    echo '<div class="row-masonry">'; 
    echo '<div class="item">'; 
    echo '<div class="well portfolio-item no-gutter">'; 
    echo '<div class="thumbnail no-gutter">'. get_the_post_thumbnail() .'</div>'; 
    echo '<div class="caption">'; 
    echo '<div class="vertical-align">'; 
    $link = get_the_permalink(); 
    echo "<a href=$link>"; 
    echo '<h2>'. get_the_title() .'</h2>'; 

    // GET THE CATEGORY -- Returns an array of all categories 
    // $categories = get_the_category(); 
    $categories = get_the_terms(get_the_ID(), 'portfolio-categories'); // for custom taxnomies 

    // If not an empty array then show the first category set 
    if (! empty($categories)) { 
     echo "<h3>" . esc_html($categories[0]->name) ."</h3>"; 
    } 



       echo "</a>"; 
       echo '</div>'; /*close caption*/ 
       echo '</div>'; /*close caption*/ 
       echo '</div>'; 

       echo '</div>'; 
endwhile; 
echo '</div>'; 
echo '</div>'; 
echo '</div>'; 

?> 




</div><!-- #page --> 
+0

Попробуйте это '$ categories = get_the_terms ($ id, 'portfolio-categories');' – Radmation

+0

Большое вам спасибо !!! Он отлично работает! Ты спас меня ;-) – Nicola

+0

@Nicola Нет проблем. Вот почему это сообщество здесь. Теперь, если вы не возражаете дать мне ответ ... СПАСИБО: D – Radmation