简体   繁体   English

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

[英]How to post a certain post category in a selected wordpress page using post ID or page permalink?

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? 如何使用帖子ID或查询中的页面永久链接在新闻发布页面中显示我的新闻发布帖子? 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? 如何用页面/帖子ID或页面永久链接替换is_home代码行? Thanks for the help. 谢谢您的帮助。

Can't use is_page in pre_get_posts (see http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts ). 无法在pre_get_posts中使用is_page(请参阅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? ": 更新:要回答这个问题:“我要插入哪个php文件插入此代码?”:

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. 您需要创建一个名为page-news-releases.php的页面。 See http://codex.wordpress.org/images/1/18/Template_Hierarchy.png for an explanation of page templates. 有关页面模板的说明,请参见http://codex.wordpress.org/images/1/18/Template_Hierarchy.png So when wordpress shows the page with the slug news-releases, it will use this file instead of the default. 因此,当wordpress显示带有最新新闻稿的页面时,它将使用此文件而不是默认文件。

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. 教wordpress模板设计超出了stackoverflow的范围,但希望这可以帮助您入门。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM