Wordpress Code & Research

Icon

All things about wordpress

Get the lastest posts from a category

<h2>Recent Posts</h2>
<ul>
<?php
	$args = array( 'numberposts' => '5' );
	$recent_posts = wp_get_recent_posts( $args );
	foreach( $recent_posts as $recent ){
		echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.$recent["post_title"].'" >' .   $recent["post_title"].'</a> </li> ';
	}
?>
</ul>
Advertisements

Filed under: BASIC CODE

Add New Hook In Prestashop

PrestaShop hooks are great way to insert or add data at the most important places or actions of this great e-commerce platform. But may be some time you want to use custom hooks with my custom modules. So here I show you how to implement custom hook?


How to create custom hook?

Step 1: Register a new hook in FrontController.php

open file \classes\FrontController.php and find below code

1
2
3
4
5
self::$smarty->assign(array(
'HOOK_HEADER' => Module::hookExec('header'),
'HOOK_TOP' => Module::hookExec('top'),
'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn')
));

Updated code

For add new hook I add this line of code

1
'HOOK_Top_Center' => Module::hookExec('topCenter') // New Hook

So you code look like below.

1
2
3
4
5
6
self::$smarty->assign(array(
'HOOK_HEADER' => Module::hookExec('header'),
'HOOK_TOP' => Module::hookExec('top'),
'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn'),
'HOOK_Top_Center' => Module::hookExec('topCenter')
));

Step 2: All active hooks are hold in Prestashop database table called ps_hook. So I make query for new hook and run into database.

1
2
3
4
5
6
7
8
9
10
INSERT INTO 'yourdatabasename'.'ps_hook' (
'id_hook' ,
'name' ,
'title' ,
'description' ,
'position'
)
VALUES (
NULL , 'topCenter', 'Top of Center', NULL , '1'
);

What are field mean ps_hook.

`id_hook` = An auto increment value
`name` = Name of the new hook
`title` = This title is used in Modules tab in Back Office
`description` = The description of hook (optional)
`position` = Whether this hook observe the order of appearance with other module or not

Or you can add value manually from php myadmin.

open your “php_myadmin” and table “ps_hook” click on “insert” and insert ‘new’ hook name, ‘new hook’ in ‘title’

Step 3: Add code in your module:

Please create new function with name hooktopCenter in class of modules PHP file which you want to use.
‘hook’ is prefix function for hook
‘topCenter’ is name of hook postion

Here I call blockmanufacturer.tpl file in this funcation.

1
2
3
4
5
6
7
8
9
10
11
12
13
function hooktopCenter($params){
global $smarty, $link;
$smarty->assign(array(
'manufacturers' => Manufacturer::getManufacturers(),
'link' => $link,
'text_list' => Configuration::get('MANUFACTURER_DISPLAY_TEXT'),
'text_list_nb' => Configuration::get('MANUFACTURER_DISPLAY_TEXT_NB'),
'form_list' => Configuration::get('MANUFACTURER_DISPLAY_FORM'),
'display_link_manufacturer' => Configuration::get('PS_DISPLAY_SUPPLIERS'),
));
return $this->display(__FILE__, 'blockmanufacturer.tpl');
}

Step 4: Add the code {$HOOK_Top_Center} in theme/prestashop/header.tpl or any other tpl file where you want to use it.

1
<div id="header_right"> {$HOOK_Top_Center} </div>

Step 5: Then go to backend
=> Click on module top of link and configure any module which you want to change position and show in you new hook position.
=> Click position
=> Click on transplant module and select “topCenter” from dropdown and save.

Than look at your frontend at that position.

Filed under: Prestashop

Add widget to wordpress theme

We can add widget to wordpress theme ( any where: sidebar,footer,content page,…)

In this example, I will guide you add widget to wordpress theme.

There are really three main parts to introducing a footer-sidebar
1. Registering the Sidebars in the WordPress Theme
2. Inserting the Sidebars In the WordPress Theme
3. Putting some style into the sidebars

1. Register the Sidebars in the WordPress Theme

Go to the WordPress theme editor and open the Theme Functions (functions.php) file. Now Search for the following line in your Theme Functions (functions.php)

if ( function_exists('register_sidebar') )

Once you find the above line then take a look at the the next line which should look similar to one of the followings depending on how many sidebars you have:

register_sidebar(array(
or
register_sidebars(2,array(

Say for example you have one sidebar in your theme and you want to add three rows of sidebars in the footer area so you can put widgets then overwrite the code with the following:

register_sidebars(4,array(

The above will register 4 sidebars (one that you already have and three more that you are about to introduce in the footer area of your wordpress theme).

2. Insert the Sidebars In the WordPress Theme

Now lets insert the siderbars where we want them in the WordPress theme. In our case we are going to insert in in the footer area of the theme so open the Footer (footer.php) file and insert the following code just above the ‘footer’ division:

<div id="footer-sidebar">
<div id="footer-sidebar1">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(2) ) : ?>
<?php endif; ?>
</div>

<div id="footer-sidebar2">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(3) ) : ?>
<?php endif; ?>
</div>

<div id="footer-sidebar3">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(4) ) : ?>
<?php endif; ?>
</div>

</div>
<div style="clear-both"></div>

3. Put some style into the sidebars

Finally, lets put a little style to all the ‘footer-sidebar’ divisions that we just introduced. Open the Stylesheet (style.css) file and insert the following CSS (you will probably have to adjust the CSS to your need depending on what wordpress theme you are using).

#footer-sidebar {
display:block;
height: 250px;
}

#footer-sidebar1 {
float: left;
width: 340px;
margin-left:5px;
margin-right:5px;
}

#footer-sidebar2 {
float: left;
width: 340px;
margin-right:5px;
}

#footer-sidebar3 {
float: left;
width: 340px;
}

Hope this helps! Now you don’t have to change your beloved WordPress theme just to get footer-sidebar :)

Filed under: BASIC CODE

Get page in wordpress

1- When you want to list all pages, you can use this function:

  <?php wp_list_pages( $args ); ?>

2- Get ID OF A PAGE BY SLUG PAGE

function get_ID_by_slug($page_slug) {
$page = get_page_by_path($page_slug);
if ($page) {
return $page->ID;
} else {
return null;
}
}

3 – Get page title by page ID

<?php $pageID = 86; $page = get_post($pageID); 
echo $page->post_title; ?>

Filed under: BASIC CODE

Get taxonomy in wordpress

Today, I post about the way to get taxonomy in wordpress:
1-Get the taxonomy name by query_var

$the_tax = get_taxonomy( get_query_var( ‘taxonomy’ ) );
echo $the_tax->labels->name;

Get

if (is_archive())
{
if(is_tax(‘destination’))
{
$term = get_term_by( ‘slug’, get_query_var( ‘term’ ),            get_query_var( ‘taxonomy’ ) );

Get the description:
echo $term->description);

or use this:

$description = explode(“|”, $term->description);
$dest1= $description[0];
echo $dest1;
}
}

Filed under: BASIC CODE, , ,

Get post meta

Post meta is a good feature of wordpress, when you use post you can attach post meta for each post.

You can get post meta value by many ways:
The code below will help you get value of this variable

<?php $meta_values = get_post_meta($post_id, $key, $single); ?>
<?php $key_1_values = get_post_meta(76, ‘key_1’); ?>
<?php $key_1_value = get_post_meta(ID, ‘key_1’, true); ?>

Filed under: BASIC CODE

Get the category in wordpress

I like wordpress very much, so I worked with it for many times.
Sometimes I cannot find my wordpress code which I stored them in my computer.
So I created the blog and saved my wordpress research and code here.

In the first post, I will post basic code when you work with category .

1- get_the_category:

You want to get the current category name, you can use it:

<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>

You want to get the current category id, the same is below:

<?php
$category = get_the_category();
echo $category[0]->cat_ID;
?>

2- Get category by slug
Example we have slug with a variable: $pageslug

$pageslug= ‘news’;
$catslug = get_category_by_slug($pageslug); // get category of the same slug
$catid = $catslug->term_id; // get id of this category

3- Get the slug of current category with query var

<?php if(is_category())
{
$cat= get_query_var(‘cat’);
$yourcat= get_category ($cat);
echo ‘the slug is ‘.$yourcat—>slug;

}
?>

Filed under: BASIC CODE