简体   繁体   English

动态拉动Wordpress类别

[英]Pulling Wordpress Categories Dynamically

I hope someone will be able to help me. 我希望有人能够帮助我。 I already know how to pull the most recent post in wordpress using this code: 我已经知道如何使用以下代码在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;?>

This works fine, but I want to extend this functionality. 这工作正常,但我想扩展此功能。 I want to be able to pull posts based on a specific category. 我希望能够根据特定类别发布帖子。 For example, the site I'm working on has a page called "Social Media" with information on the topic. 例如,我正在工作的网站上有一个名为“社交媒体”的页面,其中包含有关该主题的信息。 It also has a blog category of the same name. 它还具有相同名称的博客类别。 I want to know how I can pull the posts from the Social Media section of the blog to the informational Social Media page (and so on - we will have about 10 more categories which will also need their related posts). 我想知道如何将博客的“社交媒体”部分中的帖子拉到信息社交媒体页面上(依此类推-我们将再有10个类别,它们也需要与之相关的帖子)。

Any help would be much appreciated! 任何帮助将非常感激! Thank you! 谢谢!

wp_list_categories can help you in this regard. wp_list_categories在这方面可以为您提供帮助。 Code will be similar to the following one: 代码将类似于以下代码:

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

Detail can be found here. 可以在这里找到详细信息。 http://codex.wordpress.org/Function_Reference/wp_list_categories http://codex.wordpress.org/Function_Reference/wp_list_categories

You could do it with code like this: 您可以使用以下代码来做到这一点:

<?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; ?>

For the get_category part, you could substitute the hard-coded social-media with the $pagename (if it's the same as the category's name). 对于get_category部分,您可以将硬编码的social-media替换为$pagename (如果与类别名称相同)。

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

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