dtrpv60860 2014-12-28 21:38
浏览 37

如何按类别获取Wordpress帖子?

It's a bit more complicated than that.

For a travel website, I have 3 parent categories: Series, Post Type, and Location.

Each post is assigned to a child category. For example (respectively): EU2014, Picture Gallery, and Rome.

On each post, there is a sidebar. My intention is that this sidebar will contain links to the other related posts. A post is related if it is:

1.) In the same Series child category (such as "EU2014") AND 2.) in the same Location child category (let's say "Rome").

Really what I'm doing here is making it so that way the client can write any number of posts, and a network of links will appear on all of them (in the sidebar).

So, in sum, all posts designated "EU2014" AND "Rome" will be part of a collection.

Then I can format the sidebar nicely based on what Post Type a post in question is (with appropriate icons or whatnot).

My problem is this:

I need pseudo code. I've started working on it, but I'm very concerned that, if I am to iterate through the entire database of posts every time I load a post, simply to write that sidebar, it will be a massive resource drain on the system.

How can I

1.) Identify the child categories of the current post and 2.) get the names and links of each post whose Series and Location child categories match those of the current post in order to write that information to the sidebar of the current post

without creating a black hole of fuckery when the site grows to more than just a few total posts?

This is going to be written in PHP, so if anyone has any interest in helping me figure out more than just pseudo, I'd be thrilled to suss it out with you.

  • 写回答

1条回答 默认 最新

  • douzhanglu4591 2014-12-29 04:29
    关注
    <?php 
       $args = array(
        'posts_per_page'   => -1,
        'offset'           => 0,
        'category'         => 8, // specify category id here
        'orderby'          => 'post_date',
        'order'            => 'DESC',
        'post_type'        => 'post',
        'post_parent'      => '',
        'post_status'      => 'publish',
        'suppress_filters' => true ); 
    
        $myposts = get_posts( $args ); 
    
        foreach( $myposts as $post )
        {
          setup_postdata( $post );
          ?>
          <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
          <?php
        }
        wp_reset_postdata();
    ?>
    

    For Reference see this http://codex.wordpress.org/Template_Tags/get_posts

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教