duanhong8839 2015-01-19 09:27
浏览 23
已采纳

wordpress php按类别名称查询

I was wondering to do separate blog pages for separate category posts. What I was able to find was running query by category number, but this is not dynamic enough for me as I would like to run query on page title which is = category name.

Basically on page "events" I would like to display blog posts that are under category named "events"(category name == page title), and so on.

Any insights on how to achieve this would be great. What I tried to do and failed, was:

<?php query_posts('category_name='.get_the_title()); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div>
        <h1><?php the_title(); ?></h1>
        <p><?php the_content(); ?></p>
    </div>
<?php endwhile; else: ?>no match<?php endif; ?>

Thanks in forward for any insights, links or notices.

  • 写回答

2条回答 默认 最新

  • doudiandi6967 2015-01-19 10:22
    关注

    First of all, never ever use query_posts. It breaks the main query, reruns queries that slows your page, incorrectly sets vital functions used by plugins, functions like get_queried_object() and it messes up pagination.

    If you need to run custom queries, make use of WP_Query.

    To come to your original issue, the default category parameters don't make use of category names, only slugs and ID's. The slugs are controllable, so you might want to use slugs with the category_name parameter.

    If you do need to pass category names, then you should use a tax_query which do accept names passed to the terms parameter

    You can pass the following with your parameters

    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'name'
            'terms' => 'name of your category',
            'include_children' => false
        ) ,
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化