• Resolved Neueruser

    (@neueruser)


    At the moment I edited the Template file and added the PHP Code there. It works fine, but if I want to change some text, I have to edit the Template File…
    How can I use a Shortcode What PHP Code in the functions.php I have to ad, that I can use?
    The two blocks of code I`m using at the moment in the Templatefile:

     <?php 		
    	$argsevent = array(	
    		'timespan'	=> 10000,
    		'date_format_1'	=> 'd. F Y',
    		'date_format_2'	=> 'd. F Y',
    
    		'html_list_v1'	=> '%DATE%: <a href="%URL%">%TITLE%</a>',
    		'html_list_v2'	=> '<a href="%URL%"><span style="color: #DDD;">%DATE% </span> %TITLE%</a>',
    		'html_list_v3'	=> '%DATE% bis %ENDDATE%: <a href="%URL%">%TITLE%</a>',
    		'html_list_v4'	=> '<a href="%URL%">%DATE% (multiday!!!)%TITLE%</a>,
    		'max_events'	=> 0,
    		);
    
    	rs_event_list($argsevent); 
    ?>

    and

       <?php query_posts('category_name=featured'); ?>
    			<?php
    				// Start the Loop.
    				while ( have_posts() ) : the_post();
    
    					// Include the page content template.
    					get_template_part( 'content', 'page' );
    
    					
    				endwhile;
    			?>
    • This topic was modified 5 years, 9 months ago by Neueruser.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Please refer to the Shortcodes section of the Plugin Handbook:
    https://developer.ww.wp.xz.cn/plugins/shortcodes/

    Of note is that shortcode handlers must not generate output. All output needs to be collected into a single variable which the handler then returns to the caller. The caller is what actually echoes out the returned value. Template parts generate output. If your handler gets template parts, you need to use PHP output buffering to capture any output. You can then return the buffer contents to the caller.

    Thread Starter Neueruser

    (@neueruser)

    @bcworkz Thanks for your help!
    Unfortunately I dont understand much of the technical details and thought I could do it somehow like this:

    function event-list_shortcode( $atts ) {
      return <<<event-list
    
    <?php 		
    	$argsevent = array(	
    		'timespan'	=> 10000,
    		'date_format_1'	=> 'd. F Y',
    		'date_format_2'	=> 'd. F Y',
    
    		'html_list_v1'	=> '%DATE%: <a href="%URL%">%TITLE%</a>',
    		'html_list_v2'	=> '<a href="%URL%"><span style="color: #DDD;">%DATE% </span> %TITLE%</a>',
    		'html_list_v3'	=> '%DATE% bis %ENDDATE%: <a href="%URL%">%TITLE%</a>',
    		'html_list_v4'	=> '<a href="%URL%">%DATE% (multiday!!!)%TITLE%</a>,
    		'max_events'	=> 0,
    		);
    
    	rs_event_list($argsevent); 
    ?>
    add_shortcode( 'event-list', 'event-list_shortcode' );
    Moderator bcworkz

    (@bcworkz)

    You cannot nest <?php ?> blocks. Everything in a heredoc (the <<< syntax) block is a string literal, any function calls are treated as strings and are not executed. Clever thinking though, I like it even though it doesn’t work 🙂

    If rs_event_list() generates output, your only option is to use output buffering.
    https://www.php.net/manual/en/ref.outcontrol.php

    Thread Starter Neueruser

    (@neueruser)

    Thanks @bcworkz ! So I see that it’s not working 😭
    Other idea:
    Can I “tell” the Template File to execute the PHP code (what it’s actually already doing) and then call a function “load the content of Gutenberg”?

    Moderator bcworkz

    (@bcworkz)

    If you edited the template itself it can pretty much do whatever you want. Some things may be much more difficult than others. What’s difficult on a template is inserting custom content in the middle of post content. That’s where shortcodes come in handy. If you want something to appear before or after content, find where the content is output on the template and add your code before or after.

    Some of the functions called on a template might fire off action or filter hooks. You can use those to output content. Some templates might even have their own actions or filters that are are fired off right on the template.

    A template would typically output content from Gutenberg already. Why would you want to tell it to do so again? Or is the Gutenberg content you want coming from somewhere else besides the current post? If so, where? The “featured” category posts?

    Thread Starter Neueruser

    (@neueruser)

    Thank You very much for your time to answer my questions.
    I found a plugin that does insert the code for me: “xyz code snippets”.

    Thanks once more!!!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘PHP Code per Shortcode’ is closed to new replies.