简体   繁体   English

如何显示循环前帖子中的所有标签?

[英]How to show all tags from posts before loop?

I have a structure like this in my wordpress search.php: 我的wordpress search.php中有这样的结构:

<div class="tags">
   <!-- show all tags from posts in here -->
</div>


<div class="posts">
   <!-- Wordpress Query loop here -->
   <!-- get_the_tags() for each posts -->
   <!-- End loop -->
</div>

I'm able to show inside the loop the list of tags for all the posts. 我可以在循环内显示所有帖子的标签列表。 But I need to show them on the div with the class "tags" that is outside the loop. 但是我需要在div上用循环之外的“标签”类向他们展示它们。

I know that if I want to show them after the loop is easy, I just have to use a global php variable and show them afterwards. 我知道,如果要在循环简单之后显示它们,我只需要使用全局php变量并在之后显示它们即可。 But the only way I think I can add those tags before the loop is inserting them on the HTML DOM with javascript. 但是,我认为我可以在循环之前添加这些标签的唯一方法是使用JavaScript将它们插入HTML DOM。

Is there any other method to do this more easily?. 还有其他方法可以更轻松地执行此操作吗?

Full code as requested: 根据要求的完整代码:

<?php 
global $global_tags;
?>

<div class="col-left">
<div class="tags">
    <div class="placeholder-tags"></div>   
</div>

</div>
<div class="col-right">

<div class="profile-wrapper">
<?php 

$tag = single_tag_title( '', false );


$args = array (
'pagination'=> true,
'posts_per_page' => '8',
'post_type' => 'profile',
'tag_slug__in' =>array($tag)

);

$the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>

<!-- pagination here -->

<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div class="profile">
    <a href="<?php echo get_permalink(); ?>">

        <?php $current_profile = get_field("profile_personal")[0]; ?>


        <div class="profile-image">
        <img alt="" src="<?php echo  $current_profile["profile_image"]['sizes']['thumbnail'];  ?>"/><br>
        </div>
        <div class="profile-name">
        <?php echo  $current_profile["profile_name"] . ' ' .  $current_profile["profile_surname"];  ?>
        </div>
        <div class="profile-country">
        <?php echo $current_profile["profile_country"];  ?><br>
        </div>

        <?php

        $list_tags = get_the_tags();
        foreach ( $list_tags   as $single_tag) {
        $global_tags[] = $single_tag->slug;
        }               
        ?>

        <?php if ( has_category("bolaber",$post->ID) ) { ?>
        <div class="worked-with-us">
            &#x25cf;
        </div>
        <?php } ?>

    </a>
    </div>

<?php endwhile; ?>
<!-- end of the loop -->

<?php wp_reset_postdata(); ?>

<?php else : ?>
<p><?php _e( 'default Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

I came up with a not very elegant or efficient solution, but it works perfectly, I added another loop on the tags placeholder with the same query loop parameters, but without showing anything but the tags. 我想出了一个不太优雅或有效的解决方案,但它工作得很好,我在标签占位符上添加了另一个具有相同查询循环参数的循环,但除了标签外没有显示其他任何内容。

<?php 
global $global_tags;
?>

<div class="col-left">

<div class="tags">


    <?php 

    $tag = single_tag_title( '', false );


    $args = array (
    'pagination'=> true,
    'posts_per_page' => '8',
    'post_type' => 'profile',
    'tag_slug__in' =>array($tag)

    );

    $the_query = new WP_Query( $args ); ?>

    <?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>       
        <?php

        $list_tags = get_the_tags();
        foreach ( $list_tags   as $single_tag) {
        $global_tags[] = $single_tag->slug;
        }               
        ?>
    <?php endwhile; ?>


    <pre>
        <?php
        if ( $global_tags ) {

        $global_tags = array_unique($global_tags);


        foreach ($global_tags as $value) {
            echo '</br><a href="'. site_url() .'/tag/'. $value . '">#'. $value  .'</a>';
        }
        }


        ?>

    </pre>

    <?php wp_reset_postdata(); ?>    
    <?php else : ?>
    <?php endif; ?>


</div>

</div>
<div class="col-right">

<div class="profile-wrapper">
<?php 

$tag = single_tag_title( '', false );


$args = array (
'pagination'=> true,
'posts_per_page' => '8',
'post_type' => 'profile',
'tag_slug__in' =>array($tag)

);

$the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>

<!-- pagination here -->

<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div class="profile">
    <a href="<?php echo get_permalink(); ?>">

        <?php $current_profile = get_field("profile_personal")[0]; ?>


        <div class="profile-image">
        <img alt="" src="<?php echo  $current_profile["profile_image"]['sizes']['thumbnail'];  ?>"/><br>
        </div>
        <div class="profile-name">
        <?php echo  $current_profile["profile_name"] . ' ' .  $current_profile["profile_surname"];  ?>
        </div>
        <div class="profile-country">
        <?php echo $current_profile["profile_country"];  ?><br>
        </div>

        <?php

        $list_tags = get_the_tags();
        foreach ( $list_tags   as $single_tag) {
        $global_tags[] = $single_tag->slug;
        }               
        ?>

        <?php if ( has_category("bolaber",$post->ID) ) { ?>
        <div class="worked-with-us">
            &#x25cf;
        </div>
        <?php } ?>

    </a>
    </div>

<?php endwhile; ?>
<!-- end of the loop -->

<?php wp_reset_postdata(); ?>

<?php else : ?>
<p><?php _e( 'default Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Snoowrap - 如何在某个日期之前从 subreddit 中获取所有帖子? - Snoowrap - How to get all posts from a subreddit before a certain date? 在页面上的Wordpress循环中从一个帖子滚动到另一个帖子,并显示所有帖子 - Scroll from post to post in Wordpress loop on page with all posts displaying 通过标签将帖子动态排序到Wordpress循环中 - Dynamically sort posts by tags into a Wordpress loop 如何根据数组循环返回的值隐藏和显示图像和文本标签 - how to hide and show image and text tags according to values returned from array loop 如何通过使用JavaScript循环使用Blogger API检索所有帖子 - How to retrieve all posts using blogger API by using JavaScript loop 在处理之前从 json/js 提要中删除所有 javascript 标签 - removing all javascript tags from a json/js feed before processing 如何在反应原生中仅显示来自firestore的登录用户的帖子? - How to show posts of only the logged in user from firestore in react native? 如何使用Java Script显示/隐藏具有相同类的所有标签? - How to show / hide all tags with the same class with Java Script? 用户进入页面底部之前,Infitine滚动会发布来自MySQL的所有数据 - Infitine scroll posts all data from MySQL before user gets to the bottom of the page 如何在 while 循环中显示所有步骤 - How to show all steps in a while loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM