支持 » 安装、升级和使用 » 怎么取当前点击的tag值,并取出此tag下的所有文章列表

  • 已解决 metababy

    (@metababy)


    比如点的标签名为“淘宝”,在地址栏就是http://ant73.com/tag/淘宝
    但是我在php代码中,取出来是index.php,并不是淘宝,用 the_title() single_tag_title()能取出,但是,也没有效果,传值进去是空,在页面倒是能echo出来。感觉是在页面加载完了,才会取出这个值。而我是需要在加载完成前,就要得到,然后以tag名为参数,查出此Tag下的所有文章列表展示出来。
    以下是代码,我硬编码将tag名,定为淘宝,可以正常出来,但是想取当前点的标签名,总是不可得。

    注意 echo ‘a’. the_title() .’b’;
    预期是出现 a淘宝b
    实际是出现 淘宝ab

    目前遇到的问题是,无法取URL中的tag名
    <?php
    /**
    * The template for displaying archive pages
    *
    * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
    *
    * @package WordPress
    * @subpackage Twenty_Nineteen
    * @since 1.0.0
    */

    get_header();
    ?>

    <section id=”primary” class=”content-area”>
    <main id=”main” class=”site-main”>

    <?php if ( have_posts() ) : ?>

    <header class=”page-header”>
    <?php
    the_archive_title( ‘<h1 class=”page-title”>’, ‘</h1>’ );
    ?>
    </header><!– .page-header –>

    <?php

    echo ‘a’. the_title() .’b’;
    $tag=’淘宝’; //这里,需要取当前点击的标签值

    $args=array(‘tag’=>$tag,
    ‘showposts’=>300,
    ‘caller_get_posts’=>1);
    $my_query=new WP_Query($args);
    if ( $my_query->have_posts() ) {

    while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    <p>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></p>
    <?php
    endwhile;
    }
    wh_reset_query();
    ?>

    <?php
    // Start the Loop.

    // Previous/next page navigation.
    twentynineteen_the_posts_navigation();

    // If no content, include the “No posts found” template.
    else :
    get_template_part( ‘template-parts/content/content’, ‘none’ );

    endif;
    ?>
    </main><!– #main –>
    </section><!– #primary –>

    <?php
    get_footer();

查看 2 回复 - 1 至 2 (总计 2)
查看 2 回复 - 1 至 2 (总计 2)
  • 话题 ‘怎么取当前点击的tag值,并取出此tag下的所有文章列表’不再接受新回复。