我正在尝试修改我的循环以按类别保存帖子数据。我在这个网站上找到了一些代码,这些代码根据文章的类别保存了文章的标题,并试图修改这些代码来保存文章的内容。然而,尽管get_the_title
和get_the_category
工作get_the_content
返回null。
代码如下:
if ( false === ( $q = get_transient( \'category_list\' ) ) ) {
$args = array(
\'posts_per_page\' => -1
);
$query = new WP_Query($args);
$q = array();
$body = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = \'<a href="\'. get_permalink() .\'">\' . get_the_title() .\'</a>\';
$post_id = get_the_ID();
$post_id = $post->ID;
$body[$post_id] = array();
$body[$post_id][\'title\'] = \'<a href="\'. get_permalink() .\'">\' . get_the_title() .\'</a>\'; //works
$body[$post_id][\'content\'] = get_the_content(\'Read more\');
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = \'<a href="\' . get_category_link( $category ) . \'">\' . $category->name . \'</a>\';
}
$q[$b][] = $post_id; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
set_transient( \'category_list\', $q, 12 * HOUR_IN_SECONDS );
}
编辑:以下是我如何使用
$body
阵列:
foreach($q[$b] as $post) {
echo(\'<div class="teaser"><div class="teaser-title"><a href = "">\' . $post . \'</a></div><div class="teaser-text">\'. $body[$post] . \'</div><div class="teaser-footer"><p>pemsource.org</p></div></div>\');
}
Edit2:我添加了完整的代码。当我对body进行var转储时,我得到NULL,当我对$q进行var转储时,我得到
array(3) { ["Conundrums"]=> array(1) { [0]=> string(64) "new post" } ["Tips and Tricks"]=> array(1) { [0]=> string(80) "Tips and tricks" } ["Uncategorized"]=> array(1) { [0]=> string(78) "Tips and Tricks" } }
似乎不管我如何编辑循环。我很困惑。非常感谢您的帮助