dtxooq1020 2014-09-23 02:58
浏览 28
已采纳

如何使用帖子ID或页面固定链接在选定的wordpress页面中发布某个帖子类别?

I have created a news dispatches post category. I also created a news releases page, this is the page where I want to show my news dispatches post category posts. How can I show my news dispatches posts in news releases page using post ID or using page permalink in a query? Here is my example code.

add_action('pre_get_posts', 'ad_filter_categories');

   function ad_filter_categories($query) {
      if ($query->is_main_query() && is_home()) {
      $query->set('category_name','news dispatches');
   }
  }

How can I replace the is_home code line with the page/post ID or the page permalink? Thanks for the help.

  • 写回答

1条回答 默认 最新

  • douhang1913 2014-09-23 03:41
    关注

    Can't use is_page in pre_get_posts (see http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts).

    So in the news releases page, add this in:

    // Modify the page query
    query_posts( 'category_name=news-dispatches');    
    
    // The Loop
    if ( have_posts() ) {
        echo '<ul>';
        while ( $have_posts() ) {
            the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    

    UPDATE: To answer this question: "What php file I am gonna look into to insert this code? ":

    It's hard to give exact instructions because it really depends on how your theme is setup.

    You need to create a page called page-news-releases.php. See http://codex.wordpress.org/images/1/18/Template_Hierarchy.png for an explanation of page templates. So when wordpress shows the page with the slug news-releases, it will use this file instead of the default.

    You'll put my code in this new page for the main loop. You'll also need to look at your other theme files and copy code that needs to go before and after it (for your header and footer and that sort of stuff). It's a little out of the scope of stackoverflow to teach wordpress template design, but hopefully this will get you started. Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题