dtudj42064 2015-11-03 15:46
浏览 73
已采纳

将div设置为widget的内容| WordPress的

I'm creating a custom sidebar on Wordpress to display independent blocks, and each block has a widget in it. To do so, I registered widget areas in functions.php, and then added those widget areas to my sidebar.

The sidebar is set like this :

<aside class="sidebar-right" role="complementary">
<div class="sidebar-widget">
    <?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('widget-area-1')) ?>
</div>
<div class="sidebar-widget">
    <?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('widget-area-2')) ?>
</div>
<div class="sidebar-widget">
    <?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('widget-area-3')) ?>
</div>
<div class="sidebar-widget">
    <?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('widget-area-4')) ?>
</div>
</aside>

I roughly want my sidebar to look like this : https://jsfiddle.net/b7xtksts/

This is how the widget is called in functions.php :

register_sidebar(array(
    'name' => __('Widget Area 1'),
    'description' => __('Description'),
    'id' => 'widget-area-1',
    'before_widget' => '<div id="%1$s" class="%2$s">',
    'after_widget' => '</div>',
    'before_title' => '<div class="widget-title-background"><h3>',
    'after_title' => '</h3></div>'
));

Everything works fine, but I would like to add padding to every widget's content, without having to edit the original widget, so that every widget added in the future will fit properly thanks to the padding.

But if I set padding to, let's say, <div id="%1$s" class="%2$s" style="padding:10px;">, the padding is added to the whole widget, and the background title as well (which doesn't fill 100% of the box anymore). I'm trying to figure out how I could set a padding to the widget's content without affecting the div title above. Since there are only before/after _widget and _title, how can I set a class to the content only ?

  • 写回答

1条回答 默认 最新

  • duanping5306 2015-11-03 17:45
    关注

    Seems like we cannot add any specific class to content body of the widget.

    One option I suggest is following:

    1. Add padding to your parent widget div like you did e.g.

      .sidebar-widget{ padding: 0 10px; }

    2. To remove the effect of 10px padding on sides on the title background div, we cancel it with the negative margin on side i.e.

      .widget-title-background{ margin-left: -10px; margin-right: -10px; }

    This will make your title's dark shaded div go around the corners, canceling the effect of the padding on the main div.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)