Wordpress get_permalink($this->get_pageid()) showing wrong url - php

I am using get_permalink($this->get_pageid()) for getting page url.
my permalink is enables as sample-post. It should be example.com/forum but it is showing example.com/login

Your question is not quite clear because get_pageid() is not a wordpress' native function and you didn't provide any code. Anyways...
Actually get_permalink() expects an optional id as an argument which should be an integer and this is required only when you are using it outside the loop so make sure your $this->get_pageid() returns an integer value. For an example it should be something like this
get_permalink(10) // 10 should be your page/post id
Also you can use
get_permalink(get_page_by_title('Some Page')) // Some Page is page/post title
Alternatively, you can use
global $post;
get_permalink($post->ID); // $post->ID will return the page/post id
when you are outside of the loop. Hope this helps.

Related

WordPress remove custom variables from URL

I am appending "?sel" to my URL in order to keep track of which link was selected, and then show items based on that selection in the next page. I used the following in functions.php to register it as a Query var (rather than just use $_GET):
function add_query_vars_filter( $vars ){
$vars[] = "sel";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
Then I created the link with the var using add_query_arg() in the template where the links are.
$link = add_query_arg( 'sel', $slug, get_the_permalink($news_specials_listing_page_id) );
<a href="<?php echo $link ?>">
However, if I click that link, I still see the "?sel" in the URL. I've got my permalinks setting to post name in settings > permalinks, so I don't see the typical WordPress variables. Is it posible to hide "sel" along with the rest of the registered query variables, and still grab the value using get_query_var()?
I have also tried adding a custom rewrite tag and flushing my permalinks.
function custom_rewrite_tag() {
add_rewrite_tag('%sel%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
I have been experimenting with add_rewrite_rule() and have found that it adds RewriteRules to .htaccess. So if I add a rewrite rule that captures the sel variable in a regular expression and redirect it, would the variable still get saved? I'm also having a difficult time figuring out what the rule would be.
The whole point of using GET is to put variables into your URL. This forms a unique URL that can be bookmarked and indexed by search engines. If you do not want to do this use POST instead. POST allows you to encrypt variable names making it far more secure than the solution you are asking about.

How to write a shortcode with variable to display content from specific custom post type post?

I've only written very basic shortcodes without variables and am just not figuring out how to write one that allows someone to enter a variable to pull info from a specific post.
I have a custom post type called "Events". I want to put a shortcode on the site FrontPage that will display the contents of a specific event within that custom post type. I'm assuming a variable is the way to do this, but I don't know what the best way is. Should I be having the user find the post ID number to use as the variable? Note that I am not trying to display an arcive of the post type, only the contents of a specific post, as indicated by the shortcode variable. I can see something like the following, but don't know how to achieve it:
[display-event id="77"]
Really, this is advanced for me, so any direction you can give me would be much appreciated.
~Laura
Have a look at WordPress's documentation for add_shortcode().
You would need to add something like the following in your functions.php file:-
function baztag_func( $atts, $content = "" ) {
// What you would like your short code to do
return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
add_shortcode

How to save the content of shortcode in wordpress options?

I am developing a plugin with Wordpress. But I need to save a short code like [my-shortcode] and then in my front end show the final result of the short code. Imagine that I have one form where you can upload an image, select width, height and preloaded themes, animations, etc. But I want to use short code.
How to save the compiled short code with do_shortcode() with Wordpress options?
I think, maybe I can do it with javascript, but I didn't found it on the web.
You cannot save a shortcode with do_shortcode(). do_shortcode()is used to render a shortcode.
You can use a metabox that renders an input and then you can use your JavaScript code that targets that input and stores your shortcode in its value attribute.
You can then refer to that post meta data which holds your shortcode by using the get_post_meta(); method provided by WordPress.
Here is the link to get_post_meta http://codex.wordpress.org/Function_Reference/get_post_meta
You can refer to how to add a metabox in WordPress here:
http://codex.wordpress.org/Function_Reference/add_meta_box
Your input tag should be coded like so in your callback function:
echo '<input type="hidden" id="My-Shortcode" name="My-Shortcode" ' . get_post_meta($post->ID, 'My-Shortcode', true) . '/>';
The input with the name My-Shortcode will actually hold your shortcode but as you see it requires a $post object which actually hold all the data for that post. You can then refer to your shortcode key by stating
get_post_meta($post->ID, 'My-Shortcode', true)
True simply means the value will be echoed out.
Now in order to access any data within that $post object you need to pass the $post parameter to your function. Normally WordPress gives you access to that $post object but you need to state that in your metabox callback function like so:
function My_callback_function( $post ){
// your input will be here
}
In the front end you can then simply call get_post_meta again like:
echo get_post_meta($post->ID, 'My-Shortcode', true)
Keep in mind that there are security issues when saving data to and from your WordPress database which is why you would like to use WordPress's nonce system. Here's the link. Investigate to know more: http://codex.wordpress.org/WordPress_Nonces

Wordpress Conditional if is_single

Im trying to use a conditional statement on my single.php page.
What I want to do is, if it is of the Custom Post Type current-products, use a specific single -product.php template page, if not (ie a standard blog post) use the default single.php page.
I think Im using the right statement, but don't know what to do afterwards (to get the template to be used):
if ( is_single( 'current-products' == get_post_type() ) {
// If the post type is "Current Products" use this template...
// Please help me out on this bit
} elseif ( is_single() ) {
// If not, use the standard one...
// please help me out on this bit
}
I think that's right...?
WordPress automatically uses a different template page for different post types, if your post type is called products the files should be named single-products.php. You can read more about it from there Codex
In the same way single posts and their archives can be displayed using the single.php and archive.php template files, respectively,
single posts of a custom post type will use single-{post_type}.php
and their archives will use archive-{post_type}.php
where {post_type} is the $post_type argument of the register_post_type() function.
You can use is_singular() function, according to WordPress documentation: Ref Link
This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
True when viewing a post of the Custom Post Type book.
is_singular('book');
This will accomplish your task.
I believe if you want to use for posts try this:
is_singular('post');
Thanks

WordPress echo image and url

I'm trying to get a dynamic image to display the image defined on a variable, and also to have the link to that url to be the same as the permalink for the item.
echo '<img src="'.$thumburl.'" alt="Status">';
Any idea what I'm doing wrong with that code?
code use on your loop:
<?php
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'thumbnail');
?>
<?php echo '<a href="'.get_permalink().'"><img src="'.$image_url[0].'"
alt="Status"></a>'; ?>
You need to use get_permalink() instead of the_permalink()
echo '<img src="'.$thumburl.'" alt="Status">';
get_permalink():
Returns the permalink to a post or page for use in PHP. It does NOT display the permalink and can be used outside of The Loop. On failure returns false.
the_permalink():
Displays the URL for the permalink to the post currently being processed in The Loop. This tag must be within The Loop, and is generally used to display the permalink for each post, when the posts are being displayed. Since this template tag is limited to displaying the permalink for the post that is being processed, you cannot use it to display the permalink to an arbitrary post on your weblog. Refer to get_permalink() if you want to get the permalink for a post, given its unique post id.
You don't seem to tell us what the problem is. I suppose that the image is not displayed. The code is OK.
Double check the image path stored in $thumburl. Inspect the element in a browser to check that img's source and post it here. Are you using an absolute path in $thumburl?

Categories