doule6314 2016-04-15 07:00
浏览 20
已采纳

如何打开某个类别的帖子并在网址中传递其名称?

i have displayed all my categories on a page through loop and whenever i click on any category its id is passed and posts related to that category are displayed .but its also shows the id of that category in the url . what i want is to do something in which url shows the name of the particular category that i have clicked . for this i have tried passing name of the category in the link then the id it solves the problem of the url i.e: category name shows up in the url but it creates another problem that it does not open the posts of that category but show error page not found : my code for displaying all categories is this:

<?php
foreach ( $categories as $cat ) {
    $id = $cat->term_id;
    //$name = $cat->cat_name;



    echo '<a href="'.get_site_url() . '/state_league_archives?id='.$id.'">'.$cat->cat_name.'</a>';

 ?>
 <hr>

                        <?php 
}


    ?>  

and this id i get on the archive page to show its posts like this:

     <?php
$cat_id= $_GET['id'];

        $args = array('showposts'=>25, 'category' => '$cat_id' ,'post_type'=>'stateleague-pos-type','category__in'=>array($cat_id));
$query = new WP_Query($args);
        while ( $query->have_posts() ) : $query->the_post(); ?>
        <h1><a href="<?php echo the_permalink();?>"><?php the_title();?></a></h1>
        <?php the_content(); ?> |
         <a href="<?php the_permalink(); ?>">
                                    Read more
                                </a>
         <div class="row">
                            <div class="ct-divider"> </div>
                        </div>

        <?php endwhile; ?>
  • 写回答

2条回答 默认 最新

  • doulao3905 2016-04-15 17:41
    关注

    I think you should go to settings->permalinks and select post name in common settings. This should solve your problem.

    Also change link to:

    echo '<a href="'.get_term_link($cat).'">'.$cat->name.'</a>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测