0

I use latest WordPress and I went to GenerateWP and created a new custom post type named "book_pages". I pasted the output in the end of my functions.php. The post-type is available and I can work with it in my site.

I then went to "pages" and added a new page named "all book pages".

I'd like to show trimmed version of all nodes of the "book_pages" CPT under "all book pages".

It should be seem like this:

enter image description here


Basically I need just the headline of each webpage to appear in each box under "All book pages" (and each box will link to the relevant book_page).

I'll further style the boxes with CSS.

But I only need to know how to actually print all nodes of my "book_pages" CPT in the "All book pages" page.

Notes:

  • You likely noticed that in this book, each page is actually a chapter-page.
8
  • I think you can use get_posts() function. Use book_pages as post_type parameter.
    – nmr
    Commented Aug 29, 2018 at 13:12
  • 2
    This is a rather unusual way to achieve your goal. When you create a CPT, you can have it automatically create an archive, which is similar to Post Categories in that it typically shows the link, title, and sometimes excerpt of a group of posts within the category. You can set the URL of the archive to whatever you want it to be when you register the CPT.
    – WebElaine
    Commented Aug 29, 2018 at 13:12
  • @WebElaine I might miss that but how do you print the archive inside a given webpage? I'ld like to print such archive as you mention, directly inside the "All books page" page, and then style with CSS (because if it'll give me the heading and the entire element representing a webpage is anchored to it's book_page than that's fine for me).
    – Ben
    Commented Aug 29, 2018 at 13:27
  • 2
    Template hierarchy. You can copy archive.php to archive-book_pages.php and modify the content (display only post title with link). Archive page will replace your "All books page".
    – nmr
    Commented Aug 29, 2018 at 13:32
  • @nmr this would require me to use a sub-theme otherwise the new file would be deleted in a theme update right? I hate sub-themes, and I don't want to change file permissions. Is there no GUI approach in this case?
    – Ben
    Commented Aug 29, 2018 at 13:42

1 Answer 1

0

You could use get_posts() to get posts of a particular type, then use a for loop to print out links, titles, etc as required.

Maybe put this in a custom template for your "All book pages" page. More info on templates here.

Example:

$posts = get_posts(array(
    "post_type"=>"book_pages",
    "post_status"=>"publish",
    "posts_per_page"=>-1 
));

if ($posts && count($posts)>0) 
{
    global $post;
    foreach ($posts as $post)
    {
        setup_postdata($post);
        the_title(); //prints out the title of this post
    }
    wp_reset_postdata(); 
}
4
  • Thanks. Please see a small update in the question.
    – Ben
    Commented Aug 29, 2018 at 13:21
  • @Ben thanks, from what I can see this answer still fits - if it's not hitting the mark for you could you elaborate on the requirements? The way to show posts inside another post is either this with a custom template, or using an archive as suggested in the comments.
    – jammypeach
    Commented Aug 29, 2018 at 13:51
  • You could also create an shortcode with a query like that from @jammypeach.
    – LWS-Mo
    Commented Aug 29, 2018 at 13:55
  • Putting a query with an array is best for me as I hate creating sub-themes. I should learn how a query can be implemented inside a shortcode.
    – Ben
    Commented Aug 29, 2018 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.