Fixed bookshelf/bookshelf-title.php error

Solution
Ok, I found the fix.
I changed
<?php foreach ( $terms as $term ) : ?> with
PHP:
<?php $terms = get_terms([
    'taxonomy' => 'bookshelf_categories',
    'hide_empty' => false,
]);
            foreach ( $terms as $term ) ...

gauravtiwari

Blogger at https://gauravtiwari.org
Messages
110
Reaction score
42
Website
gauravtiwari.org
Ok, if I remove this code,
templates/bookshelf/bookshelf-title.php:
<?php if ( $terms && ! $has_categories ) : ?>
        <div class="bookshelf-categories">
            <span class="bookshelf-category<?php echo is_post_type_archive( 'bookshelf' ) ? ' is-active' : ''; ?> middot"><a href="<?php echo get_post_type_archive_link( 'bookshelf' ); ?>" class="bookshelf-category-link"><?php echo __( 'All Books', 'md' ); ?></a></span>
            <?php foreach ( $terms as $term ) : ?>
                <span class="bookshelf-category<?php echo $term_id == $term->term_id ? ' is-active' : ''; ?> middot"><a href="<?php echo get_term_link( $term->term_taxonomy_id ); ?>" class="bookshelf-category-link>"><?php echo $term->name; ?></a></span>
            <?php $c++; endforeach; ?>
        </div>
    <?php endif; ?>

Library goes back to normal and good. Maybe something is messed up here.
 
Comment

Alex

MD developer
Messages
5,175
Reaction score
1,261
Do you have any bookshelf categories made? If you create one this error may go away. But it should be fixed so it doesn't break the site.
Ok, if I remove this code,
templates/bookshelf/bookshelf-title.php:
<?php if ( $terms && ! $has_categories ) : ?>
        <div class="bookshelf-categories">
            <span class="bookshelf-category<?php echo is_post_type_archive( 'bookshelf' ) ? ' is-active' : ''; ?> middot"><a href="<?php echo get_post_type_archive_link( 'bookshelf' ); ?>" class="bookshelf-category-link"><?php echo __( 'All Books', 'md' ); ?></a></span>
            <?php foreach ( $terms as $term ) : ?>
                <span class="bookshelf-category<?php echo $term_id == $term->term_id ? ' is-active' : ''; ?> middot"><a href="<?php echo get_term_link( $term->term_taxonomy_id ); ?>" class="bookshelf-category-link>"><?php echo $term->name; ?></a></span>
            <?php $c++; endforeach; ?>
        </div>
    <?php endif; ?>

Library goes back to normal and good. Maybe something is messed up here.
Replace the exact code you posted with the code below. I haven't been able to duplicate the error but this code should check for category terms in a safer way.

PHP:
    <?php if ( ! $has_categories ) : ?>
        <div class="bookshelf-categories">
            <span class="bookshelf-category<?php echo is_post_type_archive( 'bookshelf' ) ? ' is-active' : ''; ?> middot"><a href="<?php echo get_post_type_archive_link( 'bookshelf' ); ?>" class="bookshelf-category-link"><?php echo __( 'All Books', 'md' ); ?></a></span>
            <?php if ( ! empty( $terms ) ) : ?>
                <?php foreach ( $terms as $term ) : ?>
                    <span class="bookshelf-category<?php echo $term_id == $term->term_id ? ' is-active' : ''; ?> middot"><a href="<?php echo get_term_link( $term->term_taxonomy_id ); ?>" class="bookshelf-category-link>"><?php echo $term->name; ?></a></span>
                <?php $c++; endforeach; ?>
            <?php endif; ?>
        </div>
    <?php endif; ?>

This change is already in the next MD version we are releasing very soon.
 
Comment

Alex

MD developer
Messages
5,175
Reaction score
1,261
What is the full error message? It would say something like "Fatal error: {blahblahblah} on line 22". Turn WP_DEBUG on or use Query Monitor.
 
Comment

gauravtiwari

Blogger at https://gauravtiwari.org
Messages
110
Reaction score
42
Website
gauravtiwari.org
Hi @Alex here is the error.

PHP Recoverable fatal error: Object of class WP_Error could not be converted to string in /srv/htdocs/wp-content/themes/marketers-delight/templates/bookshelf/bookshelf-title.php on line 20
 
Comment

gauravtiwari

Blogger at https://gauravtiwari.org
Messages
110
Reaction score
42
Website
gauravtiwari.org
Ok, I found the fix.
I changed
<?php foreach ( $terms as $term ) : ?> with
PHP:
<?php $terms = get_terms([
    'taxonomy' => 'bookshelf_categories',
    'hide_empty' => false,
]);
            foreach ( $terms as $term ) : ?>

I think this is error-proof and former argument was trying to load all terms from the website.
 
Last edited:
Comment
Solution
Top