mercredi 2 décembre 2020

Gradient list to parent category, custom taxonomy

I am nearly finished with creating a few pages on Wordpress using PHP with includes a navigation bar down the side, and I am struggling with the following.

I have my custom taxonomy and subjects set up like this:-

  • guideline
    • regulation-inspection
      • 1
      • 2
      • 3
    • next_subheader
      • a
      • etc...

On certain pages, it works where the navigation is the title for each article, and it works as an anchor link; see code here:

$args = array(
'post_type' => 'guideline',
'subject' => 'regulation-inspection',
'orderby'=>'menu_order',
'posts_per_page' => -1);

$my_query = new WP_Query( $args );
echo '<ul class="gradient-list gradient-list-ri">';

if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post();?>
        <a href="#post<?php the_ID(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><li><?php the_title(); ?></li></a><?php
    endwhile;
}
    echo '</ul>';
wp_reset_query();

which looks like this:

Index list with gradient

Unfortunately for this specific page I am working on, I need that list not to be the individual post but actually their parent category.

Here is the code I am using as a reference:

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

$list_child_terms_args = array(
    'taxonomy' => 'subject',
    'use_desc_for_title' => 0,
    'child_of' => 2991); //The category's ID
?>

<ul class="gradient-list gradient-list-ri">
    <a href="#post<?php the_ID(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php wp_list_categories( $list_child_terms_args ); ?></a>
</ul>

But this isn't helping me with creating anchor links to posts, and is displaying as one lump rather than individual list items like this:

Gradient list without space between

The code I am working on referencing the two codes above is this:

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

$list_child_terms_args = array(
    'taxonomy' => 'subject',
    'use_desc_for_title' => 0,
    'child_of' => 2991);

$my_query = new WP_Query( $list_child_terms_args );
echo '<ul class="gradient-list gradient-list-ri">';

if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post();?>
        <a href="#post<?php the_ID(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><li><?php the_title(); ?></li></a><?php
    endwhile;
}
    echo '</ul>';
wp_reset_query();

... Which is pulling data from somewhere else completely which I think is a fault of the 'the_title();.

Ideally, I want to create a list like the first example, which are anchor links to the category title above each article like this:

Desired outcome




Aucun commentaire:

Enregistrer un commentaire