Get a specific page's excerpt within the loop Wordpress - wordpress

I am trying to create a loop that loads a random image from any posts, whilst also retrieving the excerpt of a specific page. I have done the random post part, but cannot get it to retrieve the page excerpt... I think I may need to query the pages in their own loop but I'm not sure how to do this. I have installed the function to get page excerpt support etc. but I think I am doing something wrong within the loop, any help would be appreciated.
<div class="postimage">
<?php if (have_posts()) :
query_posts('showposts=1&orderby=rand');
while (have_posts()) : the_post(); ?>
<?php the_post_thumbnail('blog-post-image'); ?>
<div class="borderimage"></div>
<div class="tagline"><h1><?php the_excerpt('$page_id=8'); ?> </h1>
</div>
</div>
</div>
<?php endwhile; else : endif; ?>

query_posts replaces the global $wp_query, which you don't want to do since you want to keep that query for your page. Try this instead...
if (have_posts()){
while(have_posts()){
the_post(); //global $post now has the page in it
$args = array("posts_per_page"=>1,"orderby"=>"rand");
$random_posts = get_posts($args); //returns an array
$random_post = $random_posts[0];
//do your stuff...
//$post contains the original page
//$random_post contains the random post
}
}

Related

ACF: Display values outside the main loop

I am coding a theme that uses an archive on a page with a special template. Above the results of the archive (that also means outside of the loop) I want to display the values of some custom fields. (e.g. the value of the field “intro”. I tried to create an additional loop and have the values displayed, but there are no results. Does anybody see a mistake?
Thanks!
Raphael
Here is what I tried last:
<div class="newsearch_intro">
<?php global $post;
$args = array('numberposts'=>'1');
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
the_field('intro');
endforeach;
?>
</div>
Try using the_field() / get_field() with the post ID as a second parameter:
<div class="newsearch_intro">
<?php
global $post;
the_field('intro', $post->ID);
?>
</div>

Cant' Grab WordPress Built-in Posts Through Loop

Using WordPress 3.7.1 I am trying to display all Regular Post on my created page lest say TestPage. Here are the steps I took to do this:
1- Generate a Custom Page Template called:Test Page and loaded by following code
2- Generate a Page Called TestPage based on Test Page Template
after updating the page I am not getting any of Post on the page while I have already generated some!
<?php
/*
Template Name: Test Page
*/
?>
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>Sorry, this page does not exist</p>
<?php endif; ?>
<?php get_footer(); ?>
he abouve code actually is loading the page whit title and content of the TestPage and not by Posts!Can you please let me know why this is happening?
You must understand the difference between post and page.
A page is associated with a template. Through code, you can lists posts or other pages using this page /template.
The above code will only display the Page data
You don't want to create many pages using the same tempalte in order to show different data with the same tempalte. Instead you create posts and give them a category.
Then you can fetch these posts and display them in the same way asthe test-page.
You can either use get_posts or wp_get_recent_posts.
Or, if you really want to list pages, use wp_list_pages.
Besides that, you should look at http://codex.wordpress.org/Pages and http://codex.wordpress.org/Posts

How to display list categories in wordpress integration magento

Can help somebody. I spent several hours to find solution but without results
I tried to display the list of categories on homepage wordpress blog thru following code
<?php $category = Mage::registry('wordpress_category') ?>
<?php if ($category): ?>
<?php echo $category->getId() ?>: <?php echo $category->getName() ?>
<?php endif; ?>
But the method
Mage::registry('wordpress_category')
always return null.
I found that, i should probably be using the Fishpig_Wordpress_Block_Category_View. But i dont know where i should put it.
The following code will retrieve the current category when viewing a category page in your blog:
<?php Mage::registry('wordpress_category') ?>
This is not what you need. To view a list of categories, you could create a custom collection using the following:
<?php $categories = Mage::getResourceModel('wordpress/post_category_collection') ?>
A better way would be to use the category widget block:
<block type="wordpress/sidebar_widget_categories" name="wp.categories" template="wordpress/sidebar/widget/categories.phtml" />
You can create this in PHP using the following code:
<?php echo Mage::getSingleton('core/layout')
->createBlock('wordpress/sidebar_widget_categories')
->setTemplate('wordpress/sidebar/widget/categories.phtml')
->toHtml() ?>
The above code uses the default template, however, feel free to use your own custom template.

Display specific post format with limit number of post

I create a loop in Wordpress with the condition like this:
Display a specific post format (example: video)
Limiting the post number. In this case, I only want to display 2 posts.
Here my code:
<?php $i = 1; if (have_posts()) : while (have_posts() && $i < 3) : the_post(); ?>
<?php get_template_part( 'content-video', get_post_format() ); ?>
<?php $i++; endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I already have file name content-video.php.
Unfortunately, the loop does not work. Only displayed the first post, not a specific post (video post format) with template from content-video.php.
Appreciate for any help, or alternative code. Thanks in advance.
My remarks about your code:
I think you have bad wrapping for if else while statements. You are missing an endif at the end in the case your approach is correct.
Why use inside your code the i variable since you can customize any query in WP, especially for number of post using the parameter posts_per_page.
Why not to use the loop inside content-video.php and write only:get_template_part('content-video', get_post_format()); Like in single.php and loop-single.php the themes provided with wordpress installation (twenty themes) .
Good luck
the best way would be to get posts of "video" post format from a query, but I'm not sure how:)
As a workaround you could do this inside the loop:
if (!has_post_format('video'))
continue;

WordPress Rewind Posts

I have the following code: http://pastebin.com/ng99augD
It basically spits out five posts for the five authors I have specified in the query posts in each chunk of code and then rewinds the posts prior to the next query. This is far from ideal and would like to make the code more efficient without having to repeat all the template code for the posts over and over. I also want them to be in a random order instead of coming out in the order they have been written as I don't want any particular author to be at the top. Thanks.
I'm not sure why the use of rewind_posts() is necessary. You are creating a new query using by your use of query_posts() each time. rewind_posts() rewinds the query for re-use.
If your performance isn't suffering, it may be okay to run 5 queries to get your five authors. To avoid rewriting the same template code each time you have at least two options. Create a php file with the template code in it, and include it each time you need it:
<?php include('author_posts.php');?>
Or, better yet, create a function in your functions.php or in a plugin file that accepts your query results and outputs the template.
<?php $posts = query_posts('posts_per_page=1&author=author1');
if(function_exists('my_authors')){my_authors($posts);}
?>
Then in your functions.php or plugin:
function my_authors($posts){
//template code here, using $posts->ID, $posts->post_content, etc
}
The third option, which cleans up your code and the number of queries would be to use a category to display posts assigned to it here. You are making 5 queries to display five posts. To use one query to display five posts and sort randomly like you want to, edit each of the five posts and assign them to a new category, lets say:'author sampler'. Then in your template file:
<?php $posts = get_posts('category=author sampler'&order_by=rand&order=asc');
foreach($posts as $post):?>
<?php setup_postdata($post);
//your posted template code follows:
?>
<article id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?><?php edit_post_link('Edit', ' (', ')'); ?></h2>
<p class="p-cat">In: <?php the_category('|') ?></p>
<p class="p-author">
<span class="name">
<?php the_author_posts_link(); ?></span> <span class="avatar"><?php echo get_avatar( $email, $size = '32' ); ?>
</span>
</p>
<?php //etc ?>
<?php endforeach;?>

Resources