Multiple content blocks
[resolved] Fallback to top ancestor (3 posts)

  1. redsundesign
    Posted 2 years ago #

    I liked candell's post regarding displaying default content if the MCB field is empty. I've adapted this slightly and created a function to cut down on the amount of code in my page templates.

    function getMCBheadline()
    	$headlineblock = get_the_block('headline',array('type' => 'one-liner','apply_filters' => false));
    	$emptyheadline = "This is the default fall-back headline";
    	if  ($headlineblock != "") {
    		echo $headlineblock;
    	} else {
    		echo $emptyheadline;

    I'm creating a site where each main page has an addtional headline content block with a mission statement. Is there any way to get the content of the main page content block to show on child (or grandchildren) pages if their content block is empty rather than static default text using 'get_top_ancestor' or similar? Any help much appreciated.


  2. Trendwerk
    Plugin Author

    Posted 2 years ago #

    Sure there is :-) It will probably be something like this, but I don't know your exact circumstances so you'll probably have to edit the code a little.

    function the_headline() {
      global $post;
      $fallback = "This is the default fallback when the current child AND the parent both dont have a headline";
      $headline = get_the_block('headline',array('type' => 'one-liner', 'apply_filters' => false));
      if(strlen($headline) == 0) $headline = get_post_meta($post->post_parent,'mcb-headline',true);
      if(strlen($headline) == 0) $headline = $fallback;
      echo $headline;
  3. redsundesign
    Posted 2 years ago #

    Many, many thanks - that works perfectly.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Multiple content blocks
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic