【问题标题】:WordPress - Insert content after first post in LoopWordPress - 在循环中第一次发布后插入内容
【发布时间】:2020-01-16 14:14:10
【问题描述】:

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

希望有人能提供帮助。

谢谢,

湄公河

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    在 foreach 循环之前使用它。 echo '&lt;h3&gt;Browse Categories&lt;/h3&gt;';

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多