Hi Andy,
I know that this might not be the answer that you’re looking for, but if you want functions in your content area, I strongly recommend using shortcodes instead of leaving the markup open and then closing it in the template. Not only does your site become prone to markup mistakes, but also will reduce scalability and compatibility with other plugins (e.g. export / import)
Over all it is just really bad practice, and I urge you again to use shortcodes instead.
Thanks. I would switch to shortcodes but I can’t understand the instructions for making them. How would I make a shortcode from this:
<?php global $post; // required
$args = array('numberposts'=>1, 'category'=>75,39,46,23, 'order'=>'ASC');
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);?>
<h6 class="home-feature"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h6>
<?php
the_post_thumbnail();
the_excerpt( sprintf(__( 'Read More<span class="screen-reader-text"> "%s"</span>', 'twentyseventeen' ),
get_the_title()) ); ?>
<a href="<?php echo get_permalink(); ?>" class="purplez"> Read More</a>
<?php endforeach;
?>
-
This reply was modified 9 years, 3 months ago by
andy3000.
Something along the lines of the following:
// This tells WP to register the [my_shortcode] and
// run igy64326_function wherever it is found.
add_shortcode( 'my_shortcode', 'igy64326_function' );
// This is the actual function
function igy64326_function($atts){
// start outut buffering
ob_start();
// if you need to pass attributes like [my_shortcode x=10 y=5]
// remove this, if not needed.
print_r($atts);
global $post; // required
$args = array('numberposts'=>1, 'category'=>75,39,46,23, 'order'=>'ASC');
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);?>
<h6 class="home-feature"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h6>
<?php
the_post_thumbnail();
the_excerpt( sprintf(__( 'Read More<span class="screen-reader-text"> "%s"</span>', 'twentyseventeen' ),
get_the_title()) );
?>
<a href="<?php echo get_permalink(); ?>" class="purplez">
<?php
// Keep hardcoded texts translatable
echo __('Read More', 'twentyseventeen');
?>
</a>
<?php
endforeach;
// return output buffer, and clean it.
return ob_get_clean();
}
I didn’t test it, but it should give you an idea.
You’d need to change the ‘my_shortcode’ to whatever you’d like. Also, you could change the function name to something else, just make sure it’s unique.
Also, check the inline comments.
Thanks a bunch. I’ll platy around with that.
Just moved
add_shortcode( 'my_shortcode', 'igy64326_function' ); below the function and it worked right out of the box!
oh, yes.. sorry.. I copied it from a class. Anyways, glad it worked.