WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. redsundesign
    Member
    Posted 11 months 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.

    http://wordpress.org/extend/plugins/multiple-content-blocks/

  2. Trendwerk
    Member
    Plugin Author

    Posted 10 months 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
    Member
    Posted 10 months ago #

    Many, many thanks - that works perfectly.

Reply

You must log in to post.

About this Plugin

About this Topic