2017-05-08 31 views
0

我在開發我的插件時遇到過這個問題。 我必須遍歷網站的所有頁面並打印標題和它們的ID。 當我做簡單的循環它只是不打印任何東西。是否可以在wordpress的插件選項頁面內進行循環?

有沒有辦法讓我的插件的選項內循環?

+0

請問您可以發表更多的細節,比如您迄今爲止所嘗試過的代碼? – Ashkar

+1

你試過這個嗎? <?php $ pages = get_pages(); ($ page as $ page){ $ page_title = $ page-> post_title; $ page_id = $ page-> ID; } ?> – Ashkar

+0

@Ashkar是的,我試過這段代碼。它的作品,但不做我要做的事情。這隻適用於頁面。我需要爲每個自定義帖子類型。這是我試過的簡單循環<?php if(have_posts()):while(have_posts()):the_post(); the_title(); the_title(); end; endif; ?> – Gianno

回答

0

找到解決方案。

我不知道是否有某種塊的插件選項頁裏面,但是我們可以通過每一個自定義後類型與代碼的一個小部分循環:

<?php 

    //Search only for custom post type which is public. 
    $args = array('public'=>true); 

    //Get all custom post type name 
    $allMyCustomPostTypes = get_post_types($args); 

    foreach ($allMyCustomPostTypes as $myCustomPostType) { 

     //Create a filter for get_posts with every custom post type 
     $filter = array('numberposts' => 5, 'post_type' => $myCustomPostType); 

     //Pass the value to *get_posts* 
     $theposts = get_posts($filter); 

     foreach ($theposts as $thepost) { 

      //Easy print the name of the current post of the current custom post type 
      echo $thepost->post_title; 

     }; 

    }; 

?> 

我們在這裏,希望這有助於有人。

相關問題