Skip to content

Instantly share code, notes, and snippets.

@rgadon107
Last active January 10, 2017 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgadon107/affe969751c19d7bfbcaa47d904bf2d7 to your computer and use it in GitHub Desktop.
Save rgadon107/affe969751c19d7bfbcaa47d904bf2d7 to your computer and use it in GitHub Desktop.
add_action('genesis_before_content', 'ystl_add_blog_title');
/**
* Add 'Yoga St. Louis Blog' title to 'genesis_before_content' hook.
*
* @since 1.0.0
*
* @param string $title HTML markup of blog title
*
* @return string Add title before blog on front page or archive post page.
*
*/
function ystl_add_blog_title($title) {
$title = '<a href="http://ystl.dev" id="ystl-blog-title" class="featured-content blog-title">Yoga St. Louis Blog</a>' . '<br />';
if ( is_front_page() || is_singular( 'post' ) ) {
echo $title;
}
else {
echo '';
}
}
'Replace above with code block below ======================================================='
add_filter('genesis_attr_content_sidebar_wrap', 'build_content_sidebar_wrap');
/**
* Add attributes to blog title using the `genesis_attr_{context}` filter
* in 'genesis/lib/functions/markup.php.'
*
* @since 1.0.0
*
* @param array $attributes An array of attributes to build the markup.
* @return array $attributes
*/
function build_content_sidebar_wrap( $attributes ) {
if ( ! is_front_page() || ! is_singular( 'post' ) ) {
return $attributes;
}
$site_url = get_site_url();
$attributes[‘open’] = ‘<a %>’;
$attributes[‘id'] = 'blog-title';
$attributes[‘class'] = ‘featured-content’;
$attributes[‘href=""’] = $site_url;
$attributes[‘title’] = ‘Yoga St. Louis Blog’;
$attributes[‘close’] = ‘</a>’;
return $attributes;
}
@rgadon107
Copy link
Author

Is the code block below line 23 a better way to go?

@timothyjensen
Copy link

Since you are filtering now, on line 32 you need to return $attributes. Also, you can remove the 'else' because you're returning early.

@rgadon107
Copy link
Author

rgadon107 commented Jan 10, 2017

  1. Removed array data type within function parameter. Parameter data type is already defined within the filter.

  2. Changed function name to better represent what it does.

  3. Filled out doc block for filter and callback.

  4. Added $attributes to return on line 32.

  5. Added $attributes['id'] element at line 38.

  6. Lines 37 - 42, changed array variable to array key => value pairs.

@hellofromtonya
Copy link

Robert,

There are some structure and functional problems in your code. Let's walk through them.

The first function

The else codeblock is not needed at all. The first line should only happen if this web page is the front page OR a single post.

function ystl_add_blog_title() {

	if ( ! is_front_page() && ! is_singular( 'post' )  )	{
		return;
	}
	?>
	
	<a href="http://ystl.dev" id="ystl-blog-title" class="featured-content blog-title">Yoga St. Louis Blog</a><br />
	<?php
}

The second function

  1. Remove the else gatekeeper. It's unnecessary. Why? Think about it. IF this is a front page OR a post, then it returns. The else happens anyway if it's not the front page OR a post. Therefore, it's redundant and not needed.

  2. The event is : genesis_attr_content-sidebar-wrap.

  3. The code will not work until you take those attributes and then apply them yourself to the appropriate markup. Running your code, you get the following markup:

<div open="<div><a %>" class="featured-content" href="=&quot;http://sandbox.dev&quot;" title="Yoga St. Louis Blog" close="</a>"><main class="content" id="genesis-content">

That's not what you want.

Rethinking it

I assume that you are trying to insert a blog title into the page. Correct? Then you want to do this instead:

add_action('genesis_after_content_sidebar_wrap', 'render_blog_page_title_after_content_sidebar_wrap' );
/**
 * Render the blog page title after the `content-sidebar-wrap` HTML element.
 *
 * @since 1.0.0
 *
 * @return void
 */
function render_blog_page_title_after_content_sidebar_wrap() {

	if ( ! is_front_page() && ! is_singular( 'post' ) ) {
		return;
	}

	// move the HTML to a view file to keep the concerns separate.
	?>

	<h2><a href="http://ystl.dev" id="ystl-blog-title" class="featured-content blog-title">Yoga St. Louis Blog</a></h2>

	<?php
}

@rgadon107
Copy link
Author

rgadon107 commented Jan 10, 2017

// Final result:

add_action('genesis_before_content', 'render_blog_page_title_after_content_sidebar_wrap' );
/**

  • Render the blog page title on the front and post pages.

  • @SInCE 1.0.0

  • @return void
    */
    function render_blog_page_title_after_content_sidebar_wrap() {

    if ( is_front_page() || is_singular( 'post' ) ) {

    include( __DIR__ . '/lib/structure/views/blog-title.php' );
    

    }
    }

//Code included from view file (commented out for demonstration purposes):

Yoga St. Louis Blog

// Note: the php echo command in the view file was necessary to render the site url to the browser. The WP callback by itself
// returns an empty string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment