Forum Replies Created

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter palmtree

    (@toshirobot)

    Thanks guys for your advise,

    I have put following code to achieve what I wanted and it worked:)

    $cats = get_the_category();
    $cat = $cats[0];
    if ($cat->parent){
      $parent = get_category($cat->parent);
      echo '<h2><a href="' . get_category_link($cat->cat_ID) . '">' . $parent->cat_name . '</a>';
    } else {
      echo '<h2><a href="' . get_category_link($cat->cat_ID) . '">' . $cat->cat_name . '</a>';
    }
    $cats = get_the_category();
    foreach($cats as $cat):
      if($cat->parent) echo '<span class="category-separator">&nbsp;&rarr;&nbsp;</span><a href="' . get_category_link($cat->cat_ID) . '">' . $cat->cat_name . '</a>';
    endforeach;
      echo '</h2>';
    Thread Starter palmtree

    (@toshirobot)

    Now sorted out. This works as I wanted.

    $terms = get_categories( array(
      'orderby' => 'name',
      'parent'  => 0,
         ) );
    echo '<ul>';
            foreach ($terms as $term) {
            $categories = get_categories('child_of='.intval($term->term_id));
            echo '<li><a href="' .get_category_link($term->term_id). '">'.$term->name.'</a>';
            echo '<ul>';
            foreach ($categories as $category) {
            if ($category->parent==$term->term_id):
            echo '<li><a href="'. get_term_link( $category ). '">'.$category->cat_name.'</a></li>';
            endif;
        }
        echo '</ul>';
        echo '</li>';
        }
    echo '</ul>';
    Thread Starter palmtree

    (@toshirobot)

    Hi Sarmistha,

    Thank you for taking a look.

    Your code is very close. I could establish the parent and children structure output but the URL of the child category is not printed. Please see my code below. It’s bit modified because your a href code did not work. Now parent category name and URL are correct. And children’s category name is correct but their URL..

    <?php
    	$terms = get_categories( array(
    	  'orderby' => 'name',
    	  'parent'  => 0
    		 ) );
    
    	echo '<ul>';
    
    		foreach ($terms as $term) {
    			
    			$categories = get_categories('child_of='.intval($term->term_id));
    			echo '<li><a href="'.get_category_link($term->term_id).' ">'.$term->name.'</a>';
    
    			echo '<ul>';
    
    			foreach ($categories as $category) {
    			if ($category->parent ==$term->term_id):
    			echo '<li><a href="'.get_category_link($term->term_id).'">'.$category->cat_name.'</a></li>';
    			endif;
    		}
    		echo '</ul>';
    		echo '</li>';
    		}
    	echo '</ul>';
    ?>
    • This reply was modified 7 years, 5 months ago by palmtree.
    Thread Starter palmtree

    (@toshirobot)

    Your code solved everything!!
    Thank you so much:)

    Thread Starter palmtree

    (@toshirobot)

    Thanks bcworkz. Conditional structure made sense to me but the result was the same as what my initial code does.

    Initial code was fine except for the print name (name was child cat but url was parent cat. The print name and url should be both parent) and it works fine with post under the cat that doesn’t have parent.

    I wonder how this can be done…

    Thread Starter palmtree

    (@toshirobot)

    Thank you Michael,
    Your solution is getting close… because now I can see “lollipop” with URL link for the post “I like sweet things”. But for other post which doesn’t have parent category lost category output.

    For example when the post “I like sweet things” belongs to “gummy” category that has no parent, there will be no output (neither gummy nor its url pointing to gummy) by using the code above so the <span class=”category”> becomes empty.

    I think the code above only works for the post that belongs to category which has parent…

    For this (when cat doesn’t have parent cat), the previous code was fine. Only what’s missing in previous code was not being able to output “parent” name (when cat has parent) although the URL link was of the parent.

    • This reply was modified 7 years, 6 months ago by palmtree.
    • This reply was modified 7 years, 6 months ago by palmtree.
    • This reply was modified 7 years, 6 months ago by palmtree.
    Thread Starter palmtree

    (@toshirobot)

    Your code works! I really appreciate your time looking into this. However now although all the output post is the latest from each child category (as planed), those post order is not ordered by date but by something else which I can confirm they are NOT by alphabetical order, ASC or DESC.. but I found it is the order shown in list of category edit view inside WordPress control panel. This is very strange..

    For example, I have a child cat A and B. If the child cat B has one post (post-X) with newer post-date than another (post-Y) from cat A. I want post-X to be on top of post-Y. Make sense?

    I have put 'orderby'=>'post_date', 'order'=>'DESC' and 'order'=>'modified' shown below but none of these works…

    <div class="carousel-wrap">
    <div class="carousel-container">
    <div class="slides">
    
    <?php 
    	$categories = get_categories(['parent' => 54,]);
    	foreach ( $categories as $category ) {
    		$args = array(
    		'cat' => $category->term_id,
    		'post_type' => 'post',
    		'posts_per_page' => '1',
    		'orderby' => 'modified'
    		);	
    	$query = new WP_Query( $args );
    ?>
    <?php if ($query->have_posts()) : ?>   
    <?php while ($query->have_posts()) : $query->the_post(); ?>   
    <div class="box">
    	<article>
    		<p><?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of(54, $childcat)) { echo '<a href="'.get_category_link($childcat->cat_ID).'">'; echo $childcat->cat_name . '</a>'; }} ?></p>
    		<?php if ( has_post_thumbnail() ): ?><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('box-pic'); ?></a><?php endif; ?>
    		<h3><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
    	</article>
    </div>
    <?php endwhile;?>
    <?php endif; ?>
    <?php wp_reset_postdata(); } ?>
    
    </div>
    </div>
    </div>

    This is the code now.
    Do you have any idea?

    Thread Starter palmtree

    (@toshirobot)

    Thanks bcworkz. I tried the code you suggested but the number of output was still 1..
    And strangely the output post this time was the second latest while my original code output the very latest post from all the child category.. I wonder why.

    There are more than 1 (currently 7) categories under the parent 54 and those child categories have their latest posts. It means that the number of output post should be 7.

    Thread Starter palmtree

    (@toshirobot)

    Usually, when I have the Chinese charactor output problem like this, if I save plugin php file in UTF-8 enchoding, it solves the problem.

    But Yoast Breadcrumbs plugin doesn’t even let me save in UTF-8….

    Thread Starter palmtree

    (@toshirobot)

    I could solve the problem thanks to this guy I was advised to add to code below into the very bottom (just before ?>) of the “wp-settings.php” file. (usually located at wordpress/wp-setting.php)

    if ( isset( $wp_roles ) && ! isset( $wp_roles->roles['administrator']['capabilities']['create_users'] ) ) {
    	$wp_roles->roles['administrator']['capabilities']['create_users'] = true;
    	$role_key = $wpdb->prefix . 'user_roles';
    	update_option( $role_key, $wp_roles->roles );
    }

    And then accessed to user panel. I could see “add new user” option within user tab menu:) After verifying add user option, delete the code from wp-settings.php again.

    Yeah I also do have the same problem… I posted the situation today.
    Just for the couter measure – enable “allow anyone can create (register) user” in setting section in control panel can do the job. But if you want to only create user as a admin like what I want to do! this can not be a solultion for a long time..

    Please help us solving problem.

Viewing 11 replies - 1 through 11 (of 11 total)