WordPress.org

Forums

[resolved] how to add class on next/previous_post_link (9 posts)

  1. anthar01
    Member
    Posted 2 years ago #

    Hi, i'm new to WordPress theme development and i have problem adding class to previous/next_post_link, i need a class for A tag

    class="base_button"

    as seen below

    <div class="grid_8 alpha omega navigation">
    
                <div class="nav-next">
                    <a class="base_button" href="#">Next Posts →</a>
                </div>
    
                <div class="nav-previous">
                    <a class="base_button" href="#">← Previous Posts</a>
                </div>
    
            </div>

    i'm planning to accomplish this through filters, tnx.

  2. anthar01
    Member
    Posted 2 years ago #

    i'm not sure this is the best way wherein i copied the WP's previous_post_link() & next_post_link() functions and placed them inside my theme's functions.php, edited the code, renamed the functions and use them and i got the result i wanted.

  3. bcworkz
    Member
    Posted 2 years ago #

    It's a bit overkill, if the default functions get updated, your blog will not be able to benefit. You could have just altered the link contents just before they are echoed out by hooking the filters 'next_post_link' and 'previous_post_link'.

  4. anthar01
    Member
    Posted 2 years ago #

    function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    	adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
    }
    
    function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    	adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
    }
    
    function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
    	if ( $previous && is_attachment() )
    		$post = & get_post($GLOBALS['post']->post_parent);
    	else
    		$post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
    
    	if ( !$post )
    		return;
    
    	$title = $post->post_title;
    
    	if ( empty($post->post_title) )
    		$title = $previous ? __('Previous Post') : __('Next Post');
    
    	$title = apply_filters('the_title', $title, $post->ID);
    	$date = mysql2date(get_option('date_format'), $post->post_date);
    	$rel = $previous ? 'prev' : 'next';
    
    	$string = '<a href="'.get_permalink($post).'" rel="'.$rel.'">';
    	$link = str_replace('%title', $title, $link);
    	$link = str_replace('%date', $date, $link);
    	$link = $string . $link . '</a>';
    
    	$format = str_replace('%link', $link, $format); 
    
    	$adjacent = $previous ? 'previous' : 'next';
    	echo apply_filters( "{$adjacent}_post_link", $format, $link );
    }

    given the above code from link-template.php, how to use filter to add class on A tag?

  5. bcworkz
    Member
    Posted 2 years ago #

    Untested, but should work:

    function ant_add_class($format){
      $format = str_replace('href=', 'class="base_button" href=', $format);
      return $format;
    }
    add_filter('next_post_link', 'ant_add_class');
    add_filter('previous_post_link', 'ant_add_class');
  6. anthar01
    Member
    Posted 2 years ago #

    i've tested it and it works. Thanks.

    i'm confused, previous_post_link and next_post_link calls for adjacent_post_link which then ECHOs the output. as i understand, add_filter() executes the 1st parameter then the second. in add_filter('next_post_link', 'ant_add_class'); the first parameter does an echo, not a return. so how did ant_add_class able to modify the link inside $format? thanks again for your further explanation.

  7. bcworkz
    Member
    Posted 2 years ago #

    You misunderstand where the data is coming from and where it's going :)
    For add_filter(): First parameter (parm) is just an identifying tag assigned by the author who wrote the initiating filter call (apply_filters()). The second parm is our function name that will manipulate parameters passed from the apply_filters() function.

    So you must look at the initiating apply_filters() call to see what parameters are being sent to our function. For apply_filters(): The first parm is again that arbitrary tag identifying the filter. In this case it's a variable tag, but conveniently, the possible values are in the preceding line. We are usually are not so lucky. The remaining parms are what can be passed to our filter function. In this case, we are only interested in $format. To use more than one parm, a different syntax is needed.

    So our function takes the value passed in $format, modifies it, and returns it. Also consider there may be other filter functions waiting in line to modify $format. They each receive the latest value, modify it, and return it, and so it goes on to the next filter function in line.

    The final returned value eventually gets returned by the original apply_filters(). In this case, the returned value is immediately echoed out to the client browser.

    I hope this helps your understanding of filters. Cheers.

  8. anthar01
    Member
    Posted 2 years ago #

    Thanks, you have better explanation than WP doc filters :) and really helped a lot. So I was able to move on and created menu...

    http://core.trac.wordpress.org/browser/tags/3.4.2/wp-includes/nav-menu-template.php
    inside my header.php has
    wp_nav_menu(); and prints below

    <div class="menu">
    <ul>
    <li>page1</li>
    <li>page2</li>
    </ul>
    </div>

    so i wanted to remove the div wrapper and created the functions inside functions.php

    function ant_mod_menu($nav_menu)
    {
       $find = array('<div class="menu">','</div>');
       $replace =  array('','');
       $nav_menu = str_replace($find,$replace,$nav_menu);
       return $nav_menu;
    }
    add_filter('wp_nav_menu','ant_mod_menu');

    but it has no effect >.<

  9. bcworkz
    Member
    Posted 2 years ago #

    Well, at least you're making progress and have a good understanding of filters! I don't see any fundamental problems with your latest code, so it should just take some basic debugging to get it working. Good luck.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.