0

I'm stuck and grateful for any help

I have a number of categories and a site 'about' page

I also have 'about pages' for each of the categories (sort of subject intro pages)

The about pages are all in the same category so that the direction arrows link from one to the other though the subjects also have their own post categories

I'm wanting the site about page to have no sidebar and each of the subject's 'about pages' to have a single line sidebar with a 'view posts' link to their respective posts

Below is an example of what I've come up with so far - please don't be horrible, at least I tried!

I'm no coder and I have no particular interest in jelly, custard or blancmange - they're just examples

I hope this makes some sense and thanks in advance for any help

    <?php if(is_page('about')) {

    elseif(is_page('about&nbsp;jelly')){$category_id = get_cat_ID('jelly');
    $category_link = get_category_link($category_id);}

    elseif(is_page('about&nbsp;custard')){$category_id = get_cat_ID('custard');
    $category_link = get_category_link($category_id);}

    elseif(is_page('about&nbsp;blancmange')){$category_id = get_cat_ID('blancmange');
    $category_link = get_category_link($category_id);}

    } ?>

    '<div class="sidebar-float"><br><h3><a href="<?php echo esc_url($category_link) ?>" title="view&nbsp;posts">&nbsp;&nbsp;view&nbsp;posts&nbsp;&#9658;</a></h3><br></div>'

2 Answers 2

0

Instead of hard-coding the pages, categories and sidebars in your template file(s) you could consider registering a custom metabox to store the category relation (i.e. the category term ID) in the page post meta. This way you could make the sidebar handling dynamic.

The first step would be to register, render and handle the saving of a custom metabox. There are decent user-submitted examples on the add_meta_box() documentation page, which you can check. There are also plenty of tutorials in the web and answers here in WPSE regarding metaboxes.

Instead of using the generic add_meta_boxes and save_post hooks I usually use the post type specific ones, add_meta_boxes_{$post_type} and save_post_{$post->post_type}, for a cleaner result.

For rendering a category select within the metabox you can use for example wp_dropdown_categories().

As a sidenote, add_meta_box() could be considered the "traditional" way of doing things. If you're feeling adventurous, another options is to use a combination of PHP and JS to do the meta data handling with the block editor.

Once you got the data saving covered the second step is to retrieve the saved category ID and render the sidebar content. In essence it is a combination of get_post_meta() and get_term_by().

Here's a simple example.

// Helpers in functions.php
function my_page_category_term_id(int $post_id): int {
    $term_id = get_post_meta($post_id, 'my_metakey_for_category_term_id', true);

    return $term_id && is_numeric($term_id) ? (int) $term_id : 0;
}

function my_page_category_term(int $post_id): ?WP_Term {
    $term_id = my_page_category_term_id($post_id);

    return $term_id ? get_term_by('term_id', $term_id, 'category') : null;
}

function my_page_category_link(int $post_id): string {
    $term = my_page_category_term($post_id);
    if (! $term) {
        return '';
    }

    return sprintf(
        '<a href="%s">View %s posts</a>',
        esc_url(get_term_link($term)),
        esc_html($term->name)
    );
}

// E.g. in page.php template
$category_link = my_page_category_link(get_the_ID());
if ($category_link) {
    printf(
        '<aside class="sidebar-float">
            <h3>%s</h3>
        </aside>',
        $category_link
    );
}
1
  • Hi Antti and thank you for this - I had never heard of metaboxes before. I have a local copy of my theme and I'm going to spend some time 'playing' with the stuff you advise. It might be a while but I'll update the post when I have some results
    – PLinB
    Jun 27, 2023 at 10:54
0

Okay, so I was hoping to have a single 'scripted' sidebar but for now at least, I remain stuck - presumably because the sidebar is outside of the 'loop' (like I say, I'm no coder).

For anyone that may have an interest, I've resolved it by writing the script into the page.php

<?php if (is_page('about&nbsp;jelly')){get_template_part('sidebar/about-jelly');}
elseif (is_page('about&nbsp;custard')){get_template_part('sidebar/about-custard');}
elseif (is_page('about&nbsp;blancmange')){get_template_part('sidebar/about-blancmange');} ?>

and then creating different sidebars for each subject - jelly sidebar example:

<?php $category_id = get_cat_ID('jelly');
$category_link = get_category_link($category_id); ?>
<div class="sidebar-float">
<br>
<h3> <a href="<?php echo esc_url( $category_link ); ?>" title="view&nbsp;posts">&nbsp;&nbsp;view&nbsp;posts&nbsp;&#9658;</a></h3>
<br>
</div>

It's just really 'messy' and with 20+ subjects means 20+ sidebars!

If anyone has any better ideas, I'd still be grateful

Ciao for now and thanks to anyone who may have given it some thought

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.