Community Forums › Forums › Archived Forums › General Discussion › Conditional tags in Agentpress Singe Listings
- This topic has 11 replies, 2 voices, and was last updated 11 years, 1 month ago by DaleSue.
-
AuthorPosts
-
March 22, 2013 at 10:15 am #30455DaleSueMember
Hi,
this is driving me nuts!
I am trying to add content (a gravity form) by using hooks and a conditional tag.
I would the content to be displayed only on the single listing pages. That is to say when the template single-listing.php is used for displaying the content.
So I created the following code snippet:
<?php
if(is_page_template( 'single-listing.php' )) { ?>
[gravityform id=4 title=false description=false ajax=true]
<?php }
?>But it simply does not work and I have no idea why it is not working? I would really appreciate if someone could help me out here.
Regards
Dale
March 22, 2013 at 10:30 am #30458Brad DaltonParticipantMarch 22, 2013 at 10:49 am #30462DaleSueMemberHi Brad and thanks for your quick reply.
I am using the simple hooks plugin to execute the code but I used your example in order to create the following snippet:
function gravity_before_comments() {
if(is_page_template( 'single-listing.php' )) {
echo do_shortcode('[gravityform id=4 title=false description=false ajax=true]');
}
};
add_action('genesis_before_comments', 'gravity_form_before_content');Then I pasted the code into the functions.php file. And of course I cannot make it work. So I would appreciate some help here...
And why did not my previous code work? As I understand it should work.
Regards
Dale
March 22, 2013 at 11:31 am #30476Brad DaltonParticipantfunction gravity_form_before_content() { if (is_page_template( 'single-listing.php' ) ) { echo do_shortcode ('[gravityform id=4 title=false description=false ajax=true]'); } }; add_action('genesis_before_comments', 'gravity_form_before_content');
This code should work.
There was 2 problems with your code:
1. Your function has 2 different names:
gravity_before_comments
gravity_form_before_content2. Sometimes when you copy and paste code the apostrophe can get messed up and change.
Always copy and paste from the raw version and use a text editor like Notepad++ to edit code.
March 22, 2013 at 12:46 pm #30526DaleSueMemberHi again and thanks a lot for your effort.
I copied the code.
I pasted the code in the functions.php file of the Agentpress child theme.
And it does not work.
I have tried to change the code in every possible direction but it does simply not work.
If I put the shortcode for gravity forms in the the Simple Hooks plugin it displays perfectly so the issue is not the Gravity Form. But I cannot figure what it is in the code that does not work.
Again very thankful for any help I can get here.
/Dale
March 22, 2013 at 12:56 pm #30531DaleSueMemberHi again,
I have also tried to paste the following code into the Simple Hooks plugin:
<?php if(is_page_template( 'single-listing.php' ) ) { ?> [gravityform id=4 title=false description=false ajax=true] <?php } ?>
This does not work either and I cannot understand why. The code seems to be ok in every aspect or am I using any invalid arguments. This one should work!
/Dale
March 22, 2013 at 9:58 pm #30625Brad DaltonParticipantMarch 23, 2013 at 8:47 am #30670DaleSueMemberHi and thanks again.
Here is an url to a page where the form is displayed below the Google map.
The form is executed with the code [gravityform id=4 title=false description=false ajax=true] in the genesis_before_comments hook using the Simple Hooks plugin.
/Dale
March 23, 2013 at 11:24 pm #30829Brad DaltonParticipantLooks like the form is working fine to me. Did you clear your browser cache or is there something i am missing?
What you could try is:
if ( is_page ( '007' ) )
using the post i.d (ex. 007) for the page rather than the template name.
March 24, 2013 at 12:26 am #30835DaleSueMemberIt works just fine while I am adding it through Simple Hooks and only the shortcode.
The problem is that the form is displayed on all pages and post and I would like it to only be displayed when the single-listing.php file is used. That is to say only when a 'listing' in Agentpress is displayed.
And I thought php with a conditional tag would be the solution but for some reason I cannot make it work even though the code seems to be correct.
Your example with using the page id will be complicated since we will have more than thousand posts 🙂
So to sum up - I am looking for a solution for the form to be displayed only when a 'listing' (which uses the single-listing.php file) is displayed.
to exclude certain pages or
March 25, 2013 at 2:11 am #31064Brad DaltonParticipanti think you may need to post a question in the Gravity forms forum.
March 25, 2013 at 1:53 pm #31164DaleSueMemberI finally figured it out!
I still do not know why I cannot make it work with the is_template tag but I found the following in the WP Codex:
Due to certain global variables being overwritten during The Loop is_page_template() will not work. In order to use it after The Loop you must call wp_reset_query() after The Loop.
Maybe that is the explanation. I am not sure since I really do not understand what they are saying 🙂
However, here is the code for displaying a gravity form only on listings when using Agentpress.
<?php if (is_singular('listing') ): echo gravity_form(4, false, false, false, '', false); endif; ?>
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.