add button to content-single-listing-classifieds.php


  • Marie
    Newbie
     
    3 points

    I am wanting to add a button to the Listing Page next to the Contact Seller button that links to another page on the site called /guaranteed-diamond-transactions/
    BUT I only want this button to show up if the item is in the category ID 280 /listing-category/diamond-rings/

    I understand I need to code it within content-single-listing-classifieds.php , to use PHP code to add this condition with category ID.

    I edited the PHP code on that page, I used this code but it didn’t work:


    <?php
    if ( in_category('diamond-rings') ) {
    ?>

    Guaranteed Diamond Transactions

    <?php
    }
    ?>

    Can anyone help?


    JamesD
    JamesD
    Elite Member
     
    507 points

    Hello Marie,

    I’m not positive, but I don’t believe the WP “in_category()” function works with PP categories.

    Here’s a “non core altering” option you can try, but keep in mind that it’s just a basic example to give you the ‘gist’ of one of the ways to go about it. You’ll also want to customize it more as well as possibly add more styling.

    Put the following in your ‘functions.php’ file:

    	function add_button_cat_func() {
    	    /**
    	       The category name to display additional button.
                Make name all lower case!
    
                Use either the category slug or category ID.
            */
    	    $sCatToShowBtn = ''; /** Enter either the PP category ID or name here. */
    
    	    /** Only Edit Anything Below For Further Customization */
    	    if(empty($sCatToShowBtn)) { return; }
    	    global $post;
            $catid = $post->ID; /** Retrieve PP Category ID */
            if(empty($catid)) { return; }
            $catname = strtolower(get_cat_name($catid)); // Retrieve PP Category Name
            if($catid == $sCatToShowBtn || $catname == $sCatToShowBtn){
                /** Found matching category by ID */
                echo <<< FOUNDIT
    <script>
        (function(){
    	   jQuery('a.wlt_shortcode_contact_button')
                .after('<a href="#wlt_shortcode_contactmodal_14" role="button" data-toggle="modal" class="btn btn-lg btn-success active  wlt_shortcode_contact_button">My Add-on Button</a>');
        })();
    </script>
    FOUNDIT;
    	    }
    	}
    	add_filter('hook_container_after', 'add_button_cat_func');
    

    The example above basically adds a duplicate button (opens the contact modal form).
    The jQuery to make changes to the button are between the “FOUNDIT”.

    I’ve tried to make it as easy as possible for you to change and hopefully you know enough to tweak it..

    The bonus is that you don’t have to touch any of the core files!

    Hope it helps.



    Marie
    Newbie
     
    3 points

    Thank you so much for taking the time to answer my question. I’ve added the code to my functions.php with the only change to add the category ID 280 between ” of $sCatToShowBtn = ”;

    Unfortunately nothing appears after the Contact Seller button for that category.

    Just to confirm, I enter the code exactly as it is? The new button just needs to link to a page if that helps simplify things.


    JamesD
    JamesD
    Elite Member
     
    507 points

    Hello Marie,

    Other than the cat ID/name setting, yes, everything should be placed in there as such (you can take out the comments if it clears things up for you)..

    Which theme/child theme are you using and do you have a URL to that cat page?



    Marie
    Newbie
     
    3 points

    I’m using Responsive Classifieds Theme with a child theme based on December.

    The url is myweddingshop dot com/wp/listing/test4/


    JamesD
    JamesD
    Elite Member
     
    507 points

    Hi Marie,

    Thanks for the info. Place this in your functions file instead:
    [EDITED – REMOVED SNIPPET]

    There was a problem trying to post the code, so I placed it in the attached file.

    Note: Replace “PLACE_URL_HERE” with the URL you want to use. and “My Add-on Button2” with your text.

    Attachments:
    You must be logged in to view attached files.
    This reply was awarded the best answer by the topic poster.


    Marie
    Newbie
     
    3 points

    Thank you so much! That worked perfectly.


    JamesD
    JamesD
    Elite Member
     
    507 points

    That’s great news Marie, my pleasure.
    Thanks for the update!


Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.