2015-05-06 65 views
0

我在我的博客中有一個股票代碼,我想顯示其中的鏈接。 我寫下面的代碼,但the_permalink()和the_title()回顯URL和標題和我的數組填充空值?我怎麼能保存Wo​​rdPress的帖子URL到PHP數組?

$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

感謝您的答覆,我得到它 – RezaOnline

+0

get_posts()是答案的關鍵 – RezaOnline

回答

0

您可以使用此代碼來獲取RSS源並將其加載到頁面:

$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(' & ', ' &amp; ', $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/>'; 
} 
0

你同時是錯誤的,而循環,它包含任何值echo $i++;與否,還回聲$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數組。

+0

喜坦克,但我不希望任何呼應的東西,我只需要保存一個字符串,它包含一個數組中的錨點標籤,但是wordpress會在頁面中打印這些值,我不想要它 – RezaOnline

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