How create corect function with this code?
<?php $price = get_post_meta($id, 'price_input', true);
if ($price != ''){
echo $price . " Euro";
}
else {
echo "None";
}
?>
I want to put this code in function.php or another place and use <?php myfunction(); ?>
Thx for answer!
That's pretty straight forward PHP:
#in functions.php
function display_price( $post_id ){
$price = get_post_meta($post_id, 'price_input', true);
if ($price != ''){
echo $price . " Euro";
}
else {
echo "None";
}
}
in your template:
<?php display_price( $id ); ?>
Related
This code was working. I renamed an attribute. I inadvertently created 2 functions with the same name - so the system crashed. I reinstated the code taking out the duplicated function names. And now it doesn't work any more. Any ideas??
<?php
add_action( 'woocommerce_after_shop_loop_item_title', 'mc_display_author', 5 );
add_action('woocommerce_single_product_summary', 'mc_display_weight', 20);
add_action('woocommerce_single_product_summary', 'mc_display_author', 22);
add_action('woocommerce_single_product_summary', 'mc_display_format', 24);
function mc_display_author() {
global $product;
$writer = $product->get_attribute('writer');
if ($writer="" or !isset($writer)) {
$writer = "Author Unkonwn";
}
echo '<p class="bkauthor">' . $writer . '</p>';
}
//
function mc_display_weight() {
global $product;
$weight = $product->get_weight() * 1000;
echo '<p class="bkweight">' . $weight .'g</p>';
}
//
function mc_display_format() {
global $product;
$format = $product->get_attribute('format');
echo '<p class="bkformat">' . $format .'</p>';
}
?>
I've created this shortcode to display my terms (custom taxonomy) on specific post (custom post types) :
// First we create a function
function list_terms_forme_juridique_taxonomy() {
global $post;
$terms = wp_get_post_terms( $post->ID, 'forme_juridique',array('fields'
=> 'names') );
ob_start();
if( count( $terms) > 0) {
echo '<ul>';
echo '<li>' . implode( '</li><li>', $terms) . '</li>';
echo '</ul>';
}
return ob_get_clean();
}
// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy'
);
I am trying to add a link (url) on my terms in order to redirect to the term page but I didn't succeed yet.
Any help ?
Use get_term_link()function for this:
// First we create a function
function list_terms_forme_juridique_taxonomy() {
global $post;
$terms = wp_get_post_terms($post->ID, 'forme_juridique');
ob_start();
if( count( $terms) > 0) {
echo '<ul>';
foreach($terms as $term){
$term_link = get_term_link($term, 'forme_juridique');
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
return ob_get_clean();
}
// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy'
);
I have some code which will feed through the ten most recent Wordpress blog posts through to my own non-Wordpress site. Is there any way to display the featured image assigned to a blog post? I have successfully echoed the blog title, date and body.
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
} else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
} else {
$output = $text;
}
return $output;
}
$rss = new DOMDocument();
$rss->load('http://myblog.wordpress.com/rss/'); // <-- Change feed to your site
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 10; // <-- Change the number of posts shown
for ($x=0; $x<$limit; $x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substrwords($description, 400);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div style="margin-bottom:25px;">';
echo '<h3><strong><a style="color: #139035;" href="'.$link.'" title="'.$title.'" target="_blank">'.$title.'</a></strong></h3>';
echo '<p><small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
echo '<span> <strong><a target="_blank" style="color: #139035;" href="'.$link.'" title="'.$title.'">Read blog ></span></strong>';
echo '</div>';
}
?>
You can use following code to add featured image into your RSS
function img_in_rss($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = get_the_post_thumbnail( $post->ID, 'medium') . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'img_in_rss');
add_filter('the_content_feed', 'img_in_rss');
I have a custom theme for an online shop (http://themeforest.net/item/mayashop-a-flexible-responsive-ecommerce-theme/2189918) that comes with multiple content types (besides the default post type) and I need to add some kind of vote system to one of this non-default content types ( specifically to the product content type so users can vote the products they like of my shop).
Is there any plugin that provides this funcionality?
Thanks
There are a lot of plugins that do this , you can just google them up,
But if you want to know how it can be easily (and primitivly) done with the help of custom fields , then here you go :
add_action( 'wp_ajax_nopriv_o99__action', 'o99__ajax_cb' );
add_action( 'wp_ajax_o99__action', 'o99__ajax_cb' );
// this fanction is the ajax callback.
function o99__ajax_cb(){
// Verify the nonce to make sure the request is ours and legael...
if( !isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'o99__nonce' ) ) die( '-1' );
$post_id = isset( $_REQUEST['post_id'] ) ? absint( $_REQUEST['post_id'] ) : 0;
if( ! $post_id ) die( '-1' );
$counter = get_post_meta( $post_id, 'o99_good_post', true );
$counter = absint( $counter );
$cookie = 'o99_post_vote' .$post_id;
if( $counter){
if (!isset($_COOKIE[$cookie]) ) {
$counter++;
}
else {$counter=$counter;}
}
else{
$counter = mt_rand(20,150);
}
update_post_meta( $post_id, 'o99_good_post', $counter ); //hidden field
echo $counter;
die();
}
function o99__good_bad(){
global $post;
// send a simple cookie
$cookie = 'o99_post_vote' .$post_id;
$count = get_post_meta( $post->ID, 'o99_good_post', true ); //hidden field
if (!$count ){
$count = '0';
}
$icon = '<img src="';
$icon .= get_bloginfo('template_url') ; // better using get_stylesheet_uri() now
if (!isset($_COOKIE[$cookie]) ) {
$icon .= '/images/dl-star16.png"/>'; // set up your icons according to case
}
else {
$icon .= '/images/dl-v16.png"/>';
}
?>
<span id="o99__click" title="Click Here to Vote this Post up"><?php echo $icon; ?> click here to vote up
This post was voted <span id="o99__count"><?php echo strip_tags( $count );?>
</span> times</span>
<?php
}
// just injecting the JS into head --
add_action( 'wp_head', 'o99__head' );
function o99__head()
{
if( ! is_singular() ) return;
$post_id = get_queried_object_id();
?>
<script type="text/javascript">
var o99__data = {
action: 'o99__action',
post_id: '<?php echo absint( $post_id ); ?>',
nonce: '<?php echo wp_create_nonce( 'o99__nonce' ); ?>',
}
jQuery(document).ready(function(){
jQuery( '#o99__click' ).click(function(){
jQuery.get(
'<?php echo site_url( 'wp-admin/admin-ajax.php' ); ?>',
o99__data,
function( data ){
if(jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>') != null) {
alert( 'You can only vote once per post!' );
};
if(jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>') == null) {
if( '-1' != data )
{
jQuery( 'span#o99__count' ).html( data );
jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>', 'voted', { expires: 7 });
alert( 'your vote was accepted!' );
};
}
}
);
});
});
</script>
<?php
}
?>
This is a very old function that was hacked a long time ago, it should still work, but maybe need a bit of polish ..
Edit I
some examples of more complex plugins :
http://wordpress.org/extend/plugins/post-ratings/
http://wordpress.org/extend/plugins/post-ratings/screenshots/
http://wordpress.org/extend/plugins/gd-star-rating/
and if you will search the codex you will find much more ..
Those listed above support custom post types .
I want to remove the excerpt function or render it functionless as i want all posts to be viewed in full of its content. I think the theme has some tracking to make sure the excerption is in that line, so it must exist.
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
I only have these 2 lines of codes which i know is related.
I dont know where the $limit come from, i tried to find on all theme related php,no findings.
Please help me. Thank you very muc.
Just find your template file (could be index.php) where you want to display the full content instead of excerpted content and replace the function the_excerpt() with the_content() inside the loop, i.e.
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- do other stuff ... -->
the_content();
<?php endwhile; ?>
<?php endif; ?>
About the_content() and the loop.