Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement on Chapter Title System? #23

Closed
bravking opened this issue Feb 29, 2024 · 3 comments
Closed

Improvement on Chapter Title System? #23

bravking opened this issue Feb 29, 2024 · 3 comments
Labels
customization Can be achieved with actions, filters, and some CSS.

Comments

@bravking
Copy link

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
Copy link
Owner

Tetrakern commented Mar 1, 2024

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 Tetrakern added the customization Can be achieved with actions, filters, and some CSS. label Mar 1, 2024
@Tetrakern
Copy link
Owner

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

@Tetrakern
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customization Can be achieved with actions, filters, and some CSS.
Projects
None yet
Development

No branches or pull requests

2 participants