【问题标题】:how can i save wordpress posts url to an php array?如何将 wordpress 帖子 url 保存到 php 数组?
【发布时间】:2015-07-16 08:20:06
【问题描述】:

我的博客中有一个股票代码 JavaScript 代码,我想在其中显示帖子链接。 我写了下面的代码,但 the_permalink() 和 the_title() 回显了 url 和标题,我的数组填充了空值?

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

【问题讨论】:

  • 感谢您的回复,我知道了
  • get_posts() 是关键

标签: php wordpress post permalinks


【解决方案1】:

您可以使用此代码获取 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/>';
}

【讨论】:

    【解决方案2】:

    你的while错了,echo $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;
    

    如果它包含值,则 print_r($post_uris) 在循环外。但我确信$i 暂时不会工作。

    所以它不能创建它的 assoc 数组。

    【讨论】:

    • 喜坦克回答,但我不想回显任何东西,我只需要在数组中保存一个包含锚标记的字符串,但 wordpress 打印页面中的值,我不想要它
    【解决方案3】:

    我找到了最好的解决方案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      相关资源
      最近更新 更多