【问题标题】:Pulling Wordpress Categories Dynamically动态拉取 Wordpress 类别
【发布时间】:2012-12-27 17:08:41
【问题描述】:

我希望有人能够帮助我。我已经知道如何使用以下代码在 wordpress 中提取最新帖子:

<?php query_posts('posts_per_page=1'); ?>
<?php while (have_posts()): the_post(); ?> 
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<?php the_excerpt(); ?>
<?php endwhile;?>

这很好用,但我想扩展此功能。我希望能够根据特定类别提取帖子。例如,我正在处理的网站有一个名为“社交媒体”的页面,其中包含有关该主题的信息。它还有一个同名的博客类别。我想知道如何将帖子从博客的社交媒体部分拉到信息社交媒体页面(等等 - 我们将有大约 10 个类别也需要它们的相关帖子)。

任何帮助将不胜感激!谢谢!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    wp_list_categories可以在这方面为您提供帮助。代码将类似于以下代码:

    <ul>
        <?php wp_list_categories('orderby=books&include=2'); ?>
    </ul>
    

    详情可以在这里找到。 http://codex.wordpress.org/Function_Reference/wp_list_categories

    【讨论】:

      【解决方案2】:

      你可以用这样的代码来做:

      <?php $social_media_id = get_category( 'social-media', ARRAY_A ); ?>
      <?php $social_media = query_posts( 'posts_per_page=1,category=' . $social_media_id['cat_ID'] ); ?>
      <?php while (have_posts()): the_post(); ?> 
      <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
      <?php the_excerpt(); ?>
      <?php endwhile; ?>
      

      对于get_category 部分,您可以将硬编码的social-media 替换为$pagename(如果它与类别名称相同)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多