为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
鸿蒙传说 2022-07-22 16:31:34
我已经设置 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 回答

?
MMTTMM

TA贡献1869条经验 获得超4个赞

在 foreach 循环之前使用它。echo '<h3>Browse Categories</h3>';



查看完整回答
反对 回复 2022-07-22
  • 1 回答
  • 0 关注
  • 123 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号