【问题标题】:PHP/WP concatenation - string ends up in two partsPHP/WP 连接 - 字符串分为两部分
【发布时间】:2014-11-20 16:58:38
【问题描述】:
$args = array( 'post_type' => 'object', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    echo "<div class=\"item\"><a href=\"" . the_permalink() . "\">" . the_title() . "</a></div>";
endwhile;

我在 WP 中的这个循环没有回显

<div class="item"><a href="#mylink">The name of my link</a></div>

而是

#mylink The Name of my link
<div class="item"><a href=""></a></div>

我错过了一些琐碎的事情吗?

【问题讨论】:

  • 这里,既然您没有与任何变量链接,那么在 while 循环内回显有什么用?
  • 它正在返回正确的链接和我的链接的正确名称...CPT 名称是 Object 是什么意思?
  • @Indra 抱歉,编辑了问题。
  • @NabilKadimi 是的,我正在尝试循环浏览我创建的自定义帖子类型的帖子。

标签: php wordpress concatenation


【解决方案1】:

我会这样做(下面的代码),只有在以下情况下才会显示:

  • 至少有一篇已发布帖子类型为object
new WP_Query( array(
    'post_type' => 'object',
    'posts_per_page' => 10,
) );

while ( have_posts() ) :
    the_post();
    printf(
        '<div class="item"><a href="%s">%s</a></div>'
        , get_permalink()
        , get_the_title()
    );
endwhile;

wp_reset_query();

注意事项:

【讨论】:

    【解决方案2】:

    尝试像这样回显

       echo '<div class="item"><a href="'.$mylink.'">'.$myTitle.'</a></div>';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      相关资源
      最近更新 更多