Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1052051

    Hello All,

    I have made a custom post type called “blocks”
    i get all the blocks if the have a selected a “action”.

    But when i call it my global defined or class defined variable stay’s the same.

    Here is my class with the shortcode and action functions


    <?php

    class TmeBlockShortcode
    {

    function __construct()
    {
    add_action( 'init', array($this, 'DetectBlockShortcode') );
    add_shortcode( 'block', array($this,'GetSingleBlock') );

    //add_action( 'wp_head', array($this,'SingleBlockCSS') );
    $this->post_content = '';
    }

    function GetSingleBlock($atts)
    {
    $data = shortcode_atts(
    array(
    'id' => ''
    ), $atts);

    $posts = $this->GetPosts(1,$data, true);

    if(count($posts) == 0)
    {
    return 'Block id (unieke naam) niet gevonden.';
    }

    if(get_field('active',$posts[0]->ID) == 1)
    {
    return do_shortcode( $posts[0]->post_content );
    }

    return '';
    }

    public function SingleBlockCSS()
    {
    $css = '<style>';

    $css .= '</style>';
    return $css;
    }

    public function DetectBlockShortcode()
    {
    $posts = $this->GetPosts('-1');
    foreach ($posts as $post)
    {
    global $test;
    $test = $post->ID;
    $this->post_content = '321';
    $this->post_content = $post->ID;

    add_action( get_field('on_action',$post->ID), array($this, 'DetectBlockShortcode_add_action'));

    echo '

    ';
                    print_r( $this->post_content  );
                echo '

    ';
    }
    }

    public function DetectBlockShortcode_add_action()
    {
    $nu = $this->post_content;
    global $test;
    echo '

    ';
                print_r($this->post_content);
                print_r($test);
                print_r($nu);
            echo '

    ';
    //echo do_shortcode( $this->post_content );
    $this->post_content = '';
    }

    private function GetPosts($posts_per_page = 1,$data = array(), $shortcode = false)
    {
    if(!isset($data['id']) && $shortcode == true)
    {
    return 'vegeet niet het block id (unieke naam) op te geven.';
    }

    if(isset($data['id']))
    {
    $meta = array(
    'relation' => 'AND',
    array(
    'key' => 'unique_name',
    'value' => $data['id'],
    'compare' => '!='
    ),
    array(
    'key' => 'active',
    'value' => 1,
    'compare' => '='
    )
    );
    }
    else
    {
    $meta = array(
    'relation' => 'AND',
    array(
    'key' => 'on_action',
    'value' => 'no_action',
    'compare' => '!='
    ),
    array(
    'key' => 'active',
    'value' => 1,
    'compare' => '='
    )
    );
    }

    $args = array(
    'post_type' => 'blocks',
    'posts_per_page' => $posts_per_page,
    'meta_query' => $meta
    );

    $posts = get_posts($args);

    return $posts;
    }

    }
    new TmeBlockShortcode;

    The variable $this->post_content or $test within the DetectBlockShortcode_add_action function will always be the last id in my foreach loop.
    the print_r( $this->post_content ); within function DetectBlockShortcode is showing the correct ids

    Can someone tell me where i am doing it wrong?

    • This topic was modified 5 years, 10 months ago by Tieme.
    #1052059

    p.s. how to get all the code within a code block ?

    #1053096

    Hi Tieme,

    Here are some threads to consider:

    https://kriesi.at/support/topic/procedure-for-adding-custom-post-type-and-enabling-it-for-use/

    If you need further assistance please let us know.
    Best regards,
    Victoria

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.