• I have these three plugins working together:
    Advanced Custom Fields
    Categories Image – https://zahlan.net/blog/2012/06/categories-images/
    Post Tags and Categories for Pages

    I have custom fields set up which get applied to a page with page a page template named: Content Page

    The field names are:
    Page Link
    Page Link Name

    These are also repeater fields so more links to different pages can be added which then appear in the sidebar.

    Each page has its own category and each category has a category image.

    I am using this code which displays all of the links which are applied into a page with page template ‘Content Page’.

    <?php foreach (get_the_category() as $cat) : ?>
    <img />term_id); ?>" />
    
    <?php endforeach; ?>
    
    	<?php
    
    // check if the repeater field has rows of data
    if( have_rows('sidebar_links') ):
    
     	// loop through the rows of data
        while ( have_rows('sidebar_links') ) : the_row();
    	
    	    echo '<a class="content-page-sidebar-links" href="';
    	the_sub_field('page_link');
    	    echo '">&nbsp;';
    	the_sub_field('page_link_name');
    	    echo '</a>';
    	
    
        endwhile;
    
    else :
    
        // no rows found
    
    endif;
    
    		?>

    However it displays like this:

    Digital Connectivity
    – Internal Connectivity
    – External Connectivity
    – Clever Cogs
    – Bathroom

    Digital Connectivity is the category name which displays a category image in place of the category title.

    – Internal Connectivity
    – External Connectivity
    – Clever Cogs
    – Bathroom

    are all links.

    – Bathroom is a link in a different category to

    – Internal Connectivity
    – External Connectivity
    – Clever Cogs

    So bathroom should appear under a category name ‘Rooms & Areas’ and so should display the appropriate image above it, and any other links that are applied which have the category ‘Room & Areas’.

    Other links could also be applied into the sidebar which may belong to other categories.

    So I need to know how I can split the links in the sidebar so they appear under their correct category heading image. It also has to be automated so the code automatically knows that ‘Link A’ goes under ‘Heading A’ and ‘Link B’ goes under ‘Heading B’.

    But also if no links are available for ‘Heading B’ then ‘Heading B’ should not display.

    The reason this needs to be automated is because links which are applied into categories may also change categories, so it would be an administrative nightmare say if a category title was applied as a custom field at page level in the admin. These links need to be applied without any requirement to apply them under the correct heading. They should just automatically fall under the correct heading and display the correct heading image if links exist.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter nick1973

    (@nick1973)

    Just a quick follow up and to make it clearer. The menu at the moment displays like this:

    Digital Connectivity
    – Internal Connectivity
    – External Connectivity
    – Clever Cogs
    – Bathroom

    I want it to display like this:

    Digital Connectivity
    – Internal Connectivity
    – External Connectivity
    – Clever Cogs

    Rooms & Areas
    – Bathroom

    ‘Digital Connectivity’ and ‘Rooms & Areas’ are both images which are applied as a category image and display as the category title rather than the category title text.

    Moderator bcworkz

    (@bcworkz)

    Let me try summarizing your situation to be sure I’m correctly understanding it. The only relationship between sidebar content and the current page is page links in custom fields. You want to list those links grouped by their assigned category terms. These category terms may or may not be related to the current page.

    There is not any convenient WP function to manage this AFAIK, you’ll need to create a custom algorithm to do this. I would approach it like this:

    • Get all of the links from post meta/custom fields
    • For each link, get the related category term ID
    • Build an array of the collected data. Each element might be structured like this: 'page-slug' => $cat->term_id,
    • sort the array by term_id
    • step through the sorted array, outputting each link. Detect when term ID changes and output a new heading.

    If I’ve misunderstood your situation, hopefully my algorithm is still generic enough to give you an idea of a possible approach. Unresolved is how to handle pages with multiple category terms. You could either arbitrarily use the first term, or make link entries for each term so the link appears under every category.

    • This reply was modified 7 years, 3 months ago by bcworkz. Reason: used wrong term
    Thread Starter nick1973

    (@nick1973)

    Yes possible and I think that approach might be more or less how it should work.

    The Page where the links are input into custom fields has its own category.

    The links that are put in the custom fields ‘MIGHT’ belong to different categories.

    Because of this, the links need to be sorted under their correct category heading in the sidebar, but the category heading should only display if there are links for that category heading. The links cannot have a category assigned to them at the input field in the admin because they might switch category at a later date and therefore if that happens the image which is the title needs to switch to the correct image site wide.

    I’m not really a heavy weight coder with this sort of thing so not really sure where to start. This goes beyond my capabilities, so some code examples would be good.

    Thread Starter nick1973

    (@nick1973)

    Just wondered if anybody had any further ideas how I might achieve this?

    Thread Starter nick1973

    (@nick1973)

    Just saying I am really desperate for help on this one as I am hitting a brick wall everytime get anywhere near. Please please can someone help me with this one. Really need to find a way and not sure how.

    Moderator bcworkz

    (@bcworkz)

    Thanks for clarifying. I think my algorithm will work for you. Since the categories come from the links, it’s impossible to have categories with no links. Unfortunately, I cannot offer any examples. I’m sure my algorithm seems overwhelming. If you have any coding aptitude, you could likely struggle through implementing any individual step through research and trial and error. Chain the steps together and you’ll eventually get it all, learning a lot (and struggling a lot) along the way. Start with implementing the bare minimum functionality, then incrementally build upon that.

    For example, on the sidebar template, add code to get a single link from post meta. Determine its category, then output the results. It’s not what you really want, but it’s something you can build upon a little at a time until you do get what you want.

    If this approach is not for you, it’s quite understandable. You can save a lot of time and effort by hiring professional help through sites like https://jobs.wordpress.net/ or https://jetpack.pro/ .

    [puts moderator hat on] Please don’t bump your topics in these forums. It doesn’t garner any extra attention by moving your topic higher in the list like it does in most forums. Everyone here is a volunteer, replies can be slow in coming, please be patient. Bumping is also against our guidelines. Thanks for understanding.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Sidebar Category Links’ is closed to new replies.