• Resolved talino

    (@talino)


    Hi all,

    I have a problem with the way WP handles some links and it seems that the only way around this is modifying a WP core function. Since I’m trying to design my site by only touching the “theme” PHP & CSS, I was wondering if there was another way of doing things.

    Here’s the deal. I’ve modified the sidebar’s appearance so that when you hover a link, you get a nice full-width background-colored bar. CSS looks like this:


    div#sidebar ul li a {
    color: #333;
    padding: 0 0 0 20px;
    border: none;
    margin: 0;
    display: block;
    width: 170px;
    }
    div#sidebar ul li a:hover {
    background-color: #aaa;
    color: #fff;
    }

    This works fine for everything, except for the categories links. What WP created is a list-item which looks like this (excuse the href tags, the forum keeps converting my ‘a’ to links…):

    <list>
    <href href=”https://192.168.0.4:8888/?cat=2&#8243; title=”View all posts filed under Category”>Category</href> (1)
    </list>

    The number of posts (here 1) is outside the link, and therefore breaks my hover effect. Moreover it looks pretty depressing since there is a line break (the number is added below the category name).

    I can modify the style for the category posts number but I don’t think I’ll ever get it to appear right next to the name, with the block hover functioning properly.

    The only thing that would seem to solve this problem is modifying the wp_list_cats() function in template-functions-category.php, inside the includes folder, so that the number of posts is added within the link tag itself. But I’d rather not do because I’m afraid of breaking things and also because it will make upgrades to WP very annoying.

    Since a lot of people are deeply customizing WP, I would like to know if there is a safe way to accomplish this.

    Almost total newbie here, so please bear with me ??

    Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Les Bessant

    (@lesbessant)

    You could write your own function in a plugin. That would leave the core files untouched and avoid problems with upgrades.

    Thread Starter talino

    (@talino)

    Aha… Now I’ll have to learn how to write plugins (while at the same learning 300 other WP topics) ??

    If I understand correctly, this boils down to essentially copying the function to my own script, modifying it there and then activating the script as a plugin, right (assuming I’ve learn to format it correctly)?

    Kafkaesqui

    (@kafkaesqui)

    Writing your own plugin is a very good option (and if you have some understanding of PHP it’s not difficult once you have the basics down).

    Another option, though for some reason not discussed much, is to create (if it doesn’t exist) a functions.php template for your theme and insert the modified function(s) there. The functions.php template is a standard PHP document, in that it must start with <?php and end with ?>. WordPress loads it automatically before your other templates.

    PHP note: You can’t have more than one function available with the same name, so altering the function name of a built-in WP function would be part of the task of ‘formatting it correctly.’

    Thread Starter talino

    (@talino)

    Thanks Kafkaesqui. I was actually reading several WP docs titled “Writing your own plugins” and stuff like that, but I got lost quite quickly. I have some experience with PHP although not enough that it would make me understand sentences with terms like “hooks” and “filters” in them without blinking. I’ve customized my template quite heavily but I’m not really a programmer.

    I will try your idea for a functions template (and renaming the function call in the template file), because it does seem much easier to do. All I need to do is shuffle around an anchor tag. Learning to write plugins seems like overkill and anyway I’m reading 150 pages a day on WP now…

    From what I understand, your solution involves creating custom template tags, is that right?

    Kafkaesqui

    (@kafkaesqui)

    From what I understand, your solution involves creating custom template tags, is that right?

    In short, yes.

    Kafkaesqui

    (@kafkaesqui)

    For those who for whatever reason can’t ride the wave of roll your own WordPress solutions, here’s a bit of code you can replace the use of wp_list_cats() in your template with which will move the category count over into the link text:

    <?php
    ob_start();
    wp_list_cats('optioncount=1');
    $my_list_cats = ob_get_contents();
    ob_end_clean();
    echo preg_replace('/<\/a> \(([0-9]*)\)/', ' (\\1)</a>', $my_list_cats);
    ?>

    Replace the wp_list_cats() line above with your own if you set other parameters (or use list_cats()). The output buffering that occurs with this causes a little overhead, but hey, no source hacking, no plugin writing!

    Thread Starter talino

    (@talino)

    Thanks a lot for a simple solution to a simple problem. Always the best course of action…

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘(Advanced?) Theming question’ is closed to new replies.