2017-09-15 66 views
0

我是新來的WordPress編程 我創建了一個名爲「wp_locations」 我需要證明這個帖子 網頁上的標題,我把下面的代碼在我的主題指數WordPress的郵政倉庫文件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; 
} ?> 

但沒有顯示標題 請指導我

+1

您好,歡迎StackOverflow上。請參閱https://stackoverflow.com/help/how-to-ask,瞭解如何根據指南提出正確的問題並改進您的問題。 –

+1

你有沒有試過'echo $ post_title'? – purvik7373

+0

缺少來自您的代碼的'echo' – Arun

回答

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

希望這會對你有所幫助。

+0

僅限代碼答案是因爲他們沒有解釋他們如何解決問題中的問題而不鼓勵。請更新您的答案,以解釋這是什麼以及如何解決問題。請回顧[如何寫出一個好的答案](https://stackoverflow.com/help/how-to-answer) – FluffyKitten

+0

感謝評論@FluffyKitten。我已將您的評論更新爲答案。謝謝。 –

+0

謝謝大家 我忘了回聲و我的問題已解決 –

0

請使用echo在你的代碼

echo $post_title = $rows->post_title; 
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 
} ?> 
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; 
} 
相關問題