【问题标题】:Array wordpress php数组 wordpress php
【发布时间】:2016-10-27 21:32:15
【问题描述】:

我想在 wordpress 中创建一个投资组合。 它工作正常,但我想在 h2 标题下查看 h3 中的投资组合类别。

其他问题。

现在链接只能在标题 h2 上使用,我希望它在标题中作为链接。

欢迎对代码提出任何建议!非常感谢你

<?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>';
			
                I want to see here the portfolio image category in h3
  
                echo "</a>";
				echo '</div>'; /*close caption*/
				echo '</div>'; /*close caption*/
				echo '</div>'; 
				
				echo '</div>'; 
				endwhile;
				echo '</div>'; 
				echo '</div>'; 
				echo '</div>'; 
				
		?>
      
 

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

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    这应该适合你。您可以谷歌如何获取帖子的类别。它会产生你的答案 - 但这会奏效。

    <?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 -->
    

    【讨论】:

    • 试试这个$categories = get_the_terms( $id, 'portfolio-categories' );
    • @Nicola 没问题。这就是为什么这个社区在这里。现在,如果您不介意给我答案...谢谢:D
    • @Nicola 请点击接受此答案。这是答案旁边的大勾号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2021-03-19
    • 2014-09-05
    • 1970-01-01
    相关资源
    最近更新 更多