Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1279496

    JfantasyBooks
    Participant

    Hey there, Themeco Team!

    Name: JFantasyBooks
    Site: Fantasy-Books
    Site Url: https://fantasy-books.live
    WordPress Version: 4.6.1
    X Version: 4.6.4
    Cornerstone Plugin Version: 1.3.3

    Problem:

    So I have this code in my functions.php . It works, but the problem is I would like it to only work, where the shortcode shows only 1. when there is a post. I also want it to run only 2. during a particular category.

    I was given this for the category by x theme

    <?php if(is_category( 'category-slug' )): ?>
    /*your do_shortcode here*/
    <?php endif; ?>

    but I want to make sure where to place it so that it runs:

    My code:

    <?php
    
       
    add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );
    
      function Mainfunction1() {
    
           function Subfunction2(){ }
     
          ?>
    
     <div class="x-nav-1">	
    	<?php echo do_shortcode('[shortcode']);?> 
    </div>
    
      <?php
    
    }
    
    add_action('x_before_the_content_end', 'x_entry_navigation1');
    
    //============= Other Functions =======

    Thanks

    #1279568

    Rad
    Moderator

    Hi there,

    Thanks for writing in. Please try this

    <?php
    
       
    add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );
    
    function x_entry_navigation_1() {
    
    $categories = array(234, 123, 'awesome-slug', 923, 'another-slug');
    $category = get_queried_object(); 
    
    if ( is_category( $categories ) || $category->count > 0 ) :
    
    ?>
    <div class="x-nav-1">	
    	<?php echo do_shortcode('[shortcode']);?> 
    </div>
    <?php
    
    endif;
    
    }
    
    add_action('x_before_the_content_end', 'x_entry_navigation_1');
    
    //============= Other Functions =======

    You may add categories as many as you like, it could be category ID, or category slug.

    Hope this helps.

    #1279860

    JfantasyBooks
    Participant

    Unfortunately, it doesn’t work, and I’m not sure as to why. The code works without being selective about the posts by category, but once I add it that code you suggested… nothing. The whole shortcode no longer appears.

    I have added down below a more complete look of a general code at the moment. I hope that helps you make a decision.

    Also, there are two options I would like to be provided.

    1. Is having the shortcode appear only category.
    2. Is having the shortcode appear only on posts (where I don’t have to choose the category. I can just say it’s a post, so appear)

    And My Code has sub functions

    <?php
    
    //===== There are Statements here ====
    
    add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );
    
    $value;
    
      function Mainfunction1() {
       
       $categories = get_the_category();
       $category_id = $categories[0]->cat_ID;
    
         //=== A sub Function Down Below ===
    
           function Subfunction1(){
             
               switch ($category_id) {
                      
                      //==== there are cases here which declare what $value is equal to ===
                     }
                   return value; 
               }
    
    $get_value = value();
    //=== some other variables that are used in shortcode ===
          ?>
    
          <div class="x-nav-1">	
    	<?php echo do_shortcode('[shortcode']);?> 
          </div>
      <?php
    
    }
    
    add_action('x_before_the_content_end', 'x_entry_navigation1');
    
    //============= Other Functions =======

    Thank You,

    I know it might seem a bit like much, but this problem has been weighing me down for other things as well. If I get it right here, it could help me take care of the other problems.

    #1280171

    Rue Nel
    Moderator

    Hello There,

    Thanks for updating in! I am just confuse on what you wanted to achieve with the given shortcode. All I know is that you can simplify the code without any sub function (might not the best coding practice) into something like this:

    Having the shortcode appear only category:
    `function x_nav_function_1(){
    if (is_category( ‘category-slug’ )) : ?>

    <?php endif;
    }

    #1280172

    Rue Nel
    Moderator

    Hello There,

    Thanks for updating in! I am just confused on what you wanted to achieve with the given shortcode. All I know is that you can simplify the code without any sub function (might not the best coding practice) into something like this:

    Having the shortcode appear only category:

    function x_nav_for_category(){
      if ( is_category( 'category-slug-a' ) ) : ?>
    
        <div class="x-nav-1">	
          <?php echo do_shortcode('[shortcode']);?> 
        </div>
    
      elseif ( is_category( 'category-slug-b' ) ) : ?>
    
        <div class="x-nav-1">	
          <?php echo do_shortcode('[shortcode']);?> 
        </div>
    
      <?php endif;
    }
    add_action('x_before_the_content_end', 'x_nav_for_category');

    Having the shortcode appear only on posts :

    function x_nav_for_single_post(){
      if ( is_single() ) : ?>
    
        <div class="x-nav-1">	
          <?php echo do_shortcode('[shortcode']);?> 
        </div>
    
      <?php endif;
    }
    add_action('x_before_the_content_end', 'x_nav_for_single_post');

    For more details about WordPress conditional tags, please check out the codex here: https://codex.wordpress.org/Conditional_Tags

    #1281256

    JfantasyBooks
    Participant

    Thanks it worked!

    #1281258

    Rue Nel
    Moderator

    Hello There,

    You’re welcome! We are just glad we were able to help you out.
    Thanks for letting us know that it has worked for you.

    Cheers.