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>

Filed under: BASIC CODE

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