【问题标题】:Wordpress get Custom post titleWordpress 获取自定义帖子标题
【发布时间】:2018-07-21 16:08:15
【问题描述】:

我是 WordPress 编程新手 我创建了一个名为“wp_locations”的 WordPress 邮政仓库 我需要在页面上显示这篇文章的标题 我将以下代码放在我的主题索引文件中

<?php $gallery_args = array(
            'posts_per_page'   => -1,
            'orderby'=> 'date',
            'order'=> 'DESC',
            'post_type'=> 'wp_locations',
            'post_status'=> 'publish',
            'suppress_filters' => true 
    );
$posts_display_gallery = get_posts( $gallery_args ); 
foreach($posts_display_gallery as $rows){
    $post_title = $rows->post_title;
} ?>

但没有显示标题 请指导我

【问题讨论】:

  • 您好,欢迎来到 StackOverflow。请参考stackoverflow.com/help/how-to-ask,了解如何提出正确的问题并根据指南改进您的问题。
  • 你试过echo $post_title吗?
  • 您的代码中缺少“回声”

标签: wordpress


【解决方案1】:

请在您的代码中使用 echo

echo $post_title = $rows->post_title;

【讨论】:

    【解决方案2】:

    请尝试以下代码并再次检查......

     <?php
    
        global $post;
       $gallery_args = array(
                'posts_per_page'   => -1,
                'orderby'=> 'date',
                'order'=> 'DESC',
                'post_type'=> 'wp_locations',
                'post_status'=> 'publish',
                'suppress_filters' => true 
        );
       $posts_display_gallery = get_posts( $gallery_args ); 
        foreach ( $posts_display_gallery as $post ) : setup_postdata( $post ); 
         $post_title = $post->post_title; // Like this you will get post title.
         echo $post_title;  // For display
     endforeach; 
        wp_reset_postdata();?>
    

    希望这对你有帮助。

    【讨论】:

    • 不鼓励仅使用代码的答案,因为它们没有解释如何解决问题中的问题。请更新您的答案以解释它的作用以及它如何解决问题。请查看How do I write a good answer
    • 感谢@FluffyKitten 的评论。我已将答案更新为您的评论。谢谢。
    • 谢谢大家 我忘了echoو我的问题已经解决了
    【解决方案3】:
    <?php $gallery_args = array(
                'posts_per_page'   => -1,
                'orderby'=> 'date',
                'order'=> 'DESC',
                'post_type'=> 'wp_locations',
                'post_status'=> 'publish',
                'suppress_filters' => true 
        );
    $posts_display_gallery = get_posts( $gallery_args ); 
    foreach($posts_display_gallery as $rows){
        $post_title = $rows->post_title;
    
        echo $post_title; // for display the title
    } ?>
    

    【讨论】:

      【解决方案4】:

      你可以试试这段代码,应该可以工作

      $args = array(
              'post_type' => 'product',
              'posts_per_page' => -1
      );
      
      $loop = new WP_Query( $args );
      if( $loop->have_posts() ){
          while ( $loop->have_posts() ) : $loop->the_post();
              global $product;
              $product_id = $loop->post->ID;
              $product_title = $loop->post->post_title;
          endwhile;
      }
      

      【讨论】:

        【解决方案5】:

        在 foreach 循环中尝试 get_the_title()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-04
          • 1970-01-01
          • 2013-10-25
          • 2023-04-04
          相关资源
          最近更新 更多