Forum Replies Created

Viewing 15 replies - 1 through 15 (of 323 total)
  • Thread Starter berry metal

    (@erikalleman)

    There’s no point to target the grid it seems, because it adds the class too late.

    • This reply was modified 2 years, 12 months ago by berry metal.
    Thread Starter berry metal

    (@erikalleman)

    These 2 titles are also the same post type.
    My grid is my related posts grid.

    Meanwhile, I will ask the theme developer to see if he knows a way to target only my related posts titles, early enough – but the problem is that they also have the same “related” meta field as my breadcrumbs title, because my breadcrumbs title is my current post title, and my current post is also a related post for other posts – so probably there is no way to differentiate between the 2 using the “related” meta value.

    Thread Starter berry metal

    (@erikalleman)

    The goal is to only apply devidowing to my specific grid titles, but that turned out impossible since the grid is adding the class too late.
    So the goal is instead to add the dewidowing class globally with a conditional that excludes the title in my breadcrumbs title, because that would result the same:
    – only my grid titles need to be dewidowed
    – not the title in my breadcrumbs

    These titles are both present on the same “page” in every post.

    Thread Starter berry metal

    (@erikalleman)

    This is how my theme code looks that would target the title within my breadcrumbs, not sure if early enough:

    // Remove page title from breadcrumbs
    function my_remove_title_from_breadcrumbs( $trail ) {
    
        // Remove title (trail_end) for single posts
        if ( is_singular() ) {
            unset( $trail['trail_end'] );
        }
    
        // Return trail
        return $trail;
    
    }
    add_filter( 'mythemeprefix_breadcrumbs_trail', 'my_remove_title_from_breadcrumbs', 20 );
    Thread Starter berry metal

    (@erikalleman)

    Thanks for the suggestion.
    It does not work, it breaks the layout and removes content no matter what priority I use for it.

    So instead of adding the dewidowing class only to my grid block, I could use my previous code and add it to all titles again, and then try to remove the class from my breadcrumbs or from my template that contains my breadcrumbs, after it was added, and then I would achieve the same: it would only be added to my grid blocks in the end.

    But if it’s not possible to add the class before wp-Typography is looking for it, then I guess it’s not possible to remove it either before it’s looking for it, is that right?

    And because plugin code runs before theme code runs, theme code cannot remove it either early enough, or?

    So I thought I would add a conditional instead to exclude my sticky top bar template (that is a template within my post template) since I am not sure how to target only my breadcrumbs – the title in the breadcrumbs should not be dewidowed:

    add_filter( 'the_title', 'add_notypo_to_title', 10, 1, function add_notypo_to_title( $title ) {
            if (!is_singular()) {
                    $template = 8127;
                    return "<span class='noTypo'>$title</span>";
            }
    } );

    I have seen this structure in some of my theme code – function within a filter hook (it was actually an action hook), but I hope it works with filter hooks too.

    The problem is I am not sure that the priority and the args syntax is correct in my code above – could you advise?

    I want this code to add the dewidowing class to all titles except the title in my breadcrumbs, which is in my top bar template, hence I defined the ID of my template – but is this the correct way to exclude my template?

    This is my working theme code that I got inspired from for my code structure:

    add_action( 'mythemeprefix_hook_header_inner', function() {
            if (is_singular()) {
                    $template = 7027; // Change this to the templatera post ID you want to grab content from
                    echo do_shortcode( '[templatera id="' . $template . '"]' );
            }
    } );

    And the result is critical error, but it’s probably because of a syntax error.

    I tried to move the parameters to the end:

    add_filter( 'the_title', 'add_notypo_to_title', function add_notypo_to_title( $title ) {
            if (!is_singular()) {
                    $template = 8127;
                    return "<span class='noTypo'>$title</span>";
            }
    } 10, 1 );

    But this also results in a criticall error.
    What am I doing wrong?

    • This reply was modified 2 years, 12 months ago by berry metal.
    Thread Starter berry metal

    (@erikalleman)

    add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 1, 1 );
    
    // The render callback function allows to output content in cards.
    function prefix_my_block_render() {
    
        // Get current post, term, or user object.
        $post = wpgb_get_post();
    
        // Output the post title.
        echo '<h3><span class="noTypo">' . esc_html( $post->post_title ) . '</span></h3>';
    
    }

    Hi, after reading your reply I increased the priority to 1 to execute it earliest possibe, but even though the class is still added, wpTypography still doesn’t see the class, because the title is not dewidowed.

    So it’s still not added early enough.
    How do I troubleshoot this further?
    Thanks!

    • This reply was modified 2 years, 12 months ago by berry metal.
    Thread Starter berry metal

    (@erikalleman)

    Thanks for your reply. In that case I will try to sort this out with the grid developer.

    Thread Starter berry metal

    (@erikalleman)

    Hi,
    if the class is added, then it should work, so this question refers to why it doesn’t work if the class is added, not to my grid plugin.
    I don’t what are the requirements for it to work – that is what I am asking.
    It seems that it matters when is the class added.
    I attached the plugin code so you can see when is the class added.
    What should I tell to the grid developer about when should the class be added to make it work?
    If he knows when you need the class to be added in your plugin to make it work, then he can help perhaps to modify the code.
    I just need to know the requirement for when the class need to be added (not later than… some event).

    Best regards.

    Thread Starter berry metal

    (@erikalleman)

    Hi,

    I managed to add the class only to my related posts titles via grid code:

    add_filter('wp_grid_builder/the_content', [ 'WP_Typography', 'process' ] );
    
    function prefix_register_block( $blocks ) {
        
        // 'my_block' corresponds to the block slug.
        $blocks['wpgb_custom_title'] = [
            'name' => __( 'WPGB Custom Title', 'text-domain' ),
            'render_callback' => 'prefix_my_block_render',
        ];
    
        return $blocks;
        
    }
    
    add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 10, 1 );
    
    // The render callback function allows to output content in cards.
    function prefix_my_block_render() {
    
        // Get current post, term, or user object.
        $post = wpgb_get_post();
    
        // Output the post title.
        echo '<span class="noTypo">' . esc_html( $post->post_title ) . '</span>';
    
    }

    And the noTypo class is succesfully added, yet the titles are not dewidowed.

    Screenshot of the HTML:

    flameshot_screenshot

    Something is amiss here… could you advise what could be wrong?

    Because when I added the class previously with:

    function add_notypo_to_title( $title ) {
     
        return "<span class='noTypo'>$title</span>";
    }
    
    if (! is_admin() ) {
        add_filter( 'the_title', 'add_notypo_to_title', 10, 1 );
    }

    the dewidowing worked just fine.
    Is it something with the way the title is retrieved in my grid code?

    Thanks in advance,
    I hope you and your businesses are doing well,
    have a nice weekend.

    Thread Starter berry metal

    (@erikalleman)

    And this is the code that is adding the metabox for specifying related posts for each post:

    // Add related items setting for standard posts
    add_filter( 'wpex_metabox_array', function( $array, $post ) {
    
    	if ( 'post' == $post->post_type ) {
    
    	    $array['main']['settings']['related_post_ids'] = array(
    	        'title'         => __( 'Related Posts', 'total' ),
    	        'description'   => __( 'Comma seperated ID\'s for related items', 'total' ),
    	        'id'            => 'related_post_ids', 
    	        'type'          => 'text',
    	    );
    
    	}
    
        // Return fields
        return $array;
    
    }, 40, 2 );
    
    // Alter related items if the related_post_ids meta field is set
    // NOTE: for custom post types use the "wpex_related_{$post_type}_args" filter instead of "wpex_blog_post_related_query_args".
    add_filter( 'wpex_blog_post_related_query_args', function ( $args ) {
    	
    	if ( $related = get_post_meta( get_the_ID(), 'related_post_ids', true ) ) {
    		$args[ 'category__in' ] = null;
    		$args[ 'post__in' ] = explode( ',', $related );
    	}
    
    	// Return args
    	return $args; 
    
    } );
    Thread Starter berry metal

    (@erikalleman)

    This filter hook successfully outputs all my related posts that were assigned to the current post, in each post, into the grid with ID 3, which is in my post template:

    add_filter(
        'wp_grid_builder/grid/query_args',
        function( $query_args, $grid_id ) {
    
            global $post;
    
            if ( 3 === $grid_id ) {
    
                $referer = wp_get_referer();
                $post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID;
                $related = get_post_meta( $post_id, 'related_post_ids', true );
    
                if ( ! empty( $related ) ) {
    
                    $query_args['post__in'] = explode( ',', $related );
                    unset( $query_args['category__in'] );
    
                }
            }
    
            return $query_args;
    
        },
        10,
        2
    );

    Based on this hook it should be possible to write a conditional to limit adding the class to only the related posts titles.

    This is what I could write, but it’s wrong (critical error):

    function add_notypo_to_title( $title, $query_args ) {
     
     global $post;
     $referer = wp_get_referer();
                $post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID;
                $related = get_post_meta( $post_id, 'related_post_ids', true );
              $query_args = array(
              'post__in' => array( $post->ID )
    );
              $related_posts = get_posts($args);
    
        return "<span class='noTypo'>$title</span>";
    }
    
    if (! is_admin() ) {
     
        add_filter( 'the_title', 'add_notypo_to_title', 10, 2 );
    }

    Can someone help me out with this?
    I don’t need $grid_id, so I don’t care if it’s not available when the_title hook runs.
    I just need a correct conditional to limit adding the class to my related post titles.
    Question is, is $related available when the_title hook runs or how do I make it available?
    How do I determine that?
    What is the correct way to put this:
    'post__in' => $related
    into a conditional?

    Thanks in advance.

    Thread Starter berry metal

    (@erikalleman)

    Thanks,
    I just asked the grid developer about the templates, because I am not sure where they are and how do they work.

    Thread Starter berry metal

    (@erikalleman)

    If my related posts were added by specifying post IDs in a custom field in the post settings, does that imply that the related posts have some post meta that could be targeted in the conditional?

    Thread Starter berry metal

    (@erikalleman)

    Hi,

    there is no section, it’s simply the current post.

    In the current post I have 2 current post titles:
    the regular post title and the post title in the breadcrumbs.

    It’s not necessary to reveal my project, this is a generic PHP – WordPress question.
    Are there several current post conditionals in WordPress that can be defined?
    I only know about 1 current post, the one that is currently open in the browser – that is what I mean.

    The goal is to add the class to the title – only for my related posts.
    The related posts – like normally, are on the same page as the current post, in my case in a page builder column that is part of my post template, since I don’t use a sidebar.
    The related posts are assigned manually by specifying their ID in a field in the post settings – this is a feature of my theme.
    The related posts are fetched by a grid, but the variable $grid_id is not yet available at the time the global the_title hook is running, so I cannot target it- at least this is what I was told by the developer, I personally don’t have a thourough understanding of how my grid works.
    That being said, other parts of my post are also output by a grid, that has a different ID, hence I would need to target the grid ID, or to target the meta information if it exists – that refers to those posts as related posts, since they were defined as related posts by entering the post IDs in a field in the post settings.

    Thread Starter berry metal

    (@erikalleman)

    I will post to the other forums.
    Thanks.

Viewing 15 replies - 1 through 15 (of 323 total)