Support

Account

Forum Replies Created

  • because acf does not passes the correct post id of each post in the query. instead it returns the post id of the query loop post. every post in that loop get the same id. therefore it won’t work as get_field always tries to return the values of the loop containing post.

  • what do you think what i am? A machine? I can not read your comment / code. So i can not help you. But placing custom content before/after something is just like changing the order in the code. code for outputting the acf field after the title:

    add_filter( 'render_block_core/post-title', function( $block_content, $block ) {
    	if(!empty($block["attrs"]["className"]) && $block["attrs"]["className"] != "company") return $block_content;
    		
    	return sprintf("%s<div style='margin-top:%s'><strong>%s</strong></div>",$block_content,"10px",get_field("company"));
    }, 10, 2 );
  • Hi Guys – just want to share my workaround idea here…
    assuming you want to place a custom field value before (or after) the post-title block in the loop template, you can give the block a unique classname like in my case “company” and then look for that classname in the following code you need to put in your functions.php

    add_filter( 'render_block_core/post-title', function( $block_content, $block ) {
    	if(!empty($block["attrs"]["className"]) && $block["attrs"]["className"] != "company") return $block_content;
    		
    	return sprintf("<div style='margin-bottom:%s'><strong>%s</strong></div>%s","10px",get_field("company"),$block_content);
    }, 10, 2 );

    thats a pretty robust solution because it uses core code. In my case i need to output the custom field “company” before the post title in of one post in the loop. This will work for the frontend only!

  • @alessietto simplified you need to change your markup.
    this is my first block type “tabgroup”:

    <div class="tabgroup" id="<?php echo $block_id; ?>">
        <InnerBlocks allowedBlocks="<?php echo esc_attr( wp_json_encode( array( 'tabitem' ) ) );?>" template="<?php echo esc_attr( wp_json_encode( array(array('tabitem')) ) );?>" />
    </div>

    this is my second block type “tabitem” which is a child of tab_group:

    <div class="tabitem">
        <InnerBlocks />
    </div>

    tabitem needs the json-key:
    "parent" : ["tabgroup"]

    or if you work oldschool with php array:
    "parent" => array("tabgroup")

    in the end you need to order the items and play with visibility via css like:
    .tabwrapper { display:flex }
    .tabitem { order: 0 }
    .tabgroup { order: 1 }

    check this article to understand the logic

  • sorry for late reply. yes. according to acf6 you now should use register_block_type (instead of acf_register_block_type) and load the block.json file. Example:

    {
      "name"              : "lmdm/tabitem",
      "title"             : "Tab-Inhalt",
      "description"       : "Tab-Inhalt",
      "category"          : "widgets",
      "icon"              : "<svg width=\"80px\" height=\"80px\" viewBox=\"0 0 80 80\" xmlns=\"http://www.w3.org/2000/svg\">\n    <g id=\"icon-tab-item\" stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\">\n        <path d=\"M31.0453442,11.6287145 L31.0453442,27 L75,27 L75,75 L5,75 L5,27 L10.2507421,27 L10.2507421,11.6287145 L31.0453442,11.6287145 Z\" id=\"Rectangle\" stroke=\"#000000\" stroke-width=\"4\"></path>\n        <rect id=\"Rectangle\" fill=\"#000000\" x=\"13\" y=\"35\" width=\"54\" height=\"32\"></rect>\n    </g>\n</svg>",
      "keywords"          : ["tabs","navi","accordion","accordeon"],
      "parent"            : ["lmdm/tabgroup"],
      "supports"          : {
        "align"     : false,
        "align_text" : true,
        "multiple" : true,
        "color" : {
          "background": false
        },
        "jsx" : true
      },
      "acf": {
        "mode": "preview",
        "renderTemplate": "index.php"
      }
    }
  • yes thats my current status quo. you can load ONE js-file with the handle passed through “script” key. But i need to load multiple js-files. so this won’t work at the moment:

    {
        "script": ["block-qb-daterange","another-handle"],
    }
  • i found out i can get rid of the post status if i simply add
    ‘post_status’ => ‘publish’,
    to the query. but to be honest i am not sure if it influences the performance. what else can be done here?

  • thank you guys, was looking for this!

  • sure. you can also add

    'color' => array(
      'link' => true
    )
  • okay i think i got it now. parent attribute needs to be placed in acf_register_block_type:

    		acf_register_block_type(array(
    			'name'              => 'tab_item',
    			'title'             => __('Tab-Inhalt','hns'),
    			'description'       => __('Tab-Inhalt','hns'),
    			'render_template'   => 'template-parts/blocks/tabs/tab-item.php',
    			'category'          => 'widgets',
    			'keywords'          => array( 'tabs','navi'),
    			'mode'              => 'preview',
                'parent'            => array('tabs_group'),
    			'supports'          => array(
    				'align'     => false,
    				'align_text' => true,
    				'multiple' => true,
    				'jsx' => true, // innerblocks
    				//'align_content' => true, // oben, mittig, unten
    			),
    		));
  • was looking for the exact same case. building my own tabbed content block.
    the approach of @dsouzaj86 works pretty well. I tried it. thank you very much! Only thing i don’t get is the parent property to allow appearing the tab-content only in tab-group. at the moment i can place it everywhere.
    tab-group:
    <InnerBlocks allowedBlocks="<?php echo esc_attr( wp_json_encode( array( 'acf/tab-item' ) ) );?>" />

    tab-item:
    <InnerBlocks parent="<?php echo esc_attr( wp_json_encode( array( 'acf/tab-group' ) ) );?>" />

    seems parent gets ignored.

  • if you want to NOT have the cpt translatable by polylang you need to skip it in parse_query hook like this:

    function skip_cpts( $query ) {
    	if ( get_post_type() == "event" )  $query->set( 'lang', '' );
    	
    }
    add_action( 'parse_query', "skip_cpts", 0 );

    thanks to polylang support if found this solution helpful

  • WOW finally i found the issue. It occurs when you use Polylang and you did not put the CPT in CPT-settings of the plugin:
    image
    i simply forget it and this was driving me crazy for hours. JC! hopefully one day i can help someone with that monologue or ACF team will implement a fix

  • one thing i tested: as it used to work with older/other CPTs, i duplicated one post of other CPT and then changed the posttype in the database to “event” and voila, it gets accepted and saved by the relationship field: https://www.loom.com/share/ae811648893d4251bb2e8ef5c9851ad7
    but i don’t see/understand the difference of the posts

    • WP_DEBUG is enabled and debug.log file empty
    • tested with 5.9.0, 5.10.1 and 5.11-RC1
  • i am late but you can also try something like this (with type):

    function disable_acf_load_field( $field ) {
    	$field['disabled'] = true;
    	return $field;
    }
    add_filter('acf/load_field/type=text', 'disable_acf_load_field');

    or try to inspect $field array name with matches of your fields like:

    function disable_acf_load_field( $field ) {
    	$fields = "project_code","project_type","project_status","funding_year" // add more here
    	if(in_array($field["name"],$fields)) $field['disabled'] = true;
    	return $field;
    }
    add_filter('acf/load_field/type=text', 'disable_acf_load_field');
  • i was not aware of “FrmForm::get_published_forms()”. Your code is definitely better 😉

  • as far as i can see this plugin won’t work in the future too

  • i missed that statement from yoast. but i click thru their discussion and ended with this javascript documentation: https://github.com/Yoast/YoastSEO.js/blob/master/README.md

    but at the moment i have no clue how to use this code with our old hook

  • two years later and nothing happened here 🙁
    +1 for external page link

  • hey paul. i took a look into the code of yoast. seems it expects < img > strings in $content. so all you need to do is something like this:

    add_filter( 'wpseo_pre_analysis_post_content', 'filter_yoasts_wpse_119879', 10, 2 );
    function filter_yoasts_wpse_119879( $content, $post ){
    	$myimage = get_field("myimage",$post->ID);
    	$content .= "<img src='".$myimage["sizes"]["medium"]."' alt='".$myimage["alt"]."'/>";
    	return $content;
    }

    and it would recognize your custom field images.

  • okay. that was easy! here we go. simply adjust it to your needs:
    1. create a new entry in wp_options: “is_winner”
    2. insert that code to your functions.php:

    // register the meta box
    add_action( 'add_meta_boxes', 'my_custom_field_checkboxes' );
    function my_custom_field_checkboxes() {
        add_meta_box(
            'winner_box',          // this is HTML id of the box on edit screen
            'Winner',    // title of the box
            'my_customfield_box_content',   // function to be called to display the checkboxes, see the function below
            'product',        // on which edit screen the box should appear
            'side',      // part of page where the box should appear
            'default'      // priority of the box
        );
    }
    
    // display the metabox
    function my_customfield_box_content( $post_id ) {
        wp_nonce_field( 'wp_nonce_check', 'myplugin_nonce' );
        echo '<label><input type="checkbox" name="is_winner" value="1" /> Winner';
        if(get_the_id() == get_option("is_winner")) echo " (currently selected)";
        echo "</label>";
    }
    
    // save data from checkboxes
    add_action( 'save_post', 'my_custom_field_data' );
    function my_custom_field_data() {
        // check if this isn't an auto save
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return;
    
        // security check
        if ( ! isset( $_POST['myplugin_nonce'] ) || !wp_verify_nonce( $_POST['myplugin_nonce'], 'wp_nonce_check' ) )
            return;
    
        if ( isset( $_POST['is_winner'] ) )
        	update_option("is_winner", get_the_id());
        else
            return;
    }

    now in every post is a meta box along with ACF fields. when you check that meta box, the id is saved to the field in wp_options. this way, you save a lot of performance because there is no wp_query and you do not need to alter all posts!!

  • normally i do this with an option page. but i would prefer to set that post meta directly in the post too. i would do it like you but this makes me worry because of a lot of code for such a tiny thing. hmm. another approach might be to create an option field in the database which saves the post id. and toggle that id inside of each post.

Viewing 25 posts - 1 through 25 (of 50 total)