我已經設置 PHP 代碼在顯示帖子的 WordPress 主頁上的第一個帖子之后插入類別列表。它工作正常。我的問題是,我想在列表上方顯示一個標題,可能是帶有文本“瀏覽我們的類別”或其他內容的 HTML H3 標記。我的問題是,我在哪里放置這個 HTML 標題?如果我把它放在div 標簽中(見下面的代碼),標題會在頁面下方重復,在所有其他帖子下方。我只希望它出現一次 - 在第一篇文章之后,就在我的類別列表上方。我嘗試將其作為 $output 變量的一部分,例如: $output = '<h3>Browse Categories</h3>'; $output .= '<a class="cat-links" href=" ... etc.但這不起作用。我正在使用的代碼如下:<?php if (have_posts()) : while (have_posts()) : the_post(); ?><?php get_template_part('content', get_theme_mod('solopine_home_layout')); ?><?php // CUSTOM CODE TO DISPLAY LIST OF CATEGORIES ?><div style="text-align:center; padding-bottom:100px;"><?php if( $wp_query->current_post == 0 ) { $allowed = array(17,18,19,20); $categories = get_categories(array( 'orderby' => 'name', 'parent' => 0, 'hide_empty'=> true, 'include' => $allowed, ) ); $separator = ' '; $output = ''; if($categories){ foreach($categories as $category) { $output .= '<a class="cat-links" href="'.get_category_link( $category ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category- >cat_name.'</a>'; } echo trim($output); }} ?> </div><?php // END CUSTOM CODE ?> <?php endwhile; ?>希望有人可以提供幫助。
- 1 回答
- 0 關注
- 140 瀏覽
添加回答
舉報
0/150
提交
取消