Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1154624
    Paul R
    Moderator

    Hi,

    In that case, you can change is_category() with the code below.

    
    $post_categories = wp_get_post_categories( get_the_ID() );
    if (in_array("17", $post_categories)) {
        
    }
    

    Further customizations from here would be getting into custom development, which is outside the scope of support we can offer. If you need more in depth changes, you may wish to consult with a developer. X is quite extensible with child themes, so there are plenty of possibilities. Thanks for understanding.

    #1155163
    necxelos
    Participant

    I’m fine and happy with not going further with customization, background and disabling single post view is enough for me. If it worked…

    Sadly, Your code doesn’t work either so I’m still stuck in the same place πŸ™

    Here is my entire content.php file:

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <div class="entry-featured">
        <?php x_featured_image(); ?>	
    
    	<?php 
    	if(is_category('17')){
    	$background = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'full' ); 
    	} ?>	
    
      </div>  
    
    <div class="entry-wrap" style="background-image: url('<?php echo $background[0]; ?>');">
      
        <?php echo do_shortcode('[share_custom title="Share this Post" facebook="true" twitter="true" google_plus="true" linkedin="true" pinterest="true" reddit="true" email="true"]'); ?>		
    	<?php x_get_view( 'integrity', '_content', 'post-header' ); ?>
        <?php x_get_view( 'global', '_content' ); ?>
      </div>
      <?php x_get_view( 'integrity', '_content', 'post-footer' ); ?>
    </article>

    Pasting Your code instead of if(is_category(’17’)){ line just made the entire code not work at all (instead of working inside specific archive like before).

    #1155309
    Lely
    Moderator

    Hi There,

    I did test the code suggested by Paul and it’s working:

    <?php
    
    // =============================================================================
    // VIEWS/INTEGRITY/CONTENT.PHP
    // -----------------------------------------------------------------------------
    // Standard post output for Integrity.
    // =============================================================================
    
    ?>
    
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <div class="entry-featured">
        <?php x_featured_image(); ?>
      </div>
    <?php 
    	$post_categories = wp_get_post_categories( get_the_ID() );
    	if (in_array("9", $post_categories)) {
    		$background = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'full' ); 
            ?>
            <div class="entry-wrap" style="background-image: url('<?php echo $background[0]; ?>');">
            <?php
    	}else{
            ?> 
            <div class="entry-wrap">
            <?php } ?>	
    
        <?php echo do_shortcode('[share_custom title="Share this Post" facebook="true" twitter="true" google_plus="true" linkedin="true" pinterest="true" reddit="true" email="true"]'); ?>		
    	<?php x_get_view( 'integrity', '_content', 'post-header' ); ?>
        <?php x_get_view( 'global', '_content' ); ?>
      </div>
      <?php x_get_view( 'integrity', '_content', 'post-footer' ); ?>
    </article>

    On sample code above, I am using category ID 9. See this:http://screencast-o-matic.com/watch/cDjwosjei9. It is showing featured image and then the content has background image too. For that specific post that is assign to category 9, background image is working. Other post that belongs to other categories displays no background image as expected.

    #1155355
    necxelos
    Participant

    I know what is the issue now. Let me use example from my previous post in this topic again (because it’s great to make my point). My current (fictional) structure of categories looks like that (cars example for simplicity):

    Level 0 (parent of all) – All Vehicles
    Level 1 (children of level 0) – All Vehicles: Cars, Boats, Planes
    Level 2 (children of level 1) – Cars: Off-Road Cars, Sport Cars
    Level 3 (children of level 2) – Off-Road Cars: Jeep, Hummer

    – Level 3 is my lowest level category assigned to a post while creating it.
    BUT
    – Level 1 is a category, which ID I need to use (and I’m using) to decide whether post should have it’s background changed or not.

    This causes the issue. I tested Your code against my lowest level category and then it worked, but it won’t do (I plan to have pretty extensive/huge categories tree once I go live – it’s gonna be scientific site after all).

    #1155590
    Jade
    Moderator

    Hi there,

    As much as we want to help you get this sorted but this involves deeper customization which goes beyond the scope of support we can offer. If you need more in depth changes, you may wish to consult with a developer. X is quite extensible with child themes, so there are plenty of possibilities. Good luck!

    #1155885
    necxelos
    Participant

    Honestly I’ve got no idea how much many lines of code would be needed to make Lely’s code work with post category + all its parents instead of just post category. If You say it’s too much I’ll stick to what I’ve got from Lely already and copy this code several times for every category I need. It’s not a good practice but should do.

    So let’s get back to my other question that was buried and forgotten few posts ago: disabling single post view. I’ve got the code from Rad to use in my functions.php that does work on every post (not posts from one category) and make the page jump to the top:

    add_filter('post_link', 'do_not_link', 999, 3);
    function do_not_link (  $url, $post, $leavename=false ) {
    if ( get_post_format() !== 'quote' && !is_admin() ) {
    return '#'; // This one or the one with 'javascript:void(0)' instead
    }
    return $url;
    }

    And I’ve got code from Lely to use in my functions.php that I can’t make to work:

    $post_categories = wp_get_post_categories();
    $target_categories = array( 17);
    if ( ( get_post_format() !== 'quote' && in_array( $target_categories, $post_categories ) ) && !is_admin() ) {
    return 'javascript:void(0)';
    }
    #1156197
    Rad
    Moderator

    Hi there,

    In my example here https://community.theme.co/forums/topic/blog-posts-customization-questions-part-4/#post-1152874, it uses OR operator ( || ) instead of AND operator ( && )

    $post_categories = wp_get_post_categories();
    $target_categories = array( 834, 2312, 9823);
    
    if ( ( get_post_format() !== 'quote' || in_array( $target_categories, $post_categories ) ) && !is_admin() ) {
    return 'javascript:void(0)';
    }

    All you have to do is change the category.

    $target_categories = array( 17 );

    What’s confusing is, you wish to apply it on “quote” post format, as well as for posts under category 17. The question is do you wish to apply it to posts under category 17 regardless of post type? Or apply it to “quote” post format and to any posts under category 17? Or mixed? Like this,

    $post_categories = wp_get_post_categories();
    $target_categories = array( 17 );
    
    if ( ( get_post_format() !== 'quote' || ( get_post_format() !== 'quote' && in_array( $target_categories, $post_categories ) ) ) && !is_admin() ) {
    return 'javascript:void(0)';
    }

    Thanks.

    #1157001
    necxelos
    Participant

    Ok I’ll rephrase because I created some unintentional confusion by adding side-questions (not necessarily relevant to the main topic), which made my question look like far bigger issue than it really is:
    – Post type “Quote” from Integrity, was just a side question. I only wanted to know what it does, and after realising it doesn’t do much except just being separate post type I ruled this out. So I don’t need this conditional part at all.
    – I need only conditional that disables single post view for a standard blog post from specified category (or tag).
    – I used this code of Yours (which sadly doesn’t work for me):

    $post_categories = wp_get_post_categories();
    $target_categories = array( 17 );
    
    if ( ( get_post_format() !== 'quote' || ( get_post_format() !== 'quote' && in_array( $target_categories, $post_categories ) ) ) && !is_admin() ) {
    return 'javascript:void(0)';
    }

    …but I used it just “as it is” in my functions.php. Because nobody corrected me there I assumed it’s OK. So for the clarification: should this code go “just as it is” and should this code go inside functions.php file?

    #1157571
    Rad
    Moderator

    Hi there,

    It should go based on recommended changes here on your thread πŸ™‚

    And if it only targets specific posts from a catgeory regardless of post format. Then this should do it,

    $post_categories = wp_get_post_categories();
    $target_categories = array( 17 );
    
    if ( in_array( $target_categories, $post_categories )  && !is_admin() ) {
    return 'javascript:void(0)';
    }

    And I’ll summarize it here.

    add_action( 'template_redirect', 'redirect_single_post_to_blog_home_page' );
    
    function redirect_single_post_to_blog_home_page() {
    
    $post_categories = wp_get_post_categories();
    $target_categories = array( 17 );
    
      if ( is_singular('post') && in_array( $target_categories, $post_categories )  && !is_admin() ) {
        wp_redirect( home_url(), 301 );
        exit;
      }
    
    }
    
    add_filter('post_link', 'do_not_link', 999, 3);
    
    function do_not_link (  $url, $post, $leavename=false ) {
    
    $post_categories = wp_get_post_categories();
    $target_categories = array( 17 );
    
    if ( in_array( $target_categories, $post_categories )  && !is_admin() ) {
    return 'javascript:void(0)';
    }
    
    return $url;
    
    }

    Thanks!

    #1157771
    necxelos
    Participant

    Another big step πŸ™‚

    Both blocks of code (redirect block and url change block) work except exactly this part:

    
    in_array( $target_categories, $post_categories )

    …but on a wild guess while googling functions in the wordpress documentation I came across in_category() function and used:

    
    in_category($target_categories)

    …instead and it worked completely.

    a) Out of curiosity: any idea why in_category() worked and in_array() didn’t? Or maybe there is something wrong with wp_get_post_categories(), AKA it doesn’t pull the correct data while being used here?
    b) Is what I did correct or will it explode in my face later?
    c) Giving # to url was jumping the page to the top without reloading, giving ‘javascript:void(0)’ to url jumps page to the top with reloading. So I assume there is no way to just stay on the same place in the page after clicking the link no matter what?

    Great thanks as always Rad, You and Lely are still the best πŸ™‚

    #1158039
    Paul R
    Moderator

    Hi,

    Would you mind providing us with login credentials so we can take a closer look? To do this, you can make a post with the following info:

    – Link to your site
    – WordPress Admin username / password
    – FTP credentials

    Don’t forget to select Set as private reply. This ensures your information is only visible to our staff.

    #1158490
    necxelos
    Participant

    I can’t, which I stated in initial post of this topic (short version: localhost).

    Please refer to the post #1157771 (preferably Rad or Lely, because those two awesome people helped me create the piece of code we’re discussing in the first place), thank You πŸ™‚

    Have a good day everyone πŸ™‚

    #1159007
    Rad
    Moderator

    Hi there,

    a. Maybe the returned result is an object instead of an array. The in_category() works best, only use in_array() if you’re sure if it’s array.

    b. It should be okay

    c. The javascript:void(0); shouldn’t reload. It’s a javascript equivalent of do nothing ( NULL ). Try inspecting the HTML code if it’s correctly adding that code within the href=””. Or simply make href empty, or add ID to each of your items and use that ID as href’s hash. So when clicked, it will stay on same item.

    Thanks.

    #1159065
    necxelos
    Participant

    a & b) I found a solution/right function (to a tiny thing but still) on my own and it is a right one? Now I’m even more happy. Thanks Rad πŸ™‚

    c) FireBug inspect says <a href="">, instead of <a href="javascript:void(0);">. Inserting javascript:void(0); directly to <a href=""> using FireBug properly disables the link. Wild guess: maybe this javascript:void(0); is “used” instead of passed to URL as a text somehow?

    #1159372
    Rad
    Moderator

    Hi there,

    Yes, it should be the right now.

    And yes, it’s probably stripped down by other filters. How about this,

    if ( in_array( $target_categories, $post_categories )  && !is_admin() ) {
    return '#null';
    }

    Then add this CSS to Admin > Appearance > Customizer > Custom > CSS

    a[href="#null"] {
    pointer-events: none !important;
    }

    Hope this helps.

  • <script> jQuery(function($){ $("#no-reply-1146799 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>