Search Results for 'ob_start'

Home Forums Search Search Results for 'ob_start'

Viewing 15 results - 1 through 15 (of 248 total)
  • Author
    Search Results
  • #1868034
    [Support request]

    Topic: Search filter

    in forum Support
    Joe

    Hello,

    I have installed the following search filter on an overview page for hotel operations (https://rockt.joewp.com/betriebe/). The code is as follows:

    add_shortcode (‘search’, ‘tu_search_shortcode’);
    function tu_search_shortcode () {
    ob_start ();
    get_search_form ();
    $ ret = ob_get_contents ();
    ob_end_clean ();
    return $ ret;
    }

    this will filter individual pages.

    I want to limit the filtering to results from the slug /veranstalter/. Is that possible and can the above code be adapted accordingly?

    The search results e.g. for the search for “Hotel Schweizerhof” should look like this: https://rockt.joewp.com/veranstalter/hotel-schweizerhof-bern/ you can see this when you click on the first picture link.

    I am thankful for any help.

    Best regards
    Joe / JoeWP.com

    Elvin
    Staff
    Customer Support

    Ah you mean the category dynamically changes as well to whatever category the latest post is under?

    If so, you may have to get a plugin that does that.

    Or, here’s one that’s a bit tricky.

    Create a shortcode that does a query for the latest post but instead of getting the rest of the post’s details like title, date, etc, just fetch the category.

    Example:

    add_shortcode( 'latest_post_cat', function($atts) {
        $atts = shortcode_atts(
            array(
                'offset' => '0',
                'posts_per_page' => '1',
        ), $atts, 'latest_post_cat' );
    	
        $args = array(
            'posts_per_page' => $atts['posts_per_page'], // we need only the latest post, so get that post only
            'offset' => $atts['offset'],
        );
    	
        $q = new WP_Query( $args);
    
    	ob_start();
    	
        if ( $q->have_posts() ) {
            while ( $q->have_posts() ) {
            $q->the_post(); 
                echo '<a href="' . esc_url( get_category_link( get_the_category()[0]->term_id ) ) . '"><h1 class="latest-post-cat-name">'.get_the_category()[0]->name.'</h1></a>';
            }
            wp_reset_postdata();
        }
        return ob_get_clean();
    } );

    Here’s a PHP snippet for a shortcode that displays the category link and category name of the latest post.

    Example usage/s: [latest_post_cat offset="1" posts_per_page="5"] or[latest_post_cat offset="2"]

    Where offset is the offset value from the latest page. post_per_page is optional. It sets the number of posts(category name of the post) to display.

    A wise man once said:
    "Have you cleared your cache?"

    Elvin
    Staff
    Customer Support

    You’ll need a shortcode to get the current page number.

    Here’s a PHP snippet for it.

    add_shortcode( 'current_page', function() {
        ob_start();
        // Start your PHP below
    	
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	echo $paged;
      
        // End your PHP above
        return ob_get_clean();
    } );

    This lets you use a simple [current_page] that displays the current page number.

    As for usage:

    Say, for example you want “Title – page # n” where n is the page number.

    You can add something like this on the Header Element code area.

    <h1 style="color: white;">
    	{{post_title}} - page # [current_page]
    </h1>

    A wise man once said:
    "Have you cleared your cache?"

    Elvin
    Staff
    Customer Support

    Hi Scott,

    David’s recommended plugin was for post listing on the sidebar.

    If you need to display related posts in a column, you’ll need a post list plugin for that.

    You can try WP Show Posts – https://wpshowposts.com/ – to create the post list in columns.

    You then add this PHP snippet.

    add_shortcode('dynamic_wpsp', function($atts){
        $queriedArchive = get_queried_object();
        $atts = shortcode_atts( array(
    			'id' => ''
    		), $atts, 'dynamic_wpsp' );
    
        $settings = array(
            'taxonomy' => $queriedArchive->taxonomy,
            'tax_term' => $queriedArchive->slug,
        );
     
        ob_start();
        wpsp_display( $atts['id'], $settings );
        return ob_get_clean();
    });

    And then hook in the shortcode [dynamic_wpsp id=”1234″] where 1234 is the WPSP list id.

    You can use a Hook Element to hook the shortcode in. Set the Hook Element’s hook to generate_after_main_content with display rule location set to “Post – all posts” so it only displays on the bottom single posts pages. 😀

    A wise man once said:
    "Have you cleared your cache?"

    Miguel

    Hi GP Team,

    I inserted the native GeneratePress “search form” inside the “Elements -> Block Element -> Element Type -> Right Sidebar”. I’m using the Search Form “Shortcode” after adding this code with the help of  the “Pluginception” plugin:

    add_shortcode( ‘search_form’, function() {
        ob_start();
        get_search_form();
        return ob_get_clean();
    } );

    Here the URL of my Testing Website so that you can see the Issue –> https://playwp.xyz/

    Thanks for your kind help 🙂

    #1847394
    David
    Staff
    Customer Support

    Hi there,

    currently you need a shortcode for that.
    Add this PHP Snippet to your site:

    add_shortcode( 'current_year', 'lh_current_year' );
    function lh_current_year() {
        ob_start();
        echo date('Y');
        return ob_get_clean();
    }

    And then you can use [current_year]

    #1837836
    Marvin

    Hello again,

    this solution works:

    function heroesSlider() {
    	# get posts
    	$args = array(
    		'numberposts' => -1,
    		'post_type' => 'heroes',
    		'post_status' => 'publish',
    		'orderby' => 'rand'
    	);
    	$heroes = get_posts($args);
    
    	ob_start();
    
    	# loop the slides
    	if(!empty($heroes)):?>
    		<div class="glide">
    			<div class="glide__track" data-glide-el="track">
    				<ul class="glide__slides" style="margin: 0">
    				<?php foreach ($heroes as $post): ?>
    					<?php if(has_blocks($post)): ?>
    						<li class="glide__slide">
    							<?php
    							foreach($blocks = parse_blocks($post->post_content) as $block):
    								$block = render_block($block);
    								echo $block;
    							endforeach;
    							?>
    						</li>
    					<?php endif ?>
    				<?php endforeach ?>
    				</ul>
    			</div>
    		</div>
    		<script>
    			new Glide('.glide').mount()
    		</script>
    	<?php
    	endif;
    	return ob_get_clean();
    }
    add_shortcode('heroes', 'heroesSlider');
    
    /* add css classes  */
    function doHeroesCSS($content) {
    	global $post;
    	# check if shortcode is used
    	if(is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'heroes')):
    		$args = array(
    			'numberposts' => -1,
    			'post_type' => 'heroes',
    			'post_status' => 'publish',
    		);
    		$heroes = get_posts($args);
    
    		if(!empty($heroes)):
    			foreach ($heroes as $p):
    				if(has_blocks($p)): 
    					$content .= $p->post_content;
    				endif;
    			endforeach;
    		endif;
    	endif;
    	return $content;
    };
    add_filter('generateblocks_do_content', 'doHeroesCSS', 10, 2);

    But if there is more efficient solution instead of looping the content two times I am interested 🙂

    #1836664
    Marvin

    Hey guys!

    Every project I love the GeneratePress Theme more and more. Thank you really much!

    I have a custom post type heroes for a slider, which is embedded via the shortcode [heroes]:

    function heroesSlider() {
    	ob_start();
    
    	# get posts
    	$args = array(
    		'numberposts' => -1,
    		'post_type' => 'heroes',
    		'post_status' => 'publish',
    	);
    	$heroes = get_posts($args);
    
    	# loop the slides
    	if(!empty($heroes)):?>
    		<div class="glide">
    			<div class="glide__track" data-glide-el="track">
    				<ul class="glide__slides">
    				<?php foreach ($heroes as $p): ?>
    					<?php if(has_blocks($p)): ?>
    						<li class="glide__slide"><?= $p->post_content ?></li>
    					<?php endif ?>
    				<?php endforeach ?>
    				</ul>
    			</div>
    		</div>
    
    		<script>
    			new Glide('.glide').mount()
    		</script>
    	<?php
    	endif;
    
    	return ob_get_clean();
    }
    
    add_shortcode('heroes', 'heroesSlider');

    But I don’t know how to apply the generateblocks_do_content on <?= $p->post_content ?> inside the loop. As expected the output looks like this, unfortunately:

    <li class="glide__slide"><!-- wp:generateblocks/container {"uniqueId":"69d8e582","isDynamic":true} -->
    <!-- wp:generateblocks/grid {"uniqueId":"088fad56","columns":2,"isDynamic":true} -->
    <!-- wp:generateblocks/container {"uniqueId":"336f2c74","isGrid":true,"gridId":"088fad56","paddingTop":"0","paddingRight":"0","paddingBottom":"0","paddingLeft":"0","isDynamic":true} -->
    <!-- wp:paragraph -->
    ...
    </li>

    How to I apply the generateblocks_do_content in a simple way on <?= $p->post_content ?>?

    Thank you really much!
    Marvin

    #1835993

    In reply to: Some advice please

    David
    Staff
    Customer Support

    Awesome 🙂
    So you can unhook the Comments Template with this snippet:

    add_action('wp', function(){
    	remove_action( 'generate_after_do_template_part', 'generate_do_comments_template', 15 );
    },100);

    Note: That removes from ALL posts / pages.

    Then one of our users came up with the Shortcode snippet for displaying just the Comment Form, which should work in your modal…. but that modal would have to be on the Post that you’re writing your comments to:

    function wpsites_comment_form_shortcode() {
        ob_start();
        comment_form();
        $cform = ob_get_contents();
        ob_end_clean();
        return $cform;
    }
    add_shortcode( 'wpsites_comment_form', 'wpsites_comment_form_shortcode' );

    As an aside – if still required – see this doc for changing the Comment Title ( Leave a comment ):

    https://docs.generatepress.com/article/generate_leave_comment/

    Nome

    I have a shortcode for displaying a custom post type (‘event’) within different parts of my site. I also have a Content Template Block Element for handling the formatting of the event CPT. The formatting works great on the single and archive pages, as you’d expect, but I can’t figure out how to get it to apply to the CPT when I access it through my own WP_Query loop.

    Here is a simplified version of what that code looks like now:

    
    function popular_events_shortcode( $atts ) {
    // ...
      if ( $events->have_posts() ) {
        ob_start();
        while ( $events->have_posts() ) {
          $events->the_post();
          generate_do_template_part( 'single' );
        }
        return ob_get_clean();
      }
    }
    
    #1818352
    [Support request]

    Topic: One Shortcode Doesn’t work

    in forum Support
    Samuel

    Hi Team,

    I have created a shortcode, which is not working. But other custom shortcodes working perfectly.

    I just showing you my shoercode codes:

    function dl_minti_button_shortcode($atts, $content = '') {
    	extract( shortcode_atts(
            array(
                'link' => '#',
                'target' => '_blank',
                'lightbox' => '',
                'color' => '',
                'icon' => ''
            ), $atts )
        );
    
      	ob_start();
      	?>
    
      	<div class="minti_butto_wrap">
      		<a href="<?php echo $link; ?>" target="<?php echo $target; ?>"><?php echo $content; ?></a>
      	</div>
    
      	<?php
      	return ob_get_clean();
    }
    add_shortcode( 'minti_button', 'dl_minti_button_shortcode' );

    I have applied this shortcode in this page https://justcreative.com/shortcode-test-page/

    But showing me plain shortcode text. See screenshot http://i.prntscr.com/8Dm_8e8bRRixNvkW2IqMgQ.png

    So, please me how can I solve this problem.

    #1813366
    David
    Staff
    Customer Support

    Hi there,

    thats correct you can only register a function once.
    If you want to add more inserts then only duplicate this part of the code:

    add_filter( 'the_content', 'insert_content_after_p', 20 );
    function insert_content_after_p( $content ) {
        ob_start();
        ?>
        <div>HTML goes here...</div>
        <?php
        $after_p_content = ob_get_clean();
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $after_p_content, 2, $content );
        }
        return $content;
    }

    In this code you will see 2 instances of:

    insert_content_after_p

    In your duplicate function you will need to change them both to something else eg. insert_content_after_p_number_two

    #1810427
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    This might be helpful: https://medium.com/@stefanledin/wordpress-how-to-get-only-direct-child-categories-286ad724f04b

    add_shortcode( 'list_subcats', function() {
        ob_start();
        
        $current_cat = get_queried_object();
        $term_id = $current_cat->term_id;
    
        $categories = get_categories( array( 
            'parent' => $term_id,
            'hide_empty' => true,
        ) );
    
        echo '<ul class="list-subcats">';
    
        foreach ( $categories as $category ) {
    	echo '<li><a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . $category->name . '</a></li>';
        }
    
        echo '</ul>';
    
        return ob_get_clean();
    } );
    #1793567
    [Support request]

    Topic: Add pagination to shortcode

    in forum Support
    Alberto

    Hello
    I’m trying to add a standard pagination to a shortcode of mine.
    In that shortcode I’m using WP_Query to list the post in certain CPT, and I want to add the pagination after that list.
    I’ve tried different suggestions of yours, like this one or this other, but I’ve not been able to display the pagination, so I understand that I’m doing something wrong.

    This is the URL: https://circuloesceptico.org/novo/noticias (where noticias is a page)
    And this is the current shortcode:

    function dom_listado_noticias( $atts, $content = null ){
        extract(shortcode_atts(array(
          'items' => 8,
          'tipo'  => 'noticias'
        ), $atts));
    
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        if( $tipo == 'noticias'):
            $consulta = new WP_Query( array(
                    'post_type'     => 'post',
                    'posts_per_page'    => $items,
                    'tax_query' => array(
                                array (
                                    'taxonomy' => 'category',
                                    'field' => 'slug',
                                    'terms' => 'noticias',
                                )
                            ),
                    'paged'         => $paged,
                    'offset'        => 1,
                ));
        elseif( $tipo == 'evento' ):
            $consulta = new WP_Query( array(
                    'post_type'     => 'evento',
                    'posts_per_page'    => $items,
                    'paged'         => $paged,
                    'offset'        => 1,
                ));
        endif;
    
        ob_start();
        ?>
    
        <?php if( $consulta->have_posts() ): ?>
            <div class="listado-artig row">  
        
            <?php while ( $consulta->have_posts() ) : $consulta->the_post();
                $id = get_the_ID();
                $imx = get_the_post_thumbnail_url($id,'large');
                $tit_aux = get_the_title();
                $tit = wp_trim_words( strip_tags($tit_aux), 11);
                if( has_excerpt($id) ){
                    $extracto = get_the_excerpt( $id );
                } else {
                    $extracto = wp_trim_words( strip_tags(get_the_content()), 20 );
                } ?>
                
                <div class="listado-artig-item">
                    <div class="listado-artig-item__imx"><img src="<?php echo $imx; ?>" /></div>
                    <div class="listado-artig-item-txt">
                        <a href="<?php the_permalink(); ?>"><div class="listado-artig-item-txt__tit"><?php echo $tit; ?></div></a>
                        <div class="listado-artig-item-txt__txt"><?php echo $extracto; ?></div>
                        <a class="button btn-cies" href="<?php the_permalink(); ?>"><?php _e('Leer más', 'circuloesceptico'); ?></a>
                    </div>
                </div>
                
            <?php endwhile; ?>
            </div>
    
        <?php else: ?>
            <div class=""><?php _e('No hay información de ese tipo', 'circuloesceptico'); ?></div>
        <?php endif;?>
    
        <div class="paxinador">
            <?php the_posts_pagination( array(
                'mid_size'  => 2,
                'prev_text' => __( 'Prev', 'circuloesceptico' ),
                'next_text' => __( 'Next', 'circuloesceptico' ),
            ) ); ?>
        </div>
        <!--div class="paxinador">
            <a href="https://circuloesceptico.org/novo/category/noticias/"><?php _e('Ver todas las ', 'circuloesceptico'); ?><span class="violeta"><?php _e('noticias y artículos', 'circuloesceptico'); ?> <i class="fas fa-arrow-right"></i></span></a>
        </div-->
    
        <?php
        wp_reset_postdata();
        return ob_get_clean();
    }
    add_shortcode('noticias', 'dom_listado_noticias');

    I’ll appreciate your help

    TIA

    #1783359
    David
    Staff
    Customer Support

    Hi there,

    try adding this PHP Snippet to generate a Shortcode to output the image:

    add_shortcode('tax_image', function(){
        ob_start();
        if (function_exists('z_taxonomy_image')) { 
            z_taxonomy_image(); 
        } 
        return ob_get_clean();
    })

    Then you can use [tax_image] in your header element

Viewing 15 results - 1 through 15 (of 248 total)