Jump to content
Sign in to follow this  
mpeesel

custom if/then statment for hooks

Recommended Posts

mpeesel

Hi, I'd very much like to create a hook that inserts a piece of short code into the template IF the category is defined as "homework". And I don't want it to show up anywhere else in the site. I have the hook defined so it shows a test in the site, but I don't know enough php to create this simple If/then statement. Essentially, it should say:

If Category = "homework" Print "[short_code]"
And if there is no category, then it also shouldn't appear. Of course, the short code also has to work here which is doesn't if you just stick it in there... Anyone have any ideas for this? Or am I trying to do this the wrong way? Thanks!! Mark

Share this post


Link to post
Share on other sites
Simon

Hi Mark. `if ( is_category( 'homework' ) ) do_shortcode( '[short_code]' );` - or - `echo ( ( is_category( 'homework' ) ) ? do_shortcode( '[short_code]' ) : 'nothing happening!';` Couple of examples to get you started ;)

Share this post


Link to post
Share on other sites
mpeesel

Thanks Simon, these look like they would work, but it isn't rendering the php. It's just displaying the code as is. Seems like it needs something to get the php running... Here's what I have in functions.php:

		// add_action('hook_name','function name');
		add_action('pagelines_after_theloop', 'homework_tags');
	
		function homework_tags(){
	
			?>
	
	if ( is_category( 'homework' ) ) do_shortcode( '[tagcloud]' );
	
			<?php }
	
		// end function
	
	

Share this post


Link to post
Share on other sites
Simon

Ahh well you have put the code between the php tags ;) `function homework_tags(){` `if ( is_category( 'homework' ) ) do_shortcode( '[tagcloud]' );` `}` `// end function`

Share this post


Link to post
Share on other sites
mpeesel

That would make sense, wouldn't it... I *think* we're getting closer. Maybe. Now nothing shows and I don't see anything in the source either. Here's what I have:

	// add_action('hook_name','function name');
	add_action('pagelines_after_theloop', 'homework_tags');
	
	function homework_tags(){
	
	if ( is_category( 'homework' ) ) do_shortcode( '[tagcloud]' );
	
	}
	// end function
	

Share this post


Link to post
Share on other sites
cmunns

Usually caused by a PHP syntax error. Any errors showing?

Share this post


Link to post
Share on other sites
mpeesel

Nope. Nada. I wish. Nothing appears, no error...

Share this post


Link to post
Share on other sites
Simon

Try this: `if ( is_category( 'homework' ) ) echo do_shortcode( '[tagcloud]' );` rest is the same

Share this post


Link to post
Share on other sites
mpeesel

Hey that's starting to work! It's working on the main category page, but not on posts that are categorized as 'homework'. Probably my fault for not specifying that, but I *assumed* it would work on posts AND the category landing page. Thanks for everyone's help! I'd love a bit more if possible. Thanks!

Share this post


Link to post
Share on other sites
timlinson

Try: `if ( is_category('homework') || in_category('homework') ) echo do_shortcode'[tagcloud]');`

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

×