Php Wordpress:get&;商店类别信息
我想知道:有没有什么方法可以加载所有类别并将每个类别存储在不同的数组中 所以我得到了这个代码:Php Wordpress:get&;商店类别信息,php,wordpress,Php,Wordpress,我想知道:有没有什么方法可以加载所有类别并将每个类别存储在不同的数组中 所以我得到了这个代码: <?php $cat_args = array('orderby' => 'name','order' => 'ASC'); $categories = get_categories($cat_args); foreach($categories as $category) { $args = array( 'showposts' => -
<?php
$cat_args = array('orderby' => 'name','order' => 'ASC');
$categories = get_categories($cat_args);
foreach($categories as $category) {
$args = array(
'showposts' => -1,
'category__in' => array($category->term_id),
'caller_get_posts' => 1
);
$posts = get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post);
?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
注意:在$categories变量中已经有了这种格式的数据。
这样你就可以在任何地方使用
<?php echo $categories[0] -> name . $categories[1] -> name . $categories[2] -> name; ?>
如果您只需要一个类别名称数组,您可以按如下方式更改代码;(请注意:caller\u get\u posts
和showposts
在几年前已经贬值。它们分别被ignore\u sticky\u posts
和posts\u per\u page
取代)
请注意:来电者获取帖子
和展示帖子
几年前已经贬值。它们分别被忽略粘性帖子
和每页帖子
取代
<?php echo $categories[0] -> name . $categories[1] -> name . $categories[2] -> name; ?>
<?php
$cat_args = array('orderby' => 'name','order' => 'ASC');
$categories = get_categories($cat_args);
$category_names = array();
foreach($categories as $category) {
$category_names[] = $category->name;
$args = array(
'posts_per_page' => -1,
'category__in' => array($category->term_id),
'ignore_sticky_posts' => 1
);
$posts = get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post);
?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>