How to remove first image from wordpress post with caption

I struggled lot for this issue. Some times I only removed the first image. some times I removed first caption. Using following code you can easily remove the post first image and caption.

How to remove first image from wordpress post with caption if caption is present

I used the following code for first image removing.

/*
 * Removing the first image from post
 */
function remove_first_image ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
    if (preg_match("/<img[^>]+\>/i",$content)) {
		 $content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
 	}
	return $content;
   }
}

But I faced the some issues with that code. When first image has caption then only image get removed and caption will remain there.

After that I used the following code for caption remove.

/*
 * Removing the first caption from post with image
 */
function remove_first_image_caption ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
 $content = preg_replace("(\)", "", $content, 1);
 } return $content;
}
add_filter('the_content', 'remove_first_image_caption');

But if first image has no caption and second image image has caption then both the images will be get removed from post.

After some struggle I written following code. Following is the solution:

Final Code.

/*
 * Removing the first image from post
 * functionality added to delete caption is present to first image.
 */
function remove_first_image ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
    if (preg_match("/<img[^>]+\>/i",$content)) {
    //find first image URL
     $first_img = '';
     $output = preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/', $content, $matches);
     $first_img = $matches[1][0];

    //find first image caption inner text
     $output = preg_match_all("/caption=['"](.*)/", $content, $matches);

     //find first image present in first caption text or not
        $pos = strpos($matches[0][0], $first_img);

    // if image URL found in caption array then delete the caption and image
        if ($pos !== false) {
           $content = preg_replace("(\)", "", $content, 1);
        } else {
           $content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
        }
    }

 } return $content;
}
add_filter('the_content', 'remove_first_image');

solved: pagination for Custom post type not working

Some people asked me about pagination of custom post type. That is very easy.

For showing the custom post type we always use the query_post method.

Just use the following code in your template or theme file.

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php get_header(); ?>

            <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php  query_posts( 'post_type=custom-post-type&paged='.$paged );
                                    if (have_posts()) : while (have_posts()) : the_post(); ?>

loop here

              			<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endwhile; ?>
				  <div id="nav-below" class="navigation"></pre>
<div class="nav-previous"><!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentyten' ) ); ?></div>
<pre></pre>
<div class="nav-next"><!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentyten' ) ); ?></div>
<pre>
				  </div><!-- #nav-below -->
				  <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endif; wp_reset_query(); ?>

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php get_footer(); ?>
remove first image from wordpress post with caption
remove first image from wordpress post with caption
How to remove first image from wordpress post with caption if caption is present

wordpress pagination with query posts

wordpress pagination with query posts, Mostly people use query_posts function for fetching posts. But most of all people face issue with pagination.

wordpress pagination with query posts

Just use following code for pagination. It is simple and easy.

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'post_type=documents&paged='.$paged );
query_posts( 'post_type=post&paged='.$paged );
if (have_posts()) : while (have_posts()) : the_post(); ?>

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php the_excerpt();  ?>

 <?php endwhile; ?>
<div id="nav-below" class="navigation">
                                        <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
                                        <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
                                </div><!-- #nav-below -->
                                  <?php endif; wp_reset_query(); ?>

wordpress pagination with query_posts
wordpress pagination with query_posts

show posts to only registered users in wordpress

Some people want to show posts to only registered users in wordpress in site. You just need to create category and publish your posts in private category.

How to show posts to only registered users in wordpress

Use the following code in index.php and single.php and page.php file.


$cat_id = get_cat_ID('private');
$args=array(
  'category__not_in' => $cat_id,
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 5
);

if(!is_user_logged_in()){
    $cat_id = get_cat_ID('private');
   $args=array(
  'category__not_in' => $cat_id,
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 5
);

} else {
    $cat_id = get_cat_ID('public');
$args=array(
  'category__not_in' => $cat_id,
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 5
);

}
// The Query
query_posts( $args );

// The Loop
while ( have_posts() ) : the_post();
	echo '<li>';
	the_title();
	echo '</li>';
endwhile;

// Reset Query
wp_reset_query();
How to show posts to only registered users in wordpress
How to show posts to only registered users in wordpress

wordpress show posts with certain tag

WordPress tutorial, wordpress show posts with certain tag. We can show the five posts with certain tags. We can use following code for showing the posts.

wordpress show posts with certain tag

Just use the following code. Copy paste in to template page where you want to show posts.

<?php
$args = array( 'numberposts' => 5, 'order'=> 'DESC', 'orderby' => 'post_date', 'tag_slug__in' => array( 'wordpress, tag1,tag2' ) );
$postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?>
 <?php the_time('F j, Y'); ?>
<a href="<?php echo get_permalink(); ?>">
<?php the_title(); ?>
 </a> <?php the_excerpt(); ?>
 <?php endforeach; ?>
wordpress show posts with certain tag
wordpress show posts with certain tag

Limit excerpt length by characters in wordpress

On wordpress home page many sites are showing the post description or we can say the excerpt. For UI purpose some time we need to control the excerpt character limit.  I already written about this in following article. But some people need the more advanced excerpt. Following code snippet will be helpful to you show the limit excerpt length by characters in wordpress.

First open your functions.php file and put following code in that file.

[viral-lock message=”Solution code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]

function get_excerpt($count){
  $permalink = get_permalink($post->ID);
  $excerpt = get_the_content();
  $excerpt = strip_tags($excerpt);
  $excerpt = substr($excerpt, 0, $count);
  $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
  $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
  return $excerpt;
}

[/viral-lock]

Use this function if you’re planning on using it more than once with a different amount of characters.

Call the function plus the amount of characters –

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php echo get_excerpt(125); ?>

Following is my old article link.
https://purabtech.in/set-wordpress-post-excerpt-length-limited-characters/

Limit excerpt length by characters in wordpress
Limit excerpt length by characters in wordpress

How to create breadcrumbs in wordpress

we need to show breadcrumbs in wordpress site. For pages and category we can create breadcrumbs in wordpress. Shown, How to create breadcrumbs in wordpress.

How to create breadcrumbs in wordpress

We need to put following code in functions.php file.

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
function wordpress_breadcrumbs() {

  $delimiter = '&raquo;';
  $name = 'Home'; //text for the 'Home' link
  $currentBefore = '<span class="current">';</span>
  $currentAfter = '';

  if ( !is_home() && !is_front_page() || is_paged() ) {

    echo '<div id="crumbs">';

    global $post;
    $home = get_bloginfo('url');
    echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';

    if ( is_category() ) {
      global $wp_query;
      $cat_obj = $wp_query->get_queried_object();
      $thisCat = $cat_obj->term_id;
      $thisCat = get_category($thisCat);
      $parentCat = get_category($thisCat->parent);
      if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
      echo $currentBefore . 'Archive by category '';
      single_cat_title();
      echo ''' . $currentAfter;

    } elseif ( is_day() ) {
      echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
      echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
      echo $currentBefore . get_the_time('d') . $currentAfter;

    } elseif ( is_month() ) {
      echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
      echo $currentBefore . get_the_time('F') . $currentAfter;

    } elseif ( is_year() ) {
      echo $currentBefore . get_the_time('Y') . $currentAfter;

    } elseif ( is_single() ) {
      $cat = get_the_category(); $cat = $cat[0];
      echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
      echo $currentBefore;
      the_title();
      echo $currentAfter;

    } elseif ( is_page() && !$post->post_parent ) {
      echo $currentBefore;
      the_title();
      echo $currentAfter;

    } elseif ( is_page() && $post->post_parent ) {
      $parent_id  = $post->post_parent;
      $breadcrumbs = array();
      while ($parent_id) {
        $page = get_page($parent_id);
        $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
        $parent_id  = $page->post_parent;
      }
      $breadcrumbs = array_reverse($breadcrumbs);
      foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
      echo $currentBefore;
      the_title();
      echo $currentAfter;

    } elseif ( is_search() ) {
      echo $currentBefore . 'Search results for '' . get_search_query() . ''' . $currentAfter;

    } elseif ( is_tag() ) {
      echo $currentBefore . 'Posts tagged '';
      single_tag_title();
      echo ''' . $currentAfter;

    } elseif ( is_author() ) {
       global $author;
      $userdata = get_userdata($author);
      echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;

    } elseif ( is_404() ) {
      echo $currentBefore . 'Error 404' . $currentAfter;
    }

    if ( get_query_var('paged') ) {
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
      echo __('Page') . ' ' . get_query_var('paged');
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
    }

    echo '</div>';

  }
}
?>

After that put following code in header.php file.

<?php if (function_exists('wordpress_breadcrumbs')) wordpress_breadcrumbs(); ?>

After putting above code we can see the breadcrumbs as following;

Home » Parent Page » Sub Page1 » Sub Page2

Home » Category » Subcategory » Post Name

How to create breadcrumbs in wordpress
How to create breadcrumbs in wordpress

Code has been taken from above Source

create wordpress custom post type permalink structure

WordPress tutorial for, How to create wordpress custom post type permalink structure. Here we creating the Product post type with permalink.  we given code.

How to create wordpress custom post type permalink structure

Please open the functions.php file and put following code in that file.

/*
 * product custom post type added with new permalink
 */
function productposttype_with_custom_permalinks() {
register_post_type('product', array(
'label' => __('My product'),
 'singular_label' => __('product'),
 'show_ui' => true,
 'capability_type' => 'post',
 'hierarchical' => false,
 "supports" => array("title", "editor", "thumbnail", "author", "comments"),
 'taxonomies' => array('category', 'post_tag'), // this is IMPORTANT
'rewrite' => array('slug' => 'product'),
 'public' => true
    ));

//register_taxonomy( 'product-category', 'product', array ('hierarchical' => true, 'label' => __('product Categories'), 'rewrite' => array( 'slug' => 'product-category', 'with_front' => false ),));  // portfolio categories

add_rewrite_tag('%product%', '([^/]+)');
$extra_post_types = get_post_types(array('_builtin' => false, 'publicly_queryable' => true));
if (empty($extra_post_types))
return;
add_rewrite_tag('%post_type%', '(' . implode('|', $extra_post_types) . ')');
add_permastruct('product', '/%post_type%/%year%/%monthnum%/%day%/%product%/', true, 1);
}

/*
 * Funcation onload added the product custom post type
 */
add_action( 'init', 'productposttype_with_custom_permalinks' );

/*
 * product custom post added the permalink hook for enable the custom permalink
 * product custom post type
 */
function productposttype_with_product_permalink( $link, $post, $leavename, $sample ){
  if( 'product' != $post->post_type )
    return $link;
  $rewritecode = array(
    '%year%',
    '%monthnum%',
    '%day%',
    '%hour%',
    '%minute%',
    '%second%',
    $leavename? '' : '%postname%',
    '%post_id%',
    '%post_type%',
    $leavename? '' : '%pagename%',
    $leavename? '' : '%product%',
  );
  $unixtime = strtotime($post->post_date);
  $date = explode(' ', date('Y m d H i s', $unixtime));
  $replace_array = array(
    $date[0],
    $date[1],
    $date[2],
    $date[3],
    $date[4],
    $date[5],
    $post->post_name,
    $post->ID,
    $post->post_type,
    $post->post_name,
    $post->post_name,
  );
  $path = str_replace($rewritecode, $replace_array, $link);
  return $path;
}

add_action( 'post_type_link', 'productposttype_with_product_permalink', 10, 4 );

If you have any issues or problem with permalink then please write to me.

How to create wordpress custom post type permalink structure
How to create wordpress custom post type permalink structure

Show selected tags in wordpress using Selective Tag Cloud Widget plugin

Some time people want to show Show selected tags in wordpress from all site. Through selective Tags plugin, you can select the post tags which you want to show. We are very happy to launch the new wordpress plugin called Selective Tags.

How to install Selective Tag Cloud Widget?

Follow the usual routine;

  1. Open WP admin – Plugins – Add New
  2. Enter “Selective Tag Cloud Widget” under search and hit Enter
  3. Plugin will show up as the first on the list, click “Install Now”

Or if needed, upload manually. Follow the steps below to install the plugin.

  1. Upload the Selective Tag Cloud Widget directory to the /wp-content/plugins/directory
  2. Activate the plugin through the ‘Plugins’ menu in wp
  3. Go to “Selective Tag Cloud Widget” option to configure the button

Show selected tags in wordpress using selective Tags plugin

Some time people want to show the only selective tags from all site. Through this widget you can select the post tags which you want to show on your site.

More information about Plugin as follows:

Selective Tag WordPress Plugin, provides sidebar widgets which can be used to display tags in the sidebar.

Selective Tag

Sidebar Widget

Selective Tag WordPress Plugin, provides sidebar widgets which can be used to display tags in the sidebar. You can have multiple widgets with different set of tags configured for each one of them.

Each widget allows you to choose

  • The set of tags displayed which is selected by admin
  • Admin can select tag to show in sidebar. Auto complete Text box for Tags.
  • When try to type tags, It will suggest you the used tags from wordpress site.
You can download the plugin from following location.

Screenshot

Show selected tags in wordpress using selective Tags plugin
Show selected tags in wordpress using selective Tags plugin

wordpress theme wysiwyg editor default css

Many wordpress developers are creating wordpress theme. Many times when writing in editor and applying formatting. Formatting is not exactly coming in theme. we given wordpress theme wysiwyg editor default css.

Some time image alignment and photo caption is not coming properly.

wordpress theme wysiwyg editor default css

When you are creating new wordpress theme or taking theme from anyone put following CSS code in style.css file. Make sure following CSS code is present in your wordpress theme.

 /* =WordPress Core
 -------------------------------------------------------------- */
 .alignnone {
 margin: 5px 20px 20px 0;
 }

.aligncenter, div.aligncenter {
 display:block;
 margin: 5px auto 5px auto;
 }

.alignright {
 float:right;
 margin: 5px 0 20px 20px;
 }

.alignleft {
 float:left;
 margin: 5px 20px 20px 0;
 }

.aligncenter {
 display: block;
 margin: 5px auto 5px auto;
 }

a img.alignright {
 float:right;
 margin: 5px 0 20px 20px;
 }

a img.alignnone {
 margin: 5px 20px 20px 0;
 }

a img.alignleft {
 float:left;
 margin: 5px 20px 20px 0;
 }

a img.aligncenter {
 display: block;
 margin-left: auto;
 margin-right: auto
 }

.wp-caption {
 background: #fff;
 border: 1px solid #f0f0f0;
 max-width: 96%; /* Image does not overflow the content area */
 padding: 5px 3px 10px;
 text-align: center;
 }

.wp-caption.alignnone {
 margin: 5px 20px 20px 0;
 }

.wp-caption.alignleft {
 margin: 5px 20px 20px 0;
 }

.wp-caption.alignright {
 margin: 5px 0 20px 20px;
 }

.wp-caption img {
 border: 0 none;
 height: auto;
 margin:0;
 max-width: 98.5%;
 padding:0;
 width: auto;
 }

.wp-caption p.wp-caption-text {
 font-size:11px;
 line-height:17px;
 margin:0;
 padding:0 4px 5px;
 }

&nbsp;

Above CSS code is very important for every wordpress theme.

wordpress-default css for wordpress theme
wordpress-default css for wordpress theme

wordpress 3.3 version is now ready for testing

WordPress 3.3 is ready for testers. Following changes has been fixed in wordpress 3.3 version.

wordpress 3.3 version is now ready for testing

  • Updated the Blue theme
  • Fixed IE7 and RTL support
  • Improved flyout menu styling and fixed several glitches
  • Finished the Pointers implementation
  • Landed the dashboard Welcome box for new installs
  • Improved contextual help styling
  • Tweaked the admin bar a little more
  • Fixed a bunch of bugs
  • Media uploader
  • Improved admin bar
  • Fly out admin menus

As always, this is software still in development and we don’t recommend that you run it on a production site — set up a test site just to play with the new version. If you break it (find a bug), please report it, and if you’re a developer, try to help us fix it.

If all goes well, we hope to release WordPress 3.3 by the end of November. The more help we get with testing and fixing bugs, the sooner we will be able to release the final version. If you want to be a beta tester,

Download 3.3 Beta 2.

wordpress-3-3
wordpress-3-3