Wordpress Primary Category - wordpress

I recently needed to change my permalink structure in Wordpress. However the change caused all my social sharing counters to reset. I found this great script which I modified below, http://encosia.com/preserving-social-sharing-counters-through-a-url-change/. I rewrote it so it would remove the %poste-id% at the end of the url on older posts.
The problem is that for the most part this works, however I need it to call the permalink category not the first category in the get_the_category(); array. Is there anyway I can pull this info, or modify this script to work that way? This is also being used in the loop. Thanks!!
<?php
$url_change_id = 68135;
$postid = $post->ID;
$category = get_the_category();
$slug = $post->post_name;
$permalink_url = get_permalink();
if (intval($postid) < $url_change_id) {
$url_date_prefix = "/" . $category[0]->category_nicename .
"/" . $slug .
".html";
$sharing_url = str_replace($permalink_url,
"http://website.com" . $url_date_prefix,
$permalink_url);
} else {
$sharing_url = get_permalink();
}
?>
<?php echo $sharing_url; ?>

Related

Wordpress grab page attributes

Is there a Wordpress function for grabbing the page attributes? I need to be able to check which templates are being used on which pages.
I have tried the get_post and get_pages but neither one outputs the page attributes.
Thanks in advance!
solution:
$ids= get_all_page_ids();
foreach ($ids as $id){
$meta = get_metadata('post', $id);
//var_dump($meta);
$template = $meta['_wp_page_template'][0];
echo $template;
echo "<br>";
}
Try using get_all_metadata. That will fetch all the meta records for a given object.
<?php
$post_id = 123;
$meta = get_metadata('post', $post_id);
echo $meta['my_custom_field_key'];
The docs are a good place to look: Function Reference « WordPress Codex
i.e.: Function Reference/get page template which
Displays the filename of the page template used to render a Page
(printed within an HTML comment, in this example) :
<?php echo '<!-- ' . basename( get_page_template() ) . ' -->'; ?>
And,
global $wp_query;
$template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );
will give you the template file name. Use str_replace() to strip the .php from the end.
`

how to determine is_home in wordpress if query a category?

I need to query a category in home page. in index.php file I used this script
$all_featured_posts = query_posts(array('category_name'=>'featured-programs'));
Then in the header.php file I need to change the title
<title>
<?php
if ( is_home() ) {
echo 'My site name' ;
} elseif (is_404()) {
echo '404 Not Found';
} elseif (is_category()) {
echo ' Category' . wp_title('',0).' | My site name' ;
}
?>
The problem is when I query a category in the index file then the is_home return false ( Tried with is_front_page() also ) Then it alway show the title with the name of the category which I query.
How I can fix it? Thanks you!
I might be wrong, but I think because you use query_posts(), all your is_* functions change their values. And, well, because you do query a category, is_home() should return false.
What you can do to solve it, is use new WP_Query(), and get all the posts from it. This way, you will not be affecting the original WP_Query, and thus the is_* functions.
The code should look like this:
$query = new WP_Query('category_name=featured-programs');
while ( $query->have_posts() ) : $query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();

Can't get my WordPress site to update its own feed?

I'm using the following code to display some entries of my WordPress blog as a feed in the sidebar. The problem is, it's not updating no matter what I do. It still only shows the first "Hello World" post, even though I've added others, and it doesn't even show the updated name of that post after I've changed it. Thought this might be a caching issue, but if I actually click into the feed XML, the data is updated- which makes no sense to me??
<?php
// Blog Feed:
$rss_url = get_option('home')."/feed/";
?>
<ul class="side-feed">
<?php if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed($rss_url); // specify the source feed
$limit = $feed->get_item_quantity(3); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
echo count($items);
}
if ($limit == 0) echo '<div>(None)</div>';
else foreach ($items as $item) : ?>
<li><?php echo $item->get_title(); ?></li>
<?php endforeach; ?>
</ul>
Wordpress caches feeds for 12 hours by default, to change this you need to hook into the wp_feed_cache_transient_lifetime filter and return the number of seconds you want to cache for.
add_filter('wp_feed_cache_transient_lifetime', create_function('', 'return 60*60;'));

How to get the category title in a post in Wordpress?

Say I have a post called Hello World in Wordpress and I'm directly viewing this page, how would I go about finding the category of "Hello World" and displaying it?
Use get_the_category() like this:
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
It returns a list because a post can have more than one category.
The documentation also explains how to do this from outside the loop.
You can use
<?php the_category(', '); ?>
which would output them in a comma separated list.
You can also do the same for tags as well:
<?php the_tags('<em>:</em>', ', ', ''); ?>
To find the category id, category link and category name in wordpress using php, you can use below code:
$cat_id = get_cat_ID('Category Name');
$category_link = get_category_link($cat_id);
$category_name = get_cat_name($cat_id);
echo $category_name; // It will print your category name

Unable to get get_the_tags() in Wordpress Template

I am using this code to get the tags in my wordpress posts for a theme
`<?php
$posttags = get_the_tags();
if ($posttags) {
foreach ($posttags as $tag) {
$tagnames[count($tagnames)] = $tag->name;
}
$comma_separated_tagnames = implode(", ", $tagnames);
print_r($comma_separated_tagnames);
}
?>`
The PROBLEM is that it is returning tags for "all posts" not just individual posts, and I think the problem is that if a post DOESNT have tags - it just inserts tags anyway.
Can anyone help modify this so:
It return tags only for a post - not all tags
If there are no tags for a post, dont return anything
P.S - Can check out here for the wordpress docs
<footer class="entry-footer">
<?php //get all tags for the post
$t = wp_get_post_tags($post->ID);
echo "<p class='tags-list'>TAGGED WITH: ";
foreach ($t as $tag) {
$tag_link = get_tag_link($tag->term_id);
echo "<a href='$tag_link' class='used-tag' rel='tag'>".($tag->name)."</a> ";
}
echo "</p>";
?>
</footer>
This is what I did, displays tags for each post in the loop.
Try using:
<?php the_tags(); ?>
Inside the 'Loop'.
Function Reference
alright I hope this could help someone , I stuck on this issue for about hour
trying to get tags of my post "so I can mix it with twitter share link "
the_tags(); function were useless since I use it out of WP loop , get_the_tag_list(); were perfect to me since it can include post id ,
$postid = $wp_query->post->ID;
$posttags = strip_tags( get_the_tag_list( ' ', '', '', "$postid" ) ) ;
^^ the above code I stripped html codes to get tag name without href link .
this is the function use case :-
get_the_tag_list( string $before = '', string $sep = '', string $after = '', int $id )

Resources