Skip to content

Instantly share code, notes, and snippets.

@anointed
Created July 1, 2012 20:10
Show Gist options
  • Select an option

  • Save anointed/3029434 to your computer and use it in GitHub Desktop.

Select an option

Save anointed/3029434 to your computer and use it in GitHub Desktop.
get current page layout
function tumble_get_current_page_layout() {
// Use default layout for 404 pages
if ( is_404() ) {
return 'default';
}
// Otherwise, determine appropriate layout
$layout = '';
global $post;
$options = get_option('tumble_');
$meta = get_post_meta( get_the_ID(), '_tumble_default_layout', true );
$custom = ( get_post_custom( $post->ID ) ? get_post_custom( $post->ID ) : false );
$custom_layout = ( isset( $custom['_tumble_default_layout'][0] ) ? $custom['_tumble_default_layout'][0] : 'default' );
if ( ! is_admin() ) {
if ( is_attachment() ) {
$layout .= 'attachment';
}
else if ( is_home() ) {
$layout .= $options['default_home_page_layout'];
}
else if ( is_page() ) {
if ( 'default' == $custom_layout ) {
$layout = $options['default_static_page_layout'];
} else {
$layout .= $meta;
}
}
else if ( is_single() ) {
if ( 'gallery' == get_post_format() || 'image' == get_post_format() || 'video' == get_post_format() ) {
$layout .= 'full';
}
else if ( 'default' == $custom_layout ) {
$layout = $options['default_single_post_layout'];
}
else {
$layout .= $meta;
}
}
else if ( is_archive() || is_search() || is_404() ) {
$layout .= $options['post_index_layout'];
}
}
return $layout;
}
this function is causing the Notice error:
/**
* modify the excerpt with a read more section
* @param [type] $output [description]
* @return [type] [description]
*/
//function to replace invalid ellipsis with text linking to the post
function tumble_clean_excerpt($text)
{
return str_replace('[...]', '<a href="'. get_permalink($post->ID) . '">' . '[Read More &hellip;]' . '</a>', $text);
}
add_filter('the_excerpt', 'tumble_clean_excerpt');
@anointed

anointed commented Jul 1, 2012

Copy link
Copy Markdown
Author

The above function is a slightly modded version of the oneology get current page layout. I changed the function to use the settings api, and it does work properly on the website.

The Problem:
With debugging turned on, I get the following notices:
NOTICE: C:\websites\buddypress.dev\wp-content\themes\June-new-theme-stipe\framework\functions\template-functions.php:21 - Undefined variable: post
NOTICE: C:\websites\buddypress.dev\wp-content\themes\June-new-theme-stipe\framework\functions\template-functions.php:21 - Trying to get property of non-object

This file is in my custom-functions.php file, which is loaded via the functions.php file in the theme.
I then call the function on my front-end template, and indeed it does work properly.

How should I go about removing this notice?

@chipbennett

Copy link
Copy Markdown

What is line 21 of template-functions.php? Are you calling this function within the Loop? If not, in what template context are you calling the function?

@anointed

anointed commented Jul 2, 2012

Copy link
Copy Markdown
Author

Hi Chip, thanks for taking the time to answer me back.
Line 21 is the function above where it defines the global $post.

What I am doing is using the <php echo tumble_get_current_page_layout() > in my front end templates as the div id. This shows up in the archive/index/single/etc templates.

The problem I was having was the function in my comment above where I am filtering the_excerpt(). If I add that function to my theme, then I end up with the notice errors. Removing the function removes the notice issue.

Mostly I was just trying to learn why I would have an issue using str_replace on the excerpt in combination with the layout function. Concerned about future issues I may run into if I should need to run further replace functions on the content.

If I add that function to the oenology theme (changing excerpt to content for the filter) I end up with the same notice warning.

Maybe my str_replace function is written poorly?

thanks again. I really like oenology and have indeed learned a lot from it. While I am only using a few of the oenology functions in my own theme, I am trying my best to follow the code commenting and formatting as a new standard for myself to follow and understand. I only wish more code was written that clearly.

@chipbennett

Copy link
Copy Markdown

Okay, two issues:

One, in the tumble_clean_excerpt() function, you use the $post global without first globalizing it. You need to add global $post; before you reference $post.

Two, str_replace() is the wrong method to modify the ellipsis, and ``the_excerpt is the wrong filter. You need to use the [excerpt_more`](http://codex.wordpress.org/Plugin_API/Filter_Reference/excerpt_more) filter instead, e.g. as follows:

function tumble_filter_excerpt_more( $more_text ) {
    global $post;
    return '<a href="' . get_permalink( $post->ID ) . '">' . '[Read More &hellip;]' . '</a>';
}
add_filter( 'excerpt_more', 'tumble_filter_excerpt_more' );

Hope this helps! I'm glad to hear that Oenology has been helpful. As I'm sure you've read in the description, providing an educational resource for other developers is the primary reason I released the theme. If you see any areas for improvement for Oenology, whether as an educational resource or in any other way, please let me know!

@anointed

anointed commented Jul 2, 2012

Copy link
Copy Markdown
Author

Thank you!
Always learning something new.

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