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;'));
Related
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; ?>
What I need - The code to perform the following:
I am trying to setup a WordPress template that will display all the comments that have been posted to my blog. How do I pull all comments and have all the same formatting that is applied to comments under a single post? Such as the formatting that occurs when comments are displayed using the comments.php template.
Note I want to pull all the comments from my blog to a single page. I still want the comment pagination but instead of having 20 comments under post #1, 20 under post #2, etc. I want to have all 40 show up at one time on one page.
You want to use the get_comments() function.
<?php foreach (get_comments() as $comment): ?>
<div><?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".</div>
<?php endforeach; ?>
See also the apply_filters() function to apply comment output filters to specific fields.
<?php echo apply_filters('comment_text', $comment->comment_content); ?>
EDIT:
For pagination, you can use the offset and number parameters of the get_comments() arguments:
<?php
$args = array(
'number'=>20,
'offset'=>0,
'status'=>'approve',
);
foreach (get_comments($args) as $comment) {
// ...
}
?>
Here's the code I have:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$url = get_bloginfo('url');
if (current_user_can('edit_post', $post->ID)){
echo '<a href="';
echo wp_nonce_url("$url/wp-admin/post.php?post=$postid&action=delete", 'delete-post_' . $post->ID);
echo '">Delete your listing</a>';
}
?>
I'm trying to delete the post outside the wordpress loop.
When I click the link, I get:
"Your attempt to delete this post: “post-name” has failed.
Please try again."
Does anyone know why that would be?
Looking at the reference, it looks like current_user_can() only takes one argument.
Perhaps the root cause here is a permissions issue for the user. You should be checking if a user has the delete_posts capability as seen below. Note that delete_posts checks if the user can delete their own posts. To check if the user can delete the posts of other users, you can use delete_other_posts instead.
if (current_user_can('delete_posts')){
echo '<a href="';
echo wp_nonce_url("$url/wp-admin/post.php?post=$postid&action=delete", 'delete-post_' . $post->ID);
echo '">Delete your listing</a>';
}
(Assuming you are using WP version 2.1 or later)
I'm using the advanced custom field plugin for Wordpress to show the results of checkboxes. I have what I want working, I just want to tidy up the code and add the following:
Remove the underscore from the social media tag (some kind of stripping out???).
If possible I'd like to show a comma after each "tag" but not if it's the last one.
Here's my test page, they're the blue "tags" under the discipline section.
Here's my code:
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){
echo ''.strtoupper($name).'';
}
}
?>
Well it is pretty basic, you just have to do a loop. I could have write something better with more information... anyway this should do exactly what your code did but in a loop.
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){ //I don't know what this is suppose to do
echo ''.strtoupper($name).'';
}
}
?>
Try this out:
<?php foreach( get_field('categories') as $category ): ?>
<?php echo ucwords($category) ?>
<?php endforeach; ?>
Ok this should be better
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
$theID = get_cat_ID($name); // get the ID of each category
echo ''.$theID->name.'';
}
?>
I built a very unique and javascript intensive theme for wordpress and now shortcodes do not work. I do not have any plugins installed, so it's not that. What did I drop from my wordpress template files that is required to use shortcodes (ie: [gallery]).
I understand how to make shortcodes, but how does WP take your post and replace "[gallery]" when it is spitting it back out for display?
EDIT:
here is what I'm currently working with:
$pagepull = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' ORDER BY menu_order", ARRAY_A);
$i = 1;
foreach ($pagepull as $single_page){
echo "<div class=\"section\"><ul><li class=\"sub\" id=\"" . $i . "\"><div class=\"insection\">";
echo $single_page['post_content'];
$i++;
// more code that is irrelevant...
// more code that is irrelevant...
// more code that is irrelevant...
}
Ok, try this
$pagepull = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' ORDER BY menu_order", ARRAY_A);
$i = 1;
foreach ($pagepull as $single_page){
echo "<div class=\"section\"><ul><li class=\"sub\" id=\"" . $i . "\"><div class=\"insection\">";
echo apply_filters('the_content',$single_page['post_content']);
$i++;
Wordpress take your content and apply filters to it. You must register a filter and let parse your content.
If your theme is not displaying your shortcodes, probabily you output the content of the post without let Wordpress filter it.
Calling the function get_the_content() for a post, does not run the filter for shortcodes (if any).
To have apply
<?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?>
Ref: http://codex.wordpress.org/Function_Reference/get_the_content
Note: many plugins register filters with the content to implement shortcodes!
My solution was replacing
<?= get_the_content() ?>
with
<?= the_content() ?>
which, as keatch already mentioned, applies filters before returning content.
Read this carefully about the_content
use this if you want the content inside a variable:
ob_start();
the_content();
$content = ob_get_clean();
now you could just do echo $content; or use regex or whatever you want to make the content look like how you want it.
I had the same issue.
Shortcodes depends on WP Loop, but that's a different issue. To make a long story short, I've added the_post(); at the page that should be showing the shortcode (for example articles.php).
Also, make sure that you are using the_content() in order to display the text (using $post->post_data for example won't show you shortcodes).
Please use the
ob_start();
in the starting of function and use
return ob_get_clean();
before closing the function.
Hope this will help full for you.
Cheers