简体   繁体   English

如何将wordpress帖子的网址保存到php数组?

[英]how can i save wordpress posts url to an php array?

i have an ticker JavaScript code in my blog and i want to show post links in it. 我的博客中有一个自动收录器JavaScript代码,我想在其中显示帖子链接。 i write below code but the_permalink() and the_title() echo the url and title and my array fill with empty values? 我写下面的代码,但the_permalink()和the_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;

You can use this code to get the rss feed and load it into the page: 您可以使用以下代码获取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/>';
}

Your while is wrong, echo $i++; 你的一会儿错了, echo $i++; in while loop , does it contain any value or not ,Also echo $post_uris[$i]; 在while循环中,它是否包含任何值,还可以回显$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;

if it contain value then print_r($post_uris) outside the loop. 如果它包含值,则在循环外进行print_r($post_uris) But I am sure $i will not work in while. 但是我确信$i将在一段时间内不起作用。

So it can't make its assoc array. 因此,它无法使其assoc数组。

i find the best solution 我找到最好的解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM