WordPress - 在循环中第一次发布后插入内容

我已经设置 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; ?>

希望有人可以提供帮助。


鸿蒙传说
浏览 149回答 1
1回答

MMTTMM

在 foreach 循环之前使用它。echo '<h3>Browse Categories</h3>';
打开App,查看更多内容
随时随地看视频慕课网APP