• Hi there,

    I am really bad in php and I have got one problem.
    I would like to display Category and number of posts in Category under. This number should be clickable as well like it would be only one button.

    <div class="categories">
    
    	           <ul>
    
    <?php
    $variable = wp_list_categories('title_li&echo=0&show_count=1');
    
    $variable = str_replace( '(', 'Posts: ', $variable);
    $variable = str_replace( ')', '', $variable);
    echo $variable;
    ?>   
    
    	           </ul>
    
    </div>

    I did only the first part, thanks to CSS I can see number of posts unber Category name. Unfortunetly cannot do the second one – make number of posts clickable, like the whole thing would be only one button.

    Please help me…

Viewing 4 replies - 1 through 4 (of 4 total)
  • <div class="categories">
    
    	           <ul>
    
    <?php
    $variable = wp_list_categories('title_li&echo=0&show_count=1');
    
    $variable = str_replace( '</a>', ' ', $variable); //remove the closing </a> tag here
    $variable = str_replace( '(', 'Posts: ', $variable);
    $variable = str_replace( ')', '</a>', $variable); //insert the closing </a> tag after the number
    echo $variable;
    ?>   
    
    	           </ul>
    
    </div>
    Thread Starter lash666

    (@lash666)

    Thank you very much! That is what I was looking for.
    But now, Category name and Number of posts is in the same row:

    <li class="cat-item">
    <a title="Show all posts form Category Name" href="/category">Category Name  Posts: 52</a>

    I would like to displey numbers under. Should I add </ br> somewhere?
    I was thinking about definding different style for Category Name e.g. <h1> and different for Posts in Category e.g. <p>.

    Something like that:

    <div class="categories">
    
    	           <ul>
    
    <?php
    $variable = wp_list_categories('title_li&echo=0&show_count=1');
    
    $variable = str_replace( '</a>', ' ', $variable); //remove the closing </a> tag here
    $variable = str_replace( '(', '<p>Posts: ', $variable);
    $variable = str_replace( ')', '</p></a>', $variable); //insert the closing </a> tag after the number
    echo $variable;
    ?>   
    
    	           </ul>
    
    </div>

    Any ideas?

    i personaly would not use h1 anywhere else than for the main titles;
    also what you are trying would invalidate the html validation.
    assuming, if you want to use h1 and p tags, the code should look like:
    <h1><a href="link">category name</a></h1><p><a href="link">Posts: 52</a></p>

    that would be difficult to achieve with str_replace.

    to get different styles and two lines, you could work with br/ and span:
    <a href="link">category name<br /><span>Posts: 52</span></a>

    <?php
    $variable = wp_list_categories('title_li&echo=0&show_count=1');
    
    $variable = str_replace( '</a>', '<br /> ', $variable);
    $variable = str_replace( '(', '<span>Posts: ', $variable);
    $variable = str_replace( ')', '</span></a>', $variable); echo $variable;
    ?>
    Thread Starter lash666

    (@lash666)

    Thanks for your advice.

    You are right, using H1 is not a good idea.

    Anyway, your code is working perfectly, you have made my day.

    Thanks one more time mate!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Clickable number of posts in category’ is closed to new replies.