Wordpress获得自定义帖子标题

时间:2017-09-15 09:04:37

标签: wordpress

我是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;
} ?>

但没有显示标题 请指导我

5 个答案:

答案 0 :(得分:0)

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

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

希望这对你有所帮助。

答案 1 :(得分:0)

请在代码中使用echo

echo $post_title = $rows->post_title;

答案 2 :(得分:0)

<?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
} ?>

答案 3 :(得分:0)

你可以试试这段代码,应该可以吗

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

答案 4 :(得分:0)

在foreach循环中尝试get_the_title()