json:当点击一个类别链接时,我想只显示该类别中的帖子

json: when clicking on a category link I want to display only posts in that category

本文关键字:显示 链接 json 一个      更新时间:2024-01-07

我想获得分配给每个类别的帖子列表,并用slug作为链接显示标题,我不想硬编码类别名称,而是想获取分配给每个分类的每个帖子。我希望这是有道理的。

我的问题是,如果这个职位属于"所有合格的退休计划"类别,我该如何比较?

我想做的是…当我点击链接"#所有符合条件的退休计划"时,我只想让这些帖子显示为"所有符合条件退休计划"类别

    <ul data-role="listview" data-inset="true" class="ui-listview ui-listview-inset ui-corner-all ui-shadow">
    <li data-id="4" data-parent="1" class="ui-first-child">
<a href="#all-qualified-retirement-plans" class="ui-btn ui-btn-icon-right ui-icon-carat-r">All Qualified Retirement Plans</a></li>
  <li data-id="7" data-parent="0" class="ui-first-child">
<a href="#fica-ss-medicare-taxes" class="ui-btn ui-btn-icon-right ui-icon-carat-r">FICA - SS &amp; MEDICARE TAXES</a></li>
  <li data-id="2" data-parent="0">
<a href="#healthcare-plans" class="ui-btn ui-btn-icon-right ui-icon-carat-r"/> Healthcare Plans </a></li>
      <li data-id="3" data-parent="0">
<a href="#other-benefits" class="ui-btn ui-btn-icon-right ui-icon-carat-r">Other Benefits</a></li>
        </ul>

function allQualifiedRetirementPlans(data) {
  "use strict";
  var output = '<ul  data-role="listview" data-filter="true" data-filter-placeholder="Search..." >';
  $.each(data.posts, function(key, val) {
    output += '<li>';
    output += '<a href="#' + val.slug + '">';  // post slug
    output += '<h3>' + val.title + "</h3>";  // post title
    output += '</a>';
    output += '</li>';
  }); //go through each post

  output += "</ul>";
  $('#all-qualified-retirement-plans-postlist').html(output);

//json

var data = {
      "status": "ok",
        "count": 24,
        "count_total": 24,
        "pages": 1,
        "posts": [
            {
                "id": 131,
                "type": "benefits",
                "slug": "highly-compensated-employee-hce",
                "url": "http://mywebsite.com/benefits/highly-compensated-employee-hce/",
                "status": "publish",
                "title": "Highly Compensated Employee (“HCE”)",
                "title_plain": "Highly Compensated Employee (“HCE”)",
                "content": "<p>Qualified Retirement Plans must not favor Highly Compensated Employees (HCEs). HCEs are used in testing for discrimination, such as testing employee and employer contributions under a 401(k) plan. An HCE is one who either: a) owns more than 5% of the organization, either directly or indirectly, no matter what that person’s pay is, or b) any employee whose pay from the organization, for the previous year, exceeds the amount shown below.</p>'n",
                "excerpt": "<p>Qualified Retirement Plans must not favor Highly Compensated Employees (HCEs). HCEs are used in testing for discrimination, such as testing employee and employer contributions under a 401(k) plan. An HCE is one who either: a) owns more than 5% of the organization, either directly or indirectly, no matter what that person’s pay is, or b) &hellip; <a href='"http://mywebsite/benefits/highly-compensated-employee-hce/'" class='"more-link'">Continue reading<span class='"screen-reader-text'"> &#8220;Highly Compensated Employee (“HCE”)&#8221;</span></a></p>'n",
                "date": "2016-04-01 18:06:34",
                "modified": "2016-04-01 18:06:54",
                "categories": [
                    {
                        "id": 4,
                        "slug": "all-qualified-retirement-plans",
                        "title": "All Qualified Retirement Plans",
                        "description": "",
                        "parent": 1,
                        "post_count": 4
                    },
                    {
                        "id": 1,
                        "slug": "retirement-plans",
                        "title": "Retirement Plans",
                        "description": "",
                        "parent": 0,
                        "post_count": 13
                    }
                ],
                "tags": [],
                "author": {
                    "id": 1,
                    "slug": "keith",
                    "name": "keith",
                    "first_name": "",
                    "last_name": "",
                    "nickname": "keith",
                    "url": "",
                    "description": ""
                },
                "comments": [],
                "attachments": [],
                "comment_count": 0,
                "comment_status": "closed",
                "custom_fields": {
                    "extra_content_extra_content_title": [
                        ""
                    ],
                    "extra_content_extra_content_medicare": [
                        ""
                    ],
                    "extra_content_extra_content_percent_moved": [
                        " "
                    ],
                    "extra_content_extra_content_percent_value": [
                        ""
                    ],
                    "extra_content_extra_content_first_value": [
                        "2016: 120,000"
                    ],
                    "extra_content_extra_content_second_value": [
                        "2015: 120,000"
                    ],
                    "extra_content_extra_content_third_value": [
                        "2014: 115,000"
                    ],
                    "extra_content_extra_content_fourth_value": [
                        "2013: 115,000"
                    ],
                    "extra_content_extra_content_firth_value": [
                        "2012: 115,000"
                    ],
                    "extra_content_extra_content_sixth_value": [
                        "2011: 110,000"
                    ],
                    "extra_content_extra_content_seventh_value": [
                        "2010: 110,000"
                    ],
                    "extra_content_extra_content_eight_value": [
                        ""
                    ],
                    "extra_content_extra_content_nine_value": [
                        ""
                    ],
                    "extra_content_extra_content_ten_value": [
                        ""
                    ],
                    "social_security_social_security_title": [
                        ""
                    ],
                    "social_security_social_security_field_one": [
                        ""
                    ],
                    "social_security_social_security_field_two": [
                        ""
                    ],
                    "social_security_social_security_field_three": [
                        ""
                    ],
                    "social_security_social_security_field_four": [
                        ""
                    ],
                    "social_security_social_security_field_five": [
                        ""
                    ],
                    "social_security_social_security_field_six": [
                        ""
                    ],
                    "social_security_social_security_field_seven": [
                        ""
                    ],
                    "social_security_social_security_field_eight": [
                        ""
                    ],
                    "social_security_social_security_field_nine": [
                        ""
                    ],
                    "social_security_social_security_field_ten": [
                        ""
                    ],
                    "extra_content_three_title": [
                        ""
                    ],
                    "extra_content_three_field_one": [
                        ""
                    ],
                    "extra_content_three_field_two": [
                        ""
                    ],
                    "extra_content_three_field_three": [
                        ""
                    ],
                    "extra_content_three_field_four": [
                        ""
                    ],
                    "extra_content_three_field_five": [
                        ""
                    ],
                    "extra_content_three_field_six": [
                        ""
                    ],
                    "extra_content_three_field_seven": [
                        ""
                    ],
                    "extra_content_three_field_eight": [
                        ""
                    ],
                    "extra_content_three_field_nine": [
                        ""
                    ],
                    "extra_content_three_field_ten": [
                        ""
                    ]
                }
            }
};
allQualifiedRetirementPlans (data)

看起来好像在数据对象的"posts"字段中缺少关闭的]

更新

我还包含了在类别上循环的逻辑,以查看对象数组中是否存在段塞。

此外,为了表明简单循环不需要jQuery,我删除了$.each()方法

看看这把小提琴:https://jsfiddle.net/5yyg3req/2/