Forums

help to creat a widget (15 posts)

  1. taghaboy
    Member
    Posted 6 months ago #

    Hi i'm a newbie in PHP and wordpress,and i try to create a widgets for my theme, using this code who make a box with title and content:

    <div id="%1$s" class="box">
    	<div class="one-col box-top">
    		<h3 class="box-title">Title</h3>
    	</div>
    			<div class="one-col box-body">
    				txt &amp; contents her
    			</div>
    	<div class="one-col box-bottom"></div>
    </div>

    how can i insert it in the function.php and let it work in my sidebar?

    Thanks

  2. taghaboy
    Member
    Posted 6 months ago #

    any idea on how to include the code to be a widget?
    Thanks

  3. Shane G
    Member
    Posted 6 months ago #

    Hi,

    You can add this code in functions.php file of your theme and call this function from sidebar.php of the theme with required parameters..

    Thanks,

    Shane G.

  4. taghaboy
    Member
    Posted 6 months ago #

    i know but the code in the function must to be some thing as this to work :

    <?php
    if ( function_exists('register_sidebar') )
        register_sidebar(array(
            'before_widget' => '<div id="sbitem">',
            'after_widget' => '</div>',
            'before_title' => '<h2>',
            'after_title' => '</h2>',
        ));
    ?>

    Any help please.
    Thanks

  5. MichaelH
    moderator
    Posted 6 months ago #

    Couldn't you just use a text widget?

  6. taghaboy
    Member
    Posted 6 months ago #

    i need a dynamic widget not manual pls

  7. MichaelH
    moderator
    Posted 6 months ago #

    Consider downloading and installing Otto's PHP Code Widget.

  8. taghaboy
    Member
    Posted 6 months ago #

    hi MichaelH,
    thanks for the link, but i'm working to creat a website, and i need to let this code:

    <div id="%1$s" class="box">
    	<div class="one-col box-top">
    		<h3 class="box-title">Title</h3>
    	</div>
    			<div class="one-col box-body">
    				txt & contents her
    			</div>
    	<div class="one-col box-bottom"></div>
    </div>

    to be a widget ready, so how can i change it to some thing like this in the function.php :
    Exemple

    <?php
    if ( function_exists('register_sidebar') )
        register_sidebar(array(
            'before_widget' => '<div id="sbitem">',
            'after_widget' => '</div>',
            'before_title' => '<h2>',
            'after_title' => '</h2>',
        ));
    ?>

    Thanks for any help.

  9. MichaelH
    moderator
    Posted 6 months ago #

  10. renato_s
    Member
    Posted 6 months ago #

    taghaboy:
    That code is required to make a widgetized area in your theme, not to build a widget. To do this, try the following:
    1. Create a function (say "my_last_posts")that will display your widget (meaning it will echo the code you mentioned above);

    2. Register the widget and tell wp that it is going to use your function:

    function my_last_posts_init(){
        register_sidebar_widget(__('Last Posts'), 'my_last_posts');
    }

    That should be enough for you to find the widget available in your widgets list.

  11. renato_s
    Member
    Posted 6 months ago #

    Forgot to mention these 2 functions must be in functions.php or as a plugin in your wp-content/plugins directory.

  12. taghaboy
    Member
    Posted 6 months ago #

    I'm victim of my bad English, and i will explain again my need, and sorry to be a heavy guest!
    I create a theme who work fin, and i add the BOX in the sidebar manually, so in every time i must add this HTML code with some PHP on it to show for example : Recent post, Latest comment, Pages, Category...

    <div id="id-box" class="box">
    	<div class="one-col box-top">
    		<h3 class="box-title">The Title</h3>
    	</div>
    			<div class="one-col box-body">
    				My PHP code her
    			</div>
    	<div class="one-col box-bottom"></div>
    </div>

    so know, i want to upgrade my theme by supporting widget (add any widget from the admin-panel and show it in the sidebar area of my theme), so i create the file function.php (as in other widgetized themes) and i copy this code into-it :

    <?php
    if ( function_exists('register_sidebar') )
        register_sidebar(array(
            'before_widget' => '<div id="sbitem">',
            'after_widget' => '</div>',
            'before_title' => '<h2>',
            'after_title' => '</h2>',
        ));
    ?>

    but this code dont show the structure of the the box i want as in the HTML code in the top ( <div id="id-box".... )!!!
    so how can i put my HTML code in the :
    befor_widget, after_widget, befor_title, after__title

    Thanks, and again, i'm so sorry 4 my English.

  13. renato_s
    Member
    Posted 6 months ago #

    If I understood what you mean, you just have to replace the strings with the html code you want.

    So:
    'before_widget' => '<div id="sbitem">',
    would be
    'before_widget' => '<div id="id-box" class="box"><div class="one-col box-top">',

    and so on...

  14. taghaboy
    Member
    Posted 6 months ago #

    that's what i want Mr Renato.
    i hop you show me the rest of the code.
    And thanks for all

    [moderated--bump removed. Please refrain from bumping as per Forum Rules]

  15. taghaboy
    Member
    Posted 6 months ago #

    What does mean bump!!!

    Any idea or Tutorial to convert the code to some thing befor-widget...

    Thanks

Reply

You must log in to post.

About this Topic