Home / PHP/MySQL Tutorials / Archive by category 'Wordpress'

Wordpress

Create a perfectly SEO optimized title tag for wordpress

The solution to this problem is to modify the way your WordPress theme displays the title tag.
1 . To do so, open the header. php fle from your theme. The code used to generate the title tag might look like this:

<title>
<?php bloginfo(' name' ) ; ?>
<?php if ( is_single( ) )
 { 
?>
»Blog Archive
<?php 
}
?>
<?php wp_title() ; ?>
</title>

2. Once you have found it, replace it with the following:

<title>
<?php if (is_home ( ) ) { bloginfo(' name' ) ; }
elseif (is_category( ) ) { single_cat_title( ) ; echo ' - ' ; 
bloginfo( ' name' ) ; }
elseif (is_single() ) { single_post_title() ; }
elseif (is_page( ) ) { bloginfo(' name' ) ; echo ' : ' ; 
single_post_title() ; }
else { wp_title( ' ' , true) ; } ?></title>

3. Save your header. php fle. The modifcation has been made and your titles are now SEO-friendly.


Permalink structure in wordpress

1 . By default, WordPress URLs aren’t rewritten so as to avoid compatibility problems with non-Apache servers, and look like this:

 http: //www.w3mentor.com/blog/?p=1522

2. To modify your blog permalink structure, carry out the following steps:
Login to your WordPress dashboard.
Go to Settings | Permalinks.
Select the permalink structure you’d like to use, or enter a custom structure.

WordPress allows you to use ten structure tags to personalize your permalink structure:
%year%: The year the post has been published, for example, 2010
%monthnum%: The month the post has been published, for example 01 for January
%day%: The day the post has been published, for example 01 for the first day of the month
%hour%: The hour the post has been published
%minute%: The minute the post has been published
%second%: The second the post havs been published
%postname%: The name of the post
%post_id%: The post ID, for example, 715
%category%: The post category
%author%: The post author

Structure tags can be mixed up together to create a personalized structure. Don’t forget to use a separator between structure tags.
Following are the recommended separators:
the slash (/)
the underscore (_)
the hyphen (-)


Highlight featured sticky posts in wordpress

Insert the following code inside the Loop, after a call to the_post.

<?php
if(is_sticky()) {
?>
<div class="sticky-post-class">
<p>This post is sticky.</p>
</div>
<?php
}
?>

Remove posts with a certain tag from the Loop in wordpress

Add the following to functions.php.

add_action('pre_get_posts', 'remove_tag_from_loops');
function remove_tag_from_loops( $query ) {
if(!$query->get('suppress_filters')) {
//replace tag1 by the slug for the tag to be hidden
$tag_id = get_term_by('name','tag1','post_tag')->term_id;
$excluded_tags = $query->get('tag__not_in');
if(is_array( $excluded_tags )) {
$excluded_tags[] = $tag_id;
} else {
$excluded_tags = array($tag_id);
}
$query->set('tag__not_in', $excluded_tags);
}
return $query;
}

Hide posts from a certain category in WordPress

Insert the following code into functions.php.

add_action('pre_get_posts', 'remove_cat_from_loops');
function remove_cat_from_loops( $query ) {
if(!$query->get('suppress_filters')) {
//add category name below.
$cat_id = get_cat_ID('Category Name');
$excluded_cats = $query->get('category__not_in');
if(is_array($excluded_cats)) {
$excluded_cats[] = $cat_id;
} else {
$excluded_cats = array($cat_id);
}
$query->set('category__not_in', $excluded_cats);
}
return $query;
}

Displaying ads after every x posts in wordpress

Add the following to template file like index.php or category.php.

<?php
if( have_posts() ) {
$ad_counter = 0;
$after_every = x //change this to the number of posts;
while( have_posts() ) {
$ad_counter++;
the_post();
?>
<h2><?php the_title(); ?></h2>
<?php
// Display content
$ad_counter = $ad_counter % $after_every;
if( 0 == $ad_counter ) {
echo '<h2 style="color:red;">Advertisement or custom content here</h2>';
}
}
}
?>

Iterate through the available posts in wordpress and display titles

<?php
if( have_posts() ) {
while( have_posts() ) {
the_post();
?>
<h2><?php the_title(); ?></h2>
<?php
}
}
?>