Clarification about Custom Posts, Loop, Action Hooks & Templates
-
Hi, first of all this is a really well thought theme with endless possibilities. That’s probably why I don’t know how to do things properly, I’m struggling with the code.
I’ve made a Custom Post type “books” with Types plugin. When testing, I’ve been able to show those posts with their Custom Fields on the index page, no problem. But I don’t understand how to show them in posts or pages. So I need some clarification because I’ve tried doing it with several methods, but nothing seems to work. Obviously I don’t have coding skills, but I’ve tried. Here’s briefly what I try to do:
– I have a child theme with its index page showing those custom posts
– I think I need to have a single-book.php page (is that correct?) that calls a loop-book.php page (correct?) which will have the following code:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<ul>
<li><strong>ISBN: </strong><?php echo(types_render_field( "isbn", array( 'raw' => true) )); ?></li>
<li><strong>Author: </strong><?php echo(types_render_field( "author", array( 'raw' => true) )); ?></li>
<li><strong>Title: </strong><?php echo(types_render_field( "title", array( 'raw' => true) )); ?></li>
<li><strong>Part of a Series: </strong><?php echo(types_render_field( "series", array( 'raw' => true) )); ?></li>
<li><strong>Name of the Series: </strong><?php echo(types_render_field( "series-name", array( 'raw' => true) )); ?></li>
<li><strong>Year of publishing: </strong><?php echo(types_render_field( "year-of-publishing", array( 'raw' => true) )); ?></li>
<li><strong>Language: </strong><?php echo(types_render_field( "language", array( 'raw' => true) )); ?></li>
<li><strong>Awarded with: </strong><?php echo(types_render_field( "awards", array( 'raw' => true) )); ?></li>
</ul>
<?php endwhile; endif; ?>But of course it doesn’t work. I don’t know if it is better to add an action hook somewhere with some php code made with the PHP Widget plugin or build a special template “template-book.php” on my child theme. If so, how do I write the code on it for the loop to work with those custom posts / fields?
As you can see, I need some clarification on the better and most efficient way to do these kind of things. But I think this may also help others trying to accomplish similar things.
Thanks in advance
Update:
The only way I’ve managed to put my code to be output is by creating a loop-book.php file in the child theme, then copy-paste loop-single.php on it. And then inserting the code like so:
<div class="entry-content clearfix">
<?php do_action( 'graphene_before_post_content' ); ?>
<?php /* Social sharing buttons at top of post */ ?>
<?php if ( stripos( $graphene_settings['addthis_location'], 'top' ) !== false ) { graphene_addthis( get_the_ID() ); } ?>
<?php /* The full content */ ?>
<?php the_content(); ?>
<ul>
<li><strong>ISBN: </strong><?php echo(types_render_field( "isbn", array( 'raw' => true) )); ?></li>
<li><strong>Author: </strong><?php echo(types_render_field( "author", array( 'raw' => true) )); ?></li>
<li><strong>Title: </strong><?php echo(types_render_field( "title", array( 'raw' => true) )); ?></li>
<li><strong>Part of a Series: </strong><?php echo(types_render_field( "series", array( 'raw' => true) )); ?></li>
<li><strong>Name of the Series: </strong><?php echo(types_render_field( "series-name", array( 'raw' => true) )); ?></li>
<li><strong>Year of publishing: </strong><?php echo(types_render_field( "year-of-publishing", array( 'raw' => true) )); ?></li>
<li><strong>Language: </strong><?php echo(types_render_field( "language", array( 'raw' => true) )); ?></li>
<li><strong>Awarded with: </strong><?php echo(types_render_field( "awards", array( 'raw' => true) )); ?></li>
</ul>
<?php wp_link_pages( array( 'before' => '<div class="link-pages"><p><strong>' . __( 'Pages:','graphene' ) . '</strong> ', 'after' => '</p></div>', 'next_or_number' => 'number' ) ); ?>
<?php do_action( 'graphene_after_post_content' ); ?>
</div>I understand this is not correct in any way, but it shows the data that I need. I would like to know how to do it the correct and efficient way.
Thanks
Mod
I’m still not entirely sure what you are trying to accomplish. From your description, I can’t understand why simply using a WordPress category won’t work for you.
Well, as I said I’m trying to make Graphene show my custom fields / custom posts.
In this example, they are books. But I have to make some different custom posts types for my projects.
This doesn’t have anything to do with categories or normal posts/pages.
As you can see, the code is intended to make my custom fields appear into a Custom Post type named “books”.
The problem is that every explanation about making they appear gives an example of themes with basic / typical single.php and page.php files, which usually have the WordPress loop inserted in a very clear way.
In the case of Graphene, one has to go to loop-single and loop-page, but even in those files there’s no easy way to find where or how to put a code which in other more simple themes is easier. So I would like to know which is the best or more efficient way to do it, wether it needs a new specific php file (single-book.php – loop-book.php) on my child theme or in a function inserted via an action hook somewhere.
I hope this is more clear, thanks Kenneth.
Viewing 4 posts - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.
