我的博客中有一个自动收录机代码,我希望在其中显示帖子链接。 我写下面的代码,但the_permalink()和the_title()回显url和title,我的数组填充空值?
$my_query = new WP_Query('showposts=10&offset=0&category_name=allposts');
$i = 0; $post_uris = array(); $post_titles = array();
while ($my_query->have_posts()) : $my_query->the_post();
$post_uris[$i]= '<a href="'.the_permalink().'">'. the_title().'</a>';
$i++;
endwhile;
答案 0 :(得分:0)
您可以使用此代码获取RSS Feed并将其加载到页面中:
$max_posts_to_show = 5;
$rss = new DOMDocument();
$rss->load('http://your-domain.tld/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'cat' => $node->getElementsByTagName('category')->item(0)->nodeValue,);
array_push($feed, $item);
}
$limit = $max_posts_to_show;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y'.'', strtotime($feed[$x]['date']));
$category = $feed[$x]['cat'];
echo'<a href="'.$link.'"title="'.$title.'" target="_blank">'.$title.'</a><br/><br/>';
}
答案 1 :(得分:0)
你的错误,echo $i++;
在while循环中,是否包含任何值,还有echo $post_uris[$i];
$my_query = new WP_Query('showposts=10&offset=0&category_name=allposts');
$i = 0; $post_uris = array(); $post_titles = array();
while ($my_query->have_posts()) : $my_query->the_post();
echo $post_uris[$i]= '<a href="'.the_permalink().'">'. the_title().'</a>';
echo $i++;
endwhile;
如果它包含值,那么循环外的print_r($post_uris)
。但我确信$i
将无法正常工作。
所以它不能创建它的assoc数组。
答案 2 :(得分:0)
我找到了最佳解决方案
$i = 0; $uris = array(); $titles = array();
$args = array( 'posts_per_page' => 10, 'offset'=> 1, 'category' => get_cat_ID( 'allposts' ) );
$myposts = get_posts( $args );
foreach ( $myposts as $post ): setup_postdata( $post );
array_push($uris, '"'.get_permalink( $post->ID).'"');
array_push($titles, '"'.get_the_title( $post->ID).'"');
endforeach;?>
var theSummaries = new Array(<?php echo implode(",",$titles);?>);
var theSiteLinks = new Array(<?php echo implode(",",$uris);?>);