• i need a help my requirement is

    if suppose i have a button on all single posts page now i want that button/url to be dynamic

    lets say if post category is diabetes i want user to be redirected to diabetes program.

    i cant go with ACF method has i have more than 2000 posts so adding links to all post would be tedious.

    can you suggest me some plugin or method through which i can fulfil my requirements

Viewing 1 replies (of 1 total)
  • https://www.dropbox.com/s/wjf2tvt4y6qpnce/add-category-button.zip?dl=0

    here’s a simple plugin that will create a link after the content of any post where the slug is a match in a case/switch statement. You’ll need to edit the php and css to suit your needs as your request doesn’t have enough information to make something fully fleshed out.

    the relevant code:

    function add_category_button_tug23f8(){
    	$found = 0;
    	if(is_single()) {
    		global $post;
    		$postcat = get_the_category( $post->ID );
    		$slug = $postcat[0]->slug;
    		$url = '';
    		
    		/* make the links */
    		switch ($slug) {
    			case 'pancakes':
    				$url = "/your-link-here/pancakes";
    				$found = 1;
    				break;
    			case 'waffles':
    				$url = "/your-link-here/wafles";
    				$found = 1;
    				break;
    			/* etc... */
    		}
    
    		if($found){
    			add_filter('the_content', function ($content) use ($postcat, $url) {
    					/* make the button */
    					$after = '';
    					$after .= '<div class="acb-wrapper">'."\n";
    					$after .= '<a href="'. $url .'" class="acb-button">'. $postcat[0]->name .' Program</a>'."\n";
    					$after .= '</div>'."\n";
    					$content = $content . $after;
    					return $content;
    			}, 10, 2);
    		}
    	}
    }

    The important stuff here is eg:

    case 'pancakes':
         $url = "/your-link-here/pancakes";
         found = 1;
         break;

    You set each case with a slug, then you set the url variable per slug.

    This might be a lot to do if you have a ton of categories you need this for but you didn’t provide any information as to your url structure or a link to your diabetes program.

    • This reply was modified 2 years, 1 month ago by tugbucket.
    • This reply was modified 2 years, 1 month ago by tugbucket.
Viewing 1 replies (of 1 total)
  • The topic ‘Need help with conditional redirect from post’ is closed to new replies.