change the font size in tag cloud

情到浓时终转凉″ 提交于 2019-12-23 17:05:09

问题


I would like to reduce the font size in tag cloud (at least for tags with more than one hits). I was looking in css file, but couldn't find anything about font size in tag cloud. Do you know where to change? (The link is www.veda-vit.de, just in case it's needed.)


回答1:


WordPress defines the default arguments that configure the Tag Cloud. These arguments are defined here in codex. Notice that you can specify the smallest and largest font size. By default, it's set at 22 with a unit of pt.

To change this default behavior, you will want to register a callback to the filter provided in WordPress Core.

Code

In this example, I'm changing both the smallest and largest font sizes. You will need to adjust it for your specific implementation:

add_filter( 'widget_tag_cloud_args', 'change_tag_cloud_font_sizes');
/**
 * Change the Tag Cloud's Font Sizes.
 *
 * @since 1.0.0
 *
 * @param array $args
 *
 * @return array
 */
function change_tag_cloud_font_sizes( array $args ) {
    $args['smallest'] = '10';
    $args['largest'] = '18';

    return $args;
}

Where to Put the Code

A lot of people will tell you to add it to the theme's functions.php file. I'm not one of those people. I teach and advocate modular theme and plugin development. That means the theme's functions.php file should not be a collection point for everything.

Theme's functions.php

With that said, you could add the above code to your functions.php file if you wish.

Do the following steps:

  1. Open your theme's functions.php file.
  2. Scroll down to the very bottom of the file.
  3. If you find a closing PHP tag element, i.e. ?>, delete it. It's not necessary.
  4. Copy and paste the above code into the file (at the bottom of the file).
  5. Save the file.
  6. If it's on a server, then transfer it either by SSH or SFTP.

Modular Approach

I advocate a modular approach, by splitting up functionality and theme configuration into separate, distinct files that support a single purpose.

Step 1: Find the Folder

In your theme, you should a folder called lib, includes, or src. This folder is where you put custom functionality files.

Step 2: Create a New File

Within one of those folders, create a new file and call it widgets.php. Within this file, add the following code at the first line:

<?php
/**
 * Widgets functionality
 *
 * @package     YourTheme
 * @since       1.0.0
 * @author      your name
 * @link        your URL
 * @license     GPL-2+
 */

Then add the above code below it.

Step 3: Load the File

Now you need to load that file. Open up the theme's functions.php file, navigate to the end of the file, and then add the following code:

include_once( __DIR__ . '/lib/widgets.php' );

Just replace the lib with the name of the folder you put the new file into.

NOTE

The font-size is set as an inline CSS by WordPress. You can override those using the above code. It's not recommended to force it via CSS with !important. Let WordPress do its thing and just set the min and max as shown above.




回答2:


As we don't know how you implement your tag cloud, css is not the only way you have to customize it. the wp_tag_cloud() function accept an array of argument

<?php $args = array(
    'smallest'                  => 8, 
    'largest'                   => 22,
    'unit'                      => 'pt', 
    'number'                    => 45,  
    'format'                    => 'flat',
    'separator'                 => "\n",
    'orderby'                   => 'name', 
    'order'                     => 'ASC',
    'exclude'                   => null, 
    'include'                   => null, 
    'topic_count_text_callback' => default_topic_count_text,
    'link'                      => 'view', 
    'taxonomy'                  => 'post_tag', 
    'echo'                      => true,
    'child_of'                  => null, // see Note!
); ?>

If you want to modify the default behazviour without touching any template file, you can use the filter wp_tag_cloud, in functions.php,

add_filter('wp_tag_cloud', 'se_40811909', 10, 2);

function se_40811909($return, $args){

  // copy and modify $args to fit your needs

 // $args = array(.....);

 return $args;

}

You can see all details about them here



来源:https://stackoverflow.com/questions/40811909/change-the-font-size-in-tag-cloud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!