Yoast breadcrumbs no separator

I was working on a project where I wanted to use WordPress Yoast SEO plugin breadcrumbs, but don’t wanted to use a string separator. I wanted to style <span>’s in the breadcrumb with a right CSS border radius.

I wrote this hook to remove the seperator:

// Filter hook Yoast seo breadcrumbs no seperator
function no_separator( $output ) {
 $replace = array( '&raquo;' );
 $output = str_replace( $replace, '', $output );
 return $output;
}
add_filter('wpseo_breadcrumb_single_link_with_sep', 'no_separator');

 

 

Advertisements

WordPress blog url without http

If you want to show your WordPress URL without http, you can do it by a str_replace function in combination with the WordPress get_bloginfo(‘wpurl’) function, as follows:

Add al the protocols that you want to remove in an array, and then str_replace the get_bloginfo(‘wpurl’) function.

<?php
 $protocols = array('http://', 'http://www.', 'www.');
 echo str_replace($protocols, '', get_bloginfo('wpurl'));
 ?>

That’s it!

WordPress disable dashboard for non-admin

By adding this function to your functions.php, non-admin users can’t approach the WordPress admin / dashboard.

// No WordPress admin for non-administrators
add_action('admin_init', 'no_mo_dashboard');
function no_mo_dashboard() {
 if (!current_user_can('manage_options') && $_SERVER['DOING_AJAX'] != '/wp-admin/admin-ajax.php') {
 wp_redirect(home_url()); exit;
 }
}

I have used this in a project where I created a complete dashboard in the frontend of the website, so where no non-admin user was allowed to visit the WordPress admin / dashboard.

 

WordPress show admin bar only for Admin

In some situations it is helpful to don’t show the WordPress admin bar to all the user types. By adding the following code to your function.php, the admin bar only shows up when your logged in as Administrator.

// show admin bar only for admins
if (!current_user_can('manage_options')) {
 add_filter('show_admin_bar', '__return_false');
}

Related posts by author

For displaying related posts by author somewhere in your WordPress website you can use the following function:

function get_related_author_posts() {
 global $authordata, $post;

 $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );

 if( $authors_posts && count( $authors_posts ) >= 1 ) :
 $output = '<ul>';
 foreach ( $authors_posts as $authors_post ) {
 $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a>' . apply_filters( 'the_excerpt', $authors_post->post_excerpt, $authors_post->ID ) . '</li>';
 }
 $output .= '</ul>';
 else :
 $output = '<div class="error"><p>'. __( 'Nog geen artikelen geschreven...' ) .'</p></div>';
 endif;
 return $output;

 } 

 /*add this hook where you need the related posts*/
 echo get_related_author_posts();
 ?>

Tip! If you want to use posts from a custom post type, you need to specify this in get_posts() by adding: ‘post_type’ => ‘post-type-name’.

WordPress search count results

You want to create a search page in WordPress where you count the results and show them on your page, for example: Searching for “apple” gives 3 results.

For getting this search counting you need to add the following code in your search file where you want to show this “counting sentence”.

First we make these variables:

<?php
$allsearch = &new WP_Query("s=$s&showposts=-1");
$key = wp_specialchars($s, 1);
$count = $allsearch->post_count;
?>

Then we use $key for showing the search query and $count for how much results.

<p>Searching for "<?php echo $key;?>" gives <?php echo $count;?> results.</p>

WordPress archive page for multiple post types

If you want to make an archive page for multiple post types you want to show the title of the post type first. You can do that by using:

<?php post_type_archive_title(); ?>

http://codex.wordpress.org/Function_Reference/post_type_archive_title

For showing the content you can use the normal loop. But if you want to modify the loop by query_post(); you have to make a variable for ‘post_type’ that automatically fills in the right post type name.

You can do that by using the get_queried_object(); function:

http://codex.wordpress.org/Function_Reference/get_queried_object

As follows:

<?php
$obj = get_queried_object();
$post_type = $obj->name;
$args = array(
  'post_type' => $post_type,
  'orderby' => 'title',
  'order' => 'ASC',
  'posts_per_page' => 20,
 );
query_posts($args);
// And then the loop
?>

WordPress Alphabetical Glossary

I made this piece of code for a WordPress Alphabetical Glossary. It is a simple loop which makes a heading with the first character of the title, of the posts beginning with this character. Each group of posts gets a div called: “alpha_wrapper” around it, and the heading gets a class “alpha_title” so you can style it your own.

I made a alphabetical index too, so you can simply click to the right group of posts with the same first character. It is implemented in the Alphabetical Glossary loop. You can see that the headings with the class “alpha_title” have an ID corresponding with the character links in the “alphabetize” navigation.

<!-- The Alphabetical Index -->
<nav id="alphabetize">
  <?php foreach (range('a', 'z') as $char) { ?>
  <a href="#sort-<?php echo $char; ?>"><?php echo ucfirst($char); ?></a>
  <?php } ?>
  <div class="clear"></div>
</nav><!--End alphabetize-->

<!-- The Alphabetical Glossary -->
<div id="glossary">
if (have_posts()) { $curr_letter = ''; echo '<div class="alpha_wrapper">'; while (have_posts()) { the_post();
$title = get_the_title();
$title_array = explode(' ', $title);
$this_letter = substr($title_array, 0, 1);

   if ($this_letter != $curr_letter) {
      if (!empty($curr_letter)) {
      echo '<div class="clear"></div></div>
      <div class="alpha_wrapper">';
      }
   $curr_letter = $this_letter; ?> 
   <div id="sort-<?php echo strtolower($this_letter); ?>" class="alpha_title">
   <?php echo $this_letter; ?>
   </div><!--End alpha_title-->
   <?php } ?>

    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php } echo '</div>'; } ?>
</div><!--End glossary-->

If you have questions about it, please comment.