Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress:get&;商店类别信息_Php_Wordpress - Fatal编程技术网

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