I am building a wordpress ecommerce template with Cart66 plugin. Using a Cart66 shortcode inside a php template page, I would like to generate the Post ID inside the shortcode. Can someone please help and tell me if this is possible. Here is the code that I am using.
<?php echo do_shortcode("[add_to_cart item=\". the_ID() .\" quantity=\"user:1\"]"); ?>
This code will lay inside loop-single.php and above
Thank you!
#silent almost had it, but it should be get_the_ID() and not the_ID() since the later one echos it out, so try:
<?php echo do_shortcode("[add_to_cart item=\"". get_the_ID() ."\" quantity=\"user:1\"]"); ?>
The right line should be:
<?php echo do_shortcode("[add_to_cart item=\"". the_ID() ."\" quantity=\"user:1\"]"); ?>
simpler correct answer:
<?php echo do_shortcode('[add_to_cart item="'. get_the_ID() .'" quantity="user:1"]');?>
Related
I'm a little lost here, hoping that someone can help. I'm using the Meta Box plugin for WordPress, and I'm trying to create a process for the user to select an option from a predefined list, and then assign a URL to that option as a link. Im trying to define the URL in a variable, and then call it in a function, but I'm still a little green on PHP syntax. this is my code now:
<?php
$article_url= rwmb_meta('orion_2016_article_url', 'type=URL');
if (rwmb_meta('orion_2016_article_source') != '') {
echo '<a href= ("$article_url") target=blank>';
echo rwmb_meta('orion_2016_article_source');
echo '</a>';} ?> on <?php the_date(); ?>
Since the options are already predefined, it seems like assigning a random URL to one of the options should be pretty simple. Hopefully this makes sense!
You need to to place variables you wish to echo inside double quotes or simply concatenate strings using . as in my example. Note that I didn't check the plugin's specific syntax, only general PHP syntax.
<?php
$article_url= rwmb_meta( 'orion_2016_article_url', 'type=URL' );
if (rwmb_meta('orion_2016_article_source') != '') {
echo '' . rwmb_meta( 'orion_2016_article_source' ); . '';
} ?> on <?php the_date(); ?>
I'm trying to find the category ID of a post and then use it in the short code below. To find the category id I'm using <?php the_category_ID(); ?>, but i'm not sure how to use the output from category id and replace it in include_categories=. i.e. i want to replace number 4 with the current category ID i got from <?php the_category_ID(); ?>.
<?php echo do_shortcode('[include_categories="4"]'); ?>
i tired doing the following, but it didn't work. Any ideas will be appreciated.
<?php echo do_shortcode('[include_categories="<?php the_category_ID(); ?>"]'); ?>
I know i can't use php inside another php code, but i'm not sure how to place the category id between the quotation.
Thanks.
A couple of things: (1) you can't have nested <?php ?> tags, (2) the_category_ID() has been deprecated since WordPress 0.71. You'll want to use get_the_category() instead.
The correct solution would be:
<?php echo do_shortcode('[include_categories="' . get_the_category() . '"]'); ?>
The dots . "concatenate" the string with the returned values of get_the_category(). You can read more about string operators in the PHP docs.
Try with -
<?php echo do_shortcode('[include_categories="'.the_category_ID().'"]'); ?>
Use this to add php verial in short code:
<?php $category=the_category_ID();
echo do_shortcode("[include_categories=$category]"); ?>
I have Woocommerce outputting get_categories and get_tags in the example below, but need them to be text only. At present, it outputs text with a link to the category / tag. Can the link component be removed?
<?php echo $child_product['product']->get_categories(); ?>
<?php echo $child_product['product']->get_tags(); ?>
Thank you.
In case anyone else was looking for a solution to this...
<?php echo strip_tags ($child_product['product']->get_categories()); ?>
I'm not very familiar with PHP and have been trying my hardest to figure out how to create this URL. So far, this is working:
<?php echo site_url($p->post_title) ?>
Where post title is defined by the Mapify.it Wordpress plugin. The result is:
http://siteurl.com/post_title
What I'd like to do is add a string before it, ideally ?s= or /search/, but when I try to add this before $p->post_title I'm still generating the above URL. Variations such as:
<?php echo site_url('?s=', $p->post_title) ?>
<?php echo site_url('/search/', $p->post_title) ?>
produce http://siteurl.com/?s= and ignore the variable. Nothing seems to do what I want.
What am I doing wrong?
Hope you need the following url format,
http://siteurl.com/?s=Here come the post title
So,
<?php echo site_url("?s=".$p->post_title) ?>
OR
<?php echo site_url("/search/".$p->post_title) ?>
should work.
Found it!
<?php echo site_url('?s='), $p->post_title ?>
Instead of adding custom URL Parameters directly, I'd suggest you to use WordPress built-in function add_query_arg(), it's more cleaner.
Here is an usage example:
$url = get_site_url();
$params = array(
's' => $p->post_title
);
echo add_query_arg($params, $url);
You can specify multiple parameters this way.
For ref: Check add_query_arg()
I have a commercial site (php), and have a Wordpress blog in a subdirectory. I need to display latest posts at homepage which is out of Wordpress :/
site:
http://www.blabla.com
blog:
http://www.blabla.com/blog/
So I need to display posts at www.blabla.com/index.php. How can I access Wordpress functionality?
Thanks a lot! appreciate!
The easiest way is to consume your Wordpress RSS feed.
Download it using file_get_contents() or cURL for more control.
Parse it with simpleXML and output it.
You'll probably want to cache it somewhere... you could use APC user functions or PEAR::Cache_Lite.
Edit: the code would look something like this (you'd want more error checking and stuff - this is just to get you started):
$xmlText = file_get_contents('http://www.blabla.com/blog/feed/');
$xml = simplexml_load_string($xmlText);
foreach ($xml->item as $item)
{
echo 'Blog Post: <a href="' . htmlentities((string)$item->link) . '">'
. htmlentities((string)$item->title) . '</a>';
echo '<p>' . (string)$item->description . '</p>';
}
Using WordPress best practices, you shouldn't be loading wp-blog-header.php, but rather wp-load.php, as it was specifically created for this purpose.
After this, use either the WP_Query object or get_posts(). An example of how to use WP_Query is available on The Loop page on the WordPress codex. Although using either of these doesn't matter if you use them from outside WordPress, there's less chance of something interfering, such as GET parameters.
For example, using WP_Query:
<?php
$my_query = new WP_Query('showposts=3');
while ($my_query->have_posts()): $my_query->the_post();
?>
<h1><?php the_title() ?></h1>
<?php endwhile; ?>
Or, using get_posts():
<?php
global $post;
$posts = get_posts('showposts=3');
foreach($posts as $post) :
?>
<h1><?php the_title(); ?></h1>
<?php endforeach; ?>
Hope this helps! :)
hey just found a solution online;
http://www.corvidworks.com/articles/wordpress-content-on-other-pages
works great!
<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('blog/wp-blog-header.php');
query_posts('showposts=3');
?>
<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
I guess the easiest solution is to take posts directly from database.