Novelist_Book

⌘K
  1. Home
  2. Developers
  3. Classes
  4. Novelist_Book

Novelist_Book

This class is used for getting information about a specific book (or the current book on the page).

Properties

  • $ID – Post ID of the current book.
  • $title – Title of the book (as entered in the designated “Book Title” field).
  • $cover_ID – Attachment ID of the cover image.
  • $series_name – Name of the series with the number (below) appended.
  • $series_number – Numerical position in the series.
  • $publisher – Name of the publisher.
  • $pub_date – Publication date.
  • $contributors – List of contributors.
  • $genre – Comma-separated list of genres.
  • $pages – Number of pages in the book.
  • $isbn13 – ISBN13
  • $asin – ASIN
  • $synopsis – Book synopsis.
  • $goodreads_link – URL to the Goodreads page. This is just the URL – not a formatted link.
  • $purchase_links – Array of purchase links.
  • $series_books – WP_Query object for other books in the series.
  • $excerpt – Excerpt.
  • $extra_text – Extra text.

Getting information about a book

First you want to create a new instance of the Novelist_Book class. You can either manually pass in a book ID number, or use get_post_ID() to get the current book in the loop.

$book = new Novelist_Book( get_the_ID() );

Then you can use any of the properties to fetch and display book information. For example:

<h1><?php echo $book->title; ?></h1>
<?php echo wpautop( $book->synopsis ); ?>
<p><a href="<?php echo esc_url( $book->goodreads_link ); ?>" target="_blank"><?php _e( 'Add on Goodreads' ); ?></a></p>

Public Methods

get_cover_image( $size = ‘full’, $class = ”, $link = false )

Returns an HTML formatted <img> tag of the book cover.

  • $size (string) – Size of the image. Accepts any standard WordPress size (full | large | medium | thumbnail | post-thumbnail).
  • $class (string) – Class name(s) to add to the <img> tag.
  • $link (bool) – Whether or not to link the cover image to its designated location (as specified in the Book Layout settings).
get_purchase_links_list( $list_type = ‘ul’ )

Returns an HTML list (ul or ol) of purchase links.

  • $list_type (string) – List type to use. Should be ‘ol’ or ‘ul’. You can also pass through an empty string to not include the <ul> or <ol> tags. Doing so will return <li> tags only.
purchase_links_list( $list_type = ‘ul’ )

Displays the HTML list (ul or ol) of purchase links.

  • $list_type (string) – List type to use. Should be ‘ol’ or ‘ul’. You can also pass through an empty string to not include the <ul> or <ol> tags. Doing so will return <li> tags only.
get_series_books_grid()

Returns the image grid of other books in the same series as the current book.

get_series_books_links()

Returns a comma-separated text list of other books in the same series as the current book.

render( $key = ”, $enabled_fields = false )
  • $key – Property to display. i.e. ‘cover’, ‘series’, ‘purchase_links’, ‘title’, etc.
  • $enabled_fields – Array of book info fields that are enabled and their corresponding templates. If omitted, the method will fetch this information from the database.

Renders a piece of book information for display on the front-end of the site. This method is responsible for getting the value of the field and inserting it into template (as specified in the Book Layout settings).

An example of this method in action can be found in templates/book-content.php :

$enabled_fields = novelist_get_option( 'book_layout', novelist_get_default_book_field_values() );
$book           = new Novelist_Book( get_the_ID() );
?>
<div itemscope itemtype="http://schema.org/Book">
	<?php foreach ( $enabled_fields as $key => $value ) :
		$book->render( $key, $enabled_fields );
	endforeach; ?>
</div>

This piece of code cycles through all the enabled book fields and renders the values of each.

Was this article helpful to you? No Yes

How can we help?