Tetrakern / fictioneer

A web fiction theme for WordPress.
https://fictioneer-theme.com
GNU General Public License v3.0
104 stars 16 forks source link

Improvement on Chapter Title System? #23

Closed bravking closed 7 months ago

bravking commented 7 months ago

Hi tetrakern. Just wanna say that I appreciate everything you've been doing with Fictioneer so far. However, I do have one issue regarding the chapter title system. I'll start with what I did with my site first.

image

If you look at pic above, I prefer naming my chapters with [Series Acronym - see highlighted yellow] followed with whatever chapter number. Hwv, i don't really like to include the chapter title since it can be really long sometimes and partly because its easier for me to navigate the hundreds, if not thousands of chapters I have on my site. But! this leads to an issue that i have now.

While i don't want to include the chapter text title in main title box, and its slug... i do want to write the title somewhere else (not in the content box), preferably in a separate metabox so that it'll be reflected on both chapter page and stories page.

What I mean is something like this which was screenshot from other theme that i have (refer Pic 2, Pic 3 and Pic 4)

Pic 2: Chapter Text Title Metabox

image

Pic 3: Title reflected on chapter page

image

Pic 4: Title reflected on stories page

image

Is this something that you could consider to work on? Or is there a roundabout way I can do this?

Thanks in advance for any advice or help!

Tetrakern commented 7 months ago

Thank you for using Fictioneer, I hope you can make use of it.

Regarding your request, this is not an improvement but an erratic feature. Which is perfectly fine, of course. Display your content how you like. But I cannot bloat the theme further with features barely anyone needs, there are too many of those already. And someone will always come with another, where would this end?

Anyway, you are in luck, because not only can this be achieved (obviously) but also with relative ease. Well, you do need a child theme for some custom code. If you check "Enabled advanced meta fields" under Fictioneer > General > Compatibility, you will be able to use the "Short Title" field in the chapter meta box. This is not actually used by the theme, it is meant for child theme customizations and pretty much the opposite of what you need. But a field is a field.

image

Put this into your child theme's functions.php:

function child_modify_chapter_header( $identity, $args ) {
  // Setup
  $password_class = empty( $args['chapter_password'] ) ? '' : ' _password';
  $other_title = get_post_meta( $args['chapter_id'], 'fictioneer_chapter_short_title', true );
  $icon_classes = fictioneer_get_icon_field( 'fictioneer_chapter_icon', $args['chapter_id'] );
  $icon = '';
  $meta = $identity['meta'];

  // Abort if second title is not set
  if ( empty( $other_title ) ) {
    return $identity;
  }

  // Prepend icon (if any)
  if ( ! empty( $icon_classes ) ) {
    $icon = '<i class="' . $icon_classes . '"></i> '; // Mind the space afterwards
  }

  // Temporary remove meta row
  unset( $identity['meta'] );

  // Rebuild HTML
  $identity['title'] = '<h1 class="chapter__title' . $password_class . '">' . $icon . $args['chapter_title'] . '</h1>';
  $identity['other_title'] = '<h2 class="chapter-second-title">' . $other_title . '</h2>';
  $identity['meta'] = $meta;

  // Continue filter
  return $identity;
}
add_filter( 'fictioneer_filter_chapter_identity', 'child_modify_chapter_header', 10, 2 );
image

You may need to add some custom style targeting the "chapter-second-title" CSS class. The chapter header is not set up for a second title. Why would it? I also prepended the chapter icon, in case you want that.

Modifying the title on the story page (or elsewhere) is a bit more tricky, at least until the next update 5.12.0 which will add a context parameter to the fictioneer_filter_safe_title filter. The update is still a bit off, but not much. Alternatively, you could check whether the current template is the "single-fcn_story.php" and the $post_id belongs to the type "fcn_chapter", but that is not guaranteed to only hit the chapter list under certain circumstances. You should wait for the update (this code will throw an error until then).

function child_modify_chapter_list_title( $title, $post_id, $context ) {
  // Only for chapter lists (story pages and shortcode)
  if ( ! in_array( $context, ['story-chapter-list', 'shortcode-chapter-list'] ) ) {
    return $title;
  }

  // Setup
  $other_title = get_post_meta( $post_id, 'fictioneer_chapter_short_title', true );

  // Abort if second title is not set
  if ( empty( $other_title ) ) {
    return $title;
  }

  // Append to title
  $title = $title . ' — ' . $other_title;

  // Continue filter
  return $title;
}
add_filter( 'fictioneer_filter_safe_title', 'child_modify_chapter_list_title', 10, 3 );
image

Little extra advice: Be careful with adding meta fields, especially if you did not disable post revisions. Each field adds to your post_meta database table, and each revision adds a duplicate of that. So if you have a meta field for 1000 chapters, and each has at least three revisions (you updated it just two times), that means you now got +3000 rows in that database table. Now imagine using even more meta fields. Eventually, this will slow down your site.

Fictioneer has been built with that in mind and only saves what's actually needed. But there's a limit to what I can do. Anyway, I recommend turning off post revisions if you did not already.

Tetrakern commented 7 months ago

With 5.12.0 released, this should now be good to go.

Tetrakern commented 7 months ago

Seeing how there's no response coming, I'm closing this now.